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

Ank*_*wal 1 elasticsearch kibana

我正在尝试执行一个查询,其中我想要基于两个值的文档 - 名称(字符串)和百分比(数字)。示例是 - 我想要那些具有 "audience.location.countries.name" = "US" 和 "audience.location.countries.percentage" > 60 的文档。所以我想要一个具有对象 {"name": "US", "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
                }
              ]}  
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的查询,但它引发错误:“[和] 查询格式错误,查询名称后没有 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"
          }
        }
      ]
    }
Run Code Online (Sandbox Code Playgroud)

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

sel*_*ape 6

据我所知,查询在几个帐户上“已损坏”:

  1. 您使用已弃用的and查询(顺便说一句,什么 ES 版本?)
  2. rangeterm过滤器在namepercentage字段上混合使用
  3. 该字段audience.location.countries应该是一个“嵌套对象

以下是解决问题 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"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

关于问题 3,我建议您阅读elasticsearch 中的嵌套字段。简而言之 - 如果audience.location.countries不是嵌套对象,由于 Elastic 如何“扁平化”对象,您将获得“误报”结果。要解决此问题,您需要 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"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。祝你好运!