elasticsearch - 如何编写一个匹配然后过滤记录的 Elasticsearch 查询?

标签 elasticsearch search lucene

在Elastic Search中,我编写了一个查询,该查询匹配搜索到的术语并获取结果。
这是我使用的查询。

  {
    "size": 40,
    "query": {
      "bool": {
        "must": [
          {
            "match": {
              "name": {
                "query": "Salem Chennai",
                "fuzziness": 1,
                "prefix_length": 2,
                "operator": "or"
              }
            }
          }
        ]
      }
    }
  }

这是我得到的答复:
    "timed_out": false,
    "_shards": {
      "total": 5,
      "successful": 5,
      "skipped": 0,
      "failed": 0
    },
    "hits": {
      "total": 2,
      "max_score": 11.182817,
      "hits": [
        {
          "_index": "locations",
          "_type": "city",
          "_id": "1610",
          "_score": 11.182817,
          "_source": {
            "name": "Chennai",
            "code": "IN-TN-CENAI",
            "province_name": "Tamil Nadu",
            "province_code": "IN-TN",
            "country_name": "India",
            "country_code": "IN"
          }
        },
        {
          "_index": "locations",
          "_type": "city",
          "_id": "24216",
          "_score": 9.688159,
          "_source": {
            "name": "Salem",
            "code": "US-IN-SALEM",
            "province_name": "Indiana",
            "province_code": "US-IN",
            "country_name": "United States",
            "country_code": "US"
          }
        }
    ]
  }


现在,我想过滤在country_code上粘贴的响应。

所以我尝试了这个查询
{
    "size": 40,
    "query": {
      "bool": {
        "must": [
          {
            "match": {
              "name": {
                "query": "Salem Chennai",
                "fuzziness": 1,
                "prefix_length": 2,
                "operator": "or"
              }
            }
          }
        ],
        "filter": [
          {
            "term": {
              "country_name": "India"
            }
          }
        ]
      }
    }
  }

这是索引映射:
{
  "locations" : {
    "mappings" : {
      "city" : {
        "properties" : {
          "code" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "country_code" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "country_id" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "country_name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "id" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "province_code" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "province_country_code" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "province_country_id" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "province_country_name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "province_id" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "province_name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

我应该有一个结果,但是,此查询没有任何结果。
有人可以帮我写一个可以匹配的查询,然后从 flex 搜索中过滤记录吗?

我以此为引用:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html#query-filter-context-ex

最佳答案

您应该使用关键字进行过滤,如下所示:

{
    "size": 40,
    "query": {
      "bool": {
        "must": [
          {
            "match": {
              "name": {
                "query": "Salem Chennai",
                "fuzziness": 1,
                "prefix_length": 2,
                "operator": "or"
              }
            }
          }
        ],
        "filter": [
          {
            "term": {
              "country_name.keyword": "India"
            }
          }
        ]
      }
    }
  }

字段country_name(不带.keyword)在索引时进行分析(规范化),因此根据您的分析器,它已转换为其他内容(例如,小写,可能是词干,...)。

关于elasticsearch - 如何编写一个匹配然后过滤记录的 Elasticsearch 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59646573/

相关文章:

elasticsearch - 按标志过滤文档的最快方法是什么?

zend-framework - 您使用什么搜索表单?获取查询或路由参数?

search - 更改 SOLR 默认连接

java - Lucene 4.10 日期范围查询 API

lucene - 在没有针对内容所用语言的分析器的情况下实现 Lucene?

elasticsearch - query_string 和 multi_match 有什么区别?

elasticsearch - Elasticsearch 无法找到SqlServer Jdbc驱动程序

elasticsearch - 如何在Elasticsearch中获取每个文档的重要术语聚合?

search - 如何在Vertica中搜索column_names?

search - 二分搜索 - 最坏/平均情况