如何在dsl查询中对搜索参数应用筛选器

jljoyd4f  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(355)

我有一个巨大的客户数据保存在ElasticSearch
我的问题在下面 {"query": {"query_string": {"query": "Mobile"}}} 问题1
我需要过滤掉
{country: Germany} "filter": [ { "term": { "Country.keyword": "Germany" }}] 问题2
我需要过滤掉 {country: Germany} 以及 {continent:Europe}

qkf9rpyu

qkf9rpyu1#

添加索引数据、搜索查询和搜索结果的工作示例

{
  "item":"mobile are essential",
  "Country":"India",
  "continent":"Europe"
}
{
  "item":"mobile",
  "Country":"Germany",
  "continent":"Europe"
}
{
  "item":"mobile",
  "Country":"Germany",
  "continent":"asia"
}

第一个问题, need to filter out {country: Germany} ,您需要在bool query中 Package 查询:

{
      "query": {
        "bool": {
          "must": {
            "query_string": {
              "query": "Mobile"
            }
          },
          "filter": {
            "term": {
              "Country.keyword": "Germany"
            }
          }
        }
      }
    }

搜索结果:

"hits": [
      {
        "_index": "stof_64278091",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.15965708,
        "_source": {
          "item": "mobile",
          "Country": "Germany",
          "continent": "Europe"
        }
      },
      {
        "_index": "stof_64278091",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.15965708,
        "_source": {
          "item": "mobile",
          "Country": "Germany",
          "continent": "asia"
        }
      }
    ]

对于第二个问题,请尝试以下查询:

{
  "query": {
    "bool": {
      "must": {
        "query_string": {
          "query": "Mobile"
        }
      },
      "filter": [
        {
          "term": {
            "Country.keyword": "Germany"
          }
        },
        {
          "term": {
            "continent": "Europe"
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64278091",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.22920428,
        "_source": {
          "item": "mobile",
          "Country": "Germany",
          "continent": "Europe"
        }
      }
    ]

相关问题