Elasticsearch。如果查询没有空格,如何查找短语 [英] Elasticsearch. How to find phrases if query has no spaces

查看:111
本文介绍了Elasticsearch。如果查询没有空格,如何查找短语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我在名称字段中有一个短语Star wars的文档。
我想用DSL进行搜索并查询starwars并获取此文档。
我试图得到这样的东西

For example, I have a document with a phrase "Star wars" in the name field. I would like to make a search with DSL and query "starwars" and get this document. I am trying to get something like this

    GET _search
{ 
    "query" : { 
        "match_phrase" : {
            "name": {
            "query" : "starwars"
            }
        }

    } 
}

我如何用弹性搜索方式?

How can I do it with elasticsearch?

推荐答案

我想您需要使用包含同义词令牌过滤器与星球节的同义词。

I think you would need to update the analyzer on that name field with a custom analyzer that includes the synonym token filter with a synonym for starwars.

有关创建自定义分析器的文档应该帮助你此外,还应用了标准分析器默认情况下,如果您没有在映射中为该名称字段指定任何分析器。您可以使用自定义分析器,并将该同义词令牌过滤器添加到该过滤器阵列中。也许,更多的想法,你想如何分析您的其他要求的内容以及这一点。

The docs on creating a custom analyzer should help you out. Additionally, the standard analyzer is applied by default if you did not specify any analyzer for that name field in your mapping. You can base your custom analyzer on that and add that synonym token filter in that array of filters. Perhaps, give some more thought to how you want the content to be analyzed for the other requirements you have as well as this.

使用此分析仪更新,您应该能够使用该查询并获得您期望的结果。

With this analyzer update you should be able to use that query and get the result you expect.

示例:

{
    "filter" : {
        "my_synonym" : {
            "type" : "synonym",
            "synonyms" : [
                "star wars => starwars"
            ]
        }
    },
    "analyzer" : {
        "standard_with_synonym" : {
            "tokenizer" : "standard",
            "filter" : ["standard", "lowercase", "my_synonym", "stop"]
        }
    }
}

这篇关于Elasticsearch。如果查询没有空格,如何查找短语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆