【问题标题】:Misspelling suggestion ("did you mean") with phrase suggest and whitespace correction with Elasticsearch带有短语建议的拼写错误建议(“您的意思是”)和 Elasticsearch 的空格校正
【发布时间】:2020-09-14 16:19:41
【问题描述】:

我使用默认分析器“英语”来搜索文档,它非常好。 但是,当搜索查询拼写错误或按此类拼写错误的短语进行搜索时,我还需要“您的意思是”结果。

我需要什么分析器/过滤器/查询来实现这样的行为?

原文

Elasticsearch is a distributed, open source search and analytics engine for all types of data,
including textual, numerical, geospatial, structured, and unstructured. Elasticsearch is built
on Apache Lucene and was first released in 2010 by Elasticsearch N.V. (now known as Elastic).
Known for its simple REST APIs, distributed nature, speed, and scalability, Elasticsearch is
the central component of the Elastic Stack, a set of open source tools for data ingestion,
enrichment, storage, analysis, and visualization. Commonly referred to as the ELK Stack 
(after Elasticsearch, Logstash, and Kibana), the Elastic Stack now includes a rich collection
of lightweight shipping agents known as Beats for sending data to Elasticsearch.

搜索字词

搜索查询 => 你的意思是 XXX 吗?

遗漏的字母或类似的东西
Elastisearch => Elastic搜索
distribated => 分布式
亚太地区je => Apache

额外空间
弹性搜索 => 弹性搜索

没有空格
开源 => 开源

拼写错误的短语
serach engne => 搜索引擎

【问题讨论】:

  • 不错的问题+1,但如果您能解释更多关于您的用例,那就太好了,有些很容易在一个查询中实现,有些则不是,所以您可能必须使用多个 ES 查询才能获得预期结果
  • 一般用例只有一个:为用户提供一些纠正拼写错误的方法,但正如我所描述的,拼写错误可能相当复杂。可以为不同的变体使用一些索引/查询,因为我可以在之后组合结果。我已经添加了另一个案例来质疑 - 信中的错误。我需要开始:一些指向此案例的文档/示例的指针。

标签: elasticsearch nest elasticsearch-analyzers


【解决方案1】:

您的第一个丢失字母或其他内容的示例可以使用fuzzy query 来实现,第二个使用使用ngramedge-ngram tokenizer 的自定义分析器来实现,有关示例,请参阅my blog on autocomplete

在示例文档中添加模糊查询示例

索引映射

{
    "mappings": {
        "properties": {
            "title": {
                "type": "text"
    
            }
        }
    }
}

索引您的示例文档并使用以下搜索查询

{
    "query": {
        "fuzzy": {
            "title": {
                "value": "distributed"
            }
        }
    }
}

并搜索资源

 "hits": [
            {
                "_index": "didyou",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.89166296,
                "_source": {
                    "title": "distribated"
                }
            }
        ]

对于Elasticsearch

{
    "query": {
        "fuzzy": {
            "title": {
                "value": "Elasticsearch"
            }
        }
    }
}

并搜索结果

  "hits": [
            {
                "_index": "didyou",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.8173577,
                "_source": {
                    "title": "Elastisearch"
                }
            }
        ]

【讨论】:

  • 我在索引和许多字段中有整个文本,所以模糊可以显示结果但不能显示单词/短语的建议:(
  • @webness 请参考此链接elastic.co/guide/en/elasticsearch/reference/current/… 获取弹性搜索建议
  • @webness 好运吗?
  • 对于短语建议/空格 - 否 :( 但我实施了解决方法:在将查询传递给 Elasticsearch 之前按单词拆分,然后迭代数组并分别搜索每个单词。
  • @webness 这似乎是一个很好的解决方法,我自己实现了几个查询,我无法从盒子里得到一些东西,很高兴你能够解决这个问题,如果你可以投票(我猜你已经投票了)并接受答案:)
猜你喜欢
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 1970-01-01
  • 2018-04-10
相关资源
最近更新 更多