除了一般的查询字符串外,我如何在ElasticSearch中进行前缀搜索? [英] How can I do a prefix search in ElasticSearch in addition to a generic query string?

查看:236
本文介绍了除了一般的查询字符串外,我如何在ElasticSearch中进行前缀搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的用户索引,单个类型的用户具有多个字段。我不需要在索引上定义任何东西。

I have a very basic index of "users" with a single type "user" that has several fields to it. I don't have anything defined on the index besides that.

我需要做的是提供自动完成结果,优先排序前缀匹配(用户名),但也包含其他匹配来自用户的生物和网站以及其他字段的子串匹配。

What I need to do is provide autocomplete results that prioritize prefix matches (for usernames) but also contain other matches from the users bio and website and substring matches of other fields.

如何使用查询DSL完成此任务?

How does one accomplish this with the query DSL?

推荐答案

有不同的方式来实现你想要的。我会说这取决于你想要做前缀匹配的方式。您可以使用前缀查询或使 EdgeNGrams 在用户字段中搜索,没有需要一个前缀查询。第一个选项有点慢,而第二个选项导致您的索引大小增加,因为您会索引更多的条款(ngrams)。

There are different ways to achieve what you want. I'd say it depends on the way you want to make prefix matches. You can use a Prefix Query or make EdgeNGrams out of the user field and search on it without the need of a prefix query. The first option is a little bit slower, while the second one causes an increasement of your index size since you'd index more terms (the ngrams).

如果您决定对于前缀查询,您需要将不同的查询组合在一起。您可以使用布尔查询来执行此操作。您只需要确定哪些查询必须匹配,哪些不匹配,哪些不匹配(如果它们是可选的)。您还可以提升每个查询,以表示前缀匹配比较重要。

If you decide for the prefix query you need to combine different queries together. You can do that using the bool query. You just need to decide which queries must match, which ones must not match and which ones should match (if they are optional). You can also give a boost to each query in order to express that prefix matches are more important for example.

另一方面,如果您决定索引EdgeNGrams,您可以使用单个查询字符串并搜索不同的如下所示:

On the other hand, if you decide to index EdgeNGrams you can use a single query string and search on different fields giving a different weight to them, like this:

{
    "query" : {
        "query_string" : {
            "fields" : "user.ngrams^3 field1^2 field2",
            "query" : "query"
        }
    }
}

您还需要考虑到查询字符串允许您搜索多个术语(布尔值查询生成),并使用 lucene查询语法。此外,当前缀查询不是时,分析查询字符串。这一切都取决于你需要什么,以及这些功能是否对你的用户有用。

You also need to take into account that the query string allows you to search for multiple terms (a boolean query is generated out of them) and to use the lucene query syntax. Also, the query string is analyzed while the prefix query is not. It all depends on what you need and whether those features are useful for your usecase.

如果您需要更多信息,请通知我。

Let me know if you need more information.

这篇关于除了一般的查询字符串外,我如何在ElasticSearch中进行前缀搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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