【问题标题】:Highlight with fuzziness and ngram用模糊和 ngram 突出显示
【发布时间】:2016-02-20 19:17:26
【问题描述】:

我猜这个话题的标题已经把你宠坏了:D

我使用 edge_ngram 和 highlight 来构建自动完成搜索。我在查询中添加了模糊性,以允许用户拼错他们的搜索,但它破坏了一点亮点。

当我写 Sport 这就是我得到的:

<em>Spor</em>t
<em>Spor</em>t mécanique
<em>Spor</em>t nautique

我猜是因为它与 ngram 分词器生成的令牌 spor 匹配。

查询:

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "name": {
                            "query": "sport",
                            "operator": "and",
                            "fuzziness": "AUTO"
                        }
                    }
                },
                {
                    "match_phrase_prefix": {
                        "name.raw": {
                            "query": "sport"
                        }
                    }
                }
            ]
        }   
    },
    "highlight": {
        "fields": {
            "name": {
              "term_vector": "with_positions_offsets"
            }
        }
    }
}

还有映射:

{
    "settings": {
        "analysis": {
            "analyzer": {
                "partialAnalyzer": {
                    "type": "custom",
                    "tokenizer": "ngram_tokenizer",
                    "filter": ["asciifolding", "lowercase"]
                },
                "keywordAnalyzer": {
                    "type": "custom",
                    "tokenizer": "keyword",
                    "filter": ["asciifolding", "lowercase"]
                },
                "searchAnalyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": ["asciifolding", "lowercase"]
                }
            },

            "tokenizer": {
                "ngram_tokenizer": {
                    "type": "edge_ngram",
                    "min_gram": "1",
                    "max_gram": "15",
                    "token_chars": [ "letter", "digit" ]
                }
            }
        }
    },

    "mappings": {
        "place": {
            "properties": {
                "name": {
                    "type": "string",
                    "index_analyzer": "partialAnalyzer",
                    "search_analyzer": "searchAnalyzer",
                    "term_vector": "with_positions_offsets",
                    "fields": {
                        "raw": {
                            "type": "string",
                            "analyzer": "keywordAnalyzer"
                        }
                    }
                }
            }
        }
    }
}

我尝试在查询中添加一个没有模糊性的新匹配子句,以尝试匹配关键字before模糊匹配,但它没有任何改变。

'match': {
   'name': {
   'query': 'sport',
   'operator': 'and'
}

知道我该如何处理吗?

问候,拉斐尔

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我猜你可以用 highlight_query 做到这一点

    在你的高亮查询中试试这个。

    "highlight": {
        "fields": {
          "name": {
            "term_vector": "with_positions_offsets",
            "highlight_query": {
              "match": {
                "name.raw": {
                    "query": "spotr",
                    "fuzziness": 2
                }
              }
            }
          }
        }
      }
    

    希望对你有帮助。

    【讨论】:

    • 您好,我试过了,它适用于“拼写正确”的单词,但如果我写了一个拼写错误的单词(例如:spotr 而不是 sport),则不会再突出显示任何内容。跨度>
    • 我添加了fuzziness,共2个,您能再检查一下吗?
    • 与最初的问题相同的问题具有模糊性(它只突出显示“spor”):(
    • sport 是 5 个字母的单词,max edit distance 在 ES 中默认是 5 个字母的单词,因此 spotr 不能转换为 sport 因为它需要两次操作,因此编辑距离为 2。有关详细信息,请参阅 fuzziness 部分
    • 我在我的机器上试了一下,它成功了。我插入了运动并使用“spotr”运行模糊查询,它突出显示了运动。
    猜你喜欢
    • 1970-01-01
    • 2011-12-30
    • 2017-08-14
    • 2015-09-27
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多