首页 > 解决方案 > Elasticsearch 文档字段中的所有术语都应与查询匹配

问题描述

我在弹性索引中有一个包含多个单词的字段。IE

field : "nice house"

我只想查找文档,如果用户在他的查询字符串中包含该字段的所有单词,则查询字符串可能包含不在该字段中的其他单词,即

nice (should not match)
nice room (should not match)
nice house (should match)
nice house bro (should match)

minimum_should match or AND don't help here任何提示如何解决这个问题

标签: elasticsearch

解决方案


没有直接的方法可以实现您的用例。但是您可以使用渗透查询来实现您的用例

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

索引映射:

{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "query": {
        "type": "percolator"
      }
    }
  }
}

指数数据:

{
  "query": {
    "match": {
      "name": {
        "query": "nice house",
        "operator": "AND"
      }
    }
  }
}

搜索查询:

不错(不应该匹配)

{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "name": "nice"
      }
    }
  }
}

搜索结果将是:

"hits": []

好房子兄弟(应该匹配)

{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "name": "nice house bro"
      }
    }
  }
}

搜索结果将是

 "hits": [
      {
        "_index": "67433387",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.26152915,
        "_source": {
          "query": {
            "match": {
              "name": {
                "query": "nice house",
                "operator": "AND"
              }
            }
          }
        },
        "fields": {
          "_percolator_document_slot": [
            0
          ]
        }
      }
    ]

推荐阅读