为什么模糊查询 returns 匹配但模糊查询不在同一输入上?
Why fuzzy query returns a match but query with fuzziness doesn't on the same input?
我在 Elasticsearch 中创建了以下索引:
PUT /my-index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "whitespace",
"filter": ["lowercase", "3_5_edgegrams"]
}
},
"filter": {
"3_5_edgegrams": {
"type": "edge_ngram",
"min_gram": 3,
"max_gram": 10
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
然后我插入了以下文件:
{
"name": "Nuvus Gro Corp"
}
当我进行以下查询时(我们称之为 fuzzy_query
):
GET /my-index/_search
{
"query": {
"fuzzy": {
"name": {
"value": "qnuv"
}
}
}
}
我找到了上述文件的匹配项。
当我进行查询时(我们将查询称为 match_with_fuzziness
):
GET /my-index/_search
{
"query": {
"match": {
"name": {
"query": "qnuv",
"fuzziness": "AUTO"
}
}
}
}
我没有匹配到。如果我进行以下查询:
GET /my-index/_search
{
"query": {
"match": {
"name": {
"query": "nuvq",
"fuzziness": "AUTO"
}
}
}
}
我又匹配到了。我不明白为什么当我进行 match_with_fuzziness
查询时,我没有得到任何匹配项。
编辑: 我用 Kibana Profiler 分析了查询,根据分析器 match_with_fuzziness
是一个 SynonymQuery Synonym(name:qnu name:qnuv)
查询,而 fuzzy_query
是一个BoostQuery (name:nuv)^0.6666666
与 中解释的问题非常相似。
问题是您没有指定具体的 search_analyzer
,所以在搜索时 qnuv
和 nuvq
也会被 my_analyzer
和 edge-ngramed 分析以及,因此您收到的比赛。
如果我们检查第一个查询,因为您使用的是 fuzzy
查询,qnuv
(搜索词)将匹配 nuv
(第一个索引的 edge-ngramed 标记) 距离为 1(即第一个 q
是“容忍的”),这是 fuzzy
查询默认执行的操作(使用 "fuzziness: AUTO")
在第三个查询中,nuv
(搜索词的第一个 edge-ngramed 标记)将匹配 nuv
(第一个索引的 edge-ngramed 标记)。
第二个查询的情况有点特殊,下面我将参考 fuzziness
parameter works in the context of match
queries
Fuzzy matching is not applied to terms with synonyms or in cases where the analysis process produces multiple tokens at the same position. Under the hood these terms are expanded to a special synonym query that blends term frequencies, which does not support fuzzy expansion.
粗体部分适用于您的情况。由于搜索词qnuv
被my_analyzer
分析,所以在同一位置产生qnu
和qnuv
两个token,不支持模糊匹配。
您需要改为将映射更改为该映射,它将按您期望的方式工作,即所有三个查询都将 return 您的文档:
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer",
"search_analyzer": "standard" <---- add this line
}
}
}
我在 Elasticsearch 中创建了以下索引:
PUT /my-index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "whitespace",
"filter": ["lowercase", "3_5_edgegrams"]
}
},
"filter": {
"3_5_edgegrams": {
"type": "edge_ngram",
"min_gram": 3,
"max_gram": 10
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
然后我插入了以下文件:
{
"name": "Nuvus Gro Corp"
}
当我进行以下查询时(我们称之为 fuzzy_query
):
GET /my-index/_search
{
"query": {
"fuzzy": {
"name": {
"value": "qnuv"
}
}
}
}
我找到了上述文件的匹配项。
当我进行查询时(我们将查询称为 match_with_fuzziness
):
GET /my-index/_search
{
"query": {
"match": {
"name": {
"query": "qnuv",
"fuzziness": "AUTO"
}
}
}
}
我没有匹配到。如果我进行以下查询:
GET /my-index/_search
{
"query": {
"match": {
"name": {
"query": "nuvq",
"fuzziness": "AUTO"
}
}
}
}
我又匹配到了。我不明白为什么当我进行 match_with_fuzziness
查询时,我没有得到任何匹配项。
编辑: 我用 Kibana Profiler 分析了查询,根据分析器 match_with_fuzziness
是一个 SynonymQuery Synonym(name:qnu name:qnuv)
查询,而 fuzzy_query
是一个BoostQuery (name:nuv)^0.6666666
与
问题是您没有指定具体的 search_analyzer
,所以在搜索时 qnuv
和 nuvq
也会被 my_analyzer
和 edge-ngramed 分析以及,因此您收到的比赛。
如果我们检查第一个查询,因为您使用的是 fuzzy
查询,qnuv
(搜索词)将匹配 nuv
(第一个索引的 edge-ngramed 标记) 距离为 1(即第一个 q
是“容忍的”),这是 fuzzy
查询默认执行的操作(使用 "fuzziness: AUTO")
在第三个查询中,nuv
(搜索词的第一个 edge-ngramed 标记)将匹配 nuv
(第一个索引的 edge-ngramed 标记)。
第二个查询的情况有点特殊,下面我将参考 fuzziness
parameter works in the context of match
queries
Fuzzy matching is not applied to terms with synonyms or in cases where the analysis process produces multiple tokens at the same position. Under the hood these terms are expanded to a special synonym query that blends term frequencies, which does not support fuzzy expansion.
粗体部分适用于您的情况。由于搜索词qnuv
被my_analyzer
分析,所以在同一位置产生qnu
和qnuv
两个token,不支持模糊匹配。
您需要改为将映射更改为该映射,它将按您期望的方式工作,即所有三个查询都将 return 您的文档:
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer",
"search_analyzer": "standard" <---- add this line
}
}
}