Elasticsearch 文档字段中的所有术语都应与查询匹配

时间:2021-05-07 10:38:03

标签: elasticsearch

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

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 任何提示如何解决这个问题

1 个答案:

答案 0 :(得分:0)

没有直接的方法来实现您的用例。但是您可以使用 percolate query 来实现您的用例

添加一个包含索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

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

索引数据:

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

搜索查询:

<块引用>

nice(不应该匹配)

{
  "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
          ]
        }
      }
    ]