匹配一个字段中的所有部分单词

Match all partial words in one field

我有这个查询:

{
    "query": {
        "match": {
            "tag": {
                "query": "john smith",
                "operator": "and"
            }
        }
    }
}

使用 and 运算符我解决了 return 文档,其中单词 "john" 和 "smith" 必须以任何位置和顺序出现在标记字段中。但我需要 return 文档,其中所有 部分 单词必须出现在标签字段中,例如 "joh" 和 "smit"。我试试这个:

{
    "query": {
        "match": {
            "tag": {
                "query": "*joh* *smit*",
                "operator": "and"
            }
        }
    }
}

但没有 return。我该如何解决?

您可以使用 edge_ngram tokenizer and boolean query with multiple must 子句(使用您的示例 2)来获得所需的输出。

工作示例:

索引定义

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram", --> note this
          "min_gram": 1,
          "max_gram": 10
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "autocomplete", 
        "search_analyzer": "standard" 
      }
    }
  }
}

索引两个示例文档,一个应该匹配,一个不应该匹配

{
   "title" : "john bravo" --> show;dn't match
}

{
   "title" : "john smith" --> should match
}

带有 must 子句的布尔搜索查询

{
    "query": {
        "bool": {
            "must": [ --> this means both `jon` and `smit` match clause must match, replacement of your `and` operator.
                {
                    "match": {
                        "title": "joh"
                    }
                },
                {
                    "match": {
                        "title": "smit"
                    }
                }
            ]
        }
    }
}

搜索结果

"hits": [
         {
            "_index": "so_partial",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.2840209,
            "_source": {
               "title": "john smith"
            }
         }
      ]