Difference between `terms` and `term` query for a single term?

Is there any significant implementation difference between terms and term query when there is a single term to search?

In a situation where I know the number of terms in advance, is it worth it to implement them separately?
Ie:

 if tokens > 1 
     buildTermsQuery
else
   buildTermQuery

Does a terms query with a single term have any penalty?

Based on what I was able to gather from the query profiler, there seems to be a difference as the Terms query uses a MultiTermQueryConstantScoreBlendedWrapper while the Term query uses TermQuery directly. I just wonder if it ultimately matters

Hi @yeikel

"Giving my two cents", I believe that the performance impact of using terms for a single term would be negligible, even though terms requires the term to go through the list processing flow, as per its default operation. What is really worth paying attention to is correctly mapping the field, ensuring that it is suitable for term-level queries, such as term and terms. Proper mapping, such as using keyword fields, is essential for the proper functioning of these queries.

1 Like

Thanks for the input. That's understandable. We are using keyword already :slight_smile:

I am just curious as to why both exist because in theory they could just implement Term using Terms with a single term?

It's a convenience function ...instead of bool should term

2 Likes

It's a convenience function ...instead of bool should term

MultiTermQueryConstantScoreBlendedWrapper seems to be doing quite a bit of work, that's where my main question started

From the docs

The terms query is the same as the term query, except you can search for multiple values. A document will match if it contains at least one of the terms. To search for documents that contain more than one matching term, use the terms_set query.

1 Like