两个值的弹性搜索查询 - 术语和范围?

Elastic search query on two values - term and range?

我正在尝试根据两个值(名称(字符串)和百分比(数字))查询我想要的文档。 示例是 - 我想要那些具有 "audience.location.countries.name" = "US" 和 "audience.location.countries.percentage" > 60 的文档。所以我想要一个具有对象 {"name":[=19= 的文档], "percentage":"65"} 这里,"audience.location.countries" 是一个具有两个属性的对象数组 - {"name","percentage"}。 这是一个示例文档:

"location": {
              "countries": [
                {
                  "name": "CA",
                  "percentage": 4
                },
                {
                  "name": "GB",
                  "percentage": 5
                },
                {
                  "name": "JP",
                  "percentage": 8
                },
                {
                  "name": "US",
                  "percentage": 60
                }
              ]}  

这是我尝试过的查询,但它抛出错误:“[and] 查询格式错误,查询名称后没有 start_object”

GET opensponsorship/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "isPublic": true
              }
            },
            {
              "term": {
                "isDeleted": false
              }
            },
            {
              "and":[
                  {
                    "range": {
                      "audience.location.countries.name": "US"
                    }
                  },
                  {
                    "term":{
                      "audience.location.countries.percentage": {"gt": 59}
                    }
                  }
                ]
            }
          ]
        }
      },
      "size": "60",
      "from": 0,
      "sort": [
        {
          "followers": {
            "order": "desc"
          }
        }
      ]
    }

我是弹性搜索的新手,知识非常有限。有人可以帮忙吗?

据我所知,查询是 "broken" 几个帐户:

  1. 您使用 deprecated and query(顺便说一句,您使用的是什么 ES 版本?)
  2. rangeterm 过滤 namepercentage 字段是混合的
  3. 字段 audience.location.countries 应该是“nested object

以下是解决问题 1 和 2 的查询。然后我将解释问题 3:

GET opensponsorship/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "isPublic": true
          }
        },
        {
          "term": {
            "isDeleted": false
          }
        },
        {
          "term": {
            "audience.location.countries.name": "US"
          }
        },
        {
          "range": {
            "audience.location.countries.percentage": {
              "gt": 59
            }
          }
        }
      ]
    }
  },
  "size": "60",
  "from": 0,
  "sort": [
    {
      "followers": {
        "order": "desc"
      }
    }
  ]
}

关于问题 3,我建议您阅读 elasticsearch 中的 nested fields。 简而言之 - 如果 audience.location.countries 不是嵌套对象,由于 Elastic "flattens" 对象的方式,您将得到 "false positive" 结果。 要解决此问题,您需要 1) 在映射中使 audience.location.countries 成为嵌套对象类型,以及 2) 按以下方式使用嵌套查询包装 contries 术语过滤器:

GET opensponsorship/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "isPublic": true
          }
        },
        {
          "term": {
            "isDeleted": false
          }
        },
        {
          "nested": {
            "path": "audience.location.countries",
            "query": {
              "bool": {
                "filter": [
                  {
                    "term": {
                      "audience.location.countries.name": "US"
                    }
                  },
                  {
                    "range": {
                      "audience.location.countries.percentage": {
                        "gt": 59
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "size": "60",
  "from": 0,
  "sort": [
    {
      "followers": {
        "order": "desc"
      }
    }
  ]
}

希望这对您有所帮助。祝你好运!