我想搜索该字段仅存在于 elasticsearch 中的搜索词的文档

i want to search for documents that field exists only search term in elasticsearch

我的部分文档映射如下:

"character_cut": {
  "type": "keyword"
}

样本数据在这里。

doc1 character_cut:[“约翰”]

doc2 character_cut:[“约翰”,“史密斯”]

doc3 character_cut:[“史密斯”、“杰西卡”、“安娜”]

doc4 character_cut:[“约翰”]

如果我找到“John”,将检索 doc1doc2doc4

如何使用“John”查询仅检索 doc1doc4

有两种方法。

1. Token_count

A field of type token_count is really an integer field which accepts string values, analyzes them, then indexes the number of tokens in the string.

PUT index-name
{
  "mappings": {
    "properties": {
      "character_cut":{
        "type": "text",
        "fields": {
          "keyword":{
            "type":"keyword"
          },
          "length":{
            "type":"token_count", ---> no of keyword tokens
            "analyzer":"keyword"
          }
        }
      }
    }
  }
}

查询

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "character_cut.keyword": {
              "value": "John"
            }
          }
        },
        {
         "term": {
           "character_cut.length": {
             "value": 1    --> replace with no of  matches required
           }
         }
        }
      ]
    }
  }
}

2。使用 script query

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "character_cut.keyword": {
              "value": "John"
            }
          }
        },
        {
         "script": {
           "script": "doc['character_cut.keyword'].size()==1"
                                   --> replace with no of  matches required
         }
        }
      ]
    }
  }
}

token_count 将在索引时间计算计数,因此它比将在 运行 时间

计算的脚本更快