Elasticsearch 倒排搜索查询

Elasticsearch inverted search query

我想使用 elasticsearch 在我的搜索查询中必须包含的数据中进行搜索。

示例:

{
  "query":{
     "match":{       
       "field":"some data to be searched"
      }
   }
}

应 return 编辑以下字段,因为查询数据同时包含 somedata

{
 "field":"some data"
}

但此搜索查询不应 return 任何内容:

{
  "query":{
     "match":{ 
        "field":"data to be searched"
      }
   }
}

我该怎么做?

您可以使用 Percolate query 来实现您的用例。

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

索引映射:

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

索引数据:

{
  "query": {
    "match": {
      "field": {
        "query": "some data",
        "operator": "AND"
      }
    }
  }
}

搜索查询:

{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "field": "data to be searched"
      }
    }
  }
}

搜索结果:

"hits": []

搜索查询:

{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "field": "some data to be searched"
      }
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "67658971",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.26152915,
        "_source": {
          "query": {
            "match": {
              "field": {
                "query": "some data",
                "operator": "AND"
              }
            }
          }
        },
        "fields": {
          "_percolator_document_slot": [
            0
          ]
        }
      }
    ]