Home > OS >  Performance of match vs term query in elasticsearch?
Performance of match vs term query in elasticsearch?

Time:04-13

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[...]}})

CodePudding user response:

Elastic discourages to use term queries for text fields for obvious reasons (analysis!!), but if you know you need to query a keyword field (not analyzed!!), definitely go for term/terms queries instead of match, because the match query does a lot more things aside from analyzing the input and will eventually end up executing a term query anyway because it notices that the queried field is a keyword field.

CodePudding user response:

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.

  • Related