Elasticsearch 中匹配与术语查询的性能?
我在项目中使用了大量 match
查询。现在,我刚刚在 Elasticsearch 中遇到了 term
查询。如果指定了查询的关键字,则术语查询似乎会更快。 现在我有一个问题.. 我应该重构我的代码(很多)并使用 term 而不是 match 吗? 使用 term 的性能比 match 好多少?
在我的查询中使用术语:
main_query["query"]["bool"]["must"].append({"term":{object[..]:object[...]}})
在我的查询中使用匹配查询:
main_query["query"]["bool"]["must"].append({"match":{object[..]:object[...]}})
I've been using a lot of match
queries in my project. Now, I have just faced with term
query in Elasticsearch. It seems the term query is more faster in case that keyword of your query is specified.
Now I have a question there..
Should I refactor my codes (it's a lot) and use term instead of match?
How much is the performance of using term better than match?
using term in my query:
main_query["query"]["bool"]["must"].append({"term":{object[..]:object[...]}})
using match query in my query:
main_query["query"]["bool"]["must"].append({"match":{object[..]:object[...]}})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Elastic 不鼓励对 明显的原因(分析!!),但是如果你知道你需要查询一个
关键字
字段(不是分析!!),绝对使用term/terms
查询而不是match
,因为match
查询除了分析无论如何,最终都会执行term
查询,因为它注意到查询的字段是keyword
字段。Elastic discourages to use
term
queries fortext
fields for obvious reasons (analysis!!), but if you know you need to query akeyword
field (not analyzed!!), definitely go forterm/terms
queries instead ofmatch
, because thematch
query does a lot more things aside from analyzing the input and will eventually end up executing aterm
query anyway because it notices that the queried field is akeyword
field.据我所知,当您使用匹配查询时,这意味着您的字段被映射为“文本”并且您使用分析器。这样,您的索引词将生成标记,当您运行查询时,您将通过分析器,并且将为每个标记建立对应关系。
Term会做精确匹配,也就是说,它不会经过任何分析器,它会在倒排索引中寻找精确的term。
正因为如此,我相信通过不通过分析器,Term 会更快。
我使用术语匹配来搜索关键字,例如类别、标签,以及使用分析器没有意义的东西。
As far as I know when you use the match query it means your field is mapped as "text" and you use an analyzer. With that, your indexed word will generate tokens and when you run the query you go through an analyzer and the correspondence will be made for each of them.
Term will do the exact match, that is, it does not go through any analyzer, it will look for the exact term in the inverted index.
Because of this I believe that by not going through analyzers, Term is faster.
I use Term match to search for keywords like categories, tag, things that don't make sense use an analyzer.