# How exactly does max\_query\_terms works in a MLT query

**URL:** <https://discuss.elastic.co/t/how-exactly-does-max-query-terms-works-in-a-mlt-query/233098>\
**Category:** Elasticsearch\
**Created:** [May 18, 2020, 11:47am UTC](https://discuss.elastic.co/t/how-exactly-does-max-query-terms-works-in-a-mlt-query/233098 "2020-05-18T11:47:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lok63](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lok63/32/64812_2.png) [@lok63](https://discuss.elastic.co/u/lok63)\
**Post date:** [May 18, 2020, 11:47am UTC](https://discuss.elastic.co/t/how-exactly-does-max-query-terms-works-in-a-mlt-query/233098/1 "2020-05-18T11:47:39Z")

</div>

As the official documentation states, `max_query_terms` is the MAX number of query terms that will be selected. I would like to know how this works.

For example if I set `max_query_terms = 6`, then using the explain parameter, I can see that for some results, only 2 or 3 words were used to calculate the BM25 score. I really want to understand how this works and when ES decides to use only 2 words or the max number I defined?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 18, 2020, 4:54pm UTC](https://discuss.elastic.co/t/how-exactly-does-max-query-terms-works-in-a-mlt-query/233098/2 "2020-05-18T16:54:59Z")

</div>

If you'd like to poke around the code, that limit is used here: [https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/common/lucene/search/XMoreLikeThis.java#L728](https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/common/lucene/search/XMoreLikeThis.java#L728)

My understanding is that the query will look up the term frequencies for a particular field, then construct a priority queue that is sized to either `max_query_terms` or the number of terms in the field, whichever is smaller. The priority queue determines which terms are added to the final boolean query that MLT creates.

So if a field only has two different values across all the docs (on the shard), the queue will be sized to 2 rather than `max_query_terms`. Fields with higher cardinality will bump into the limit instead and so the priority queue will be limited in size.

\*Caveat: not an expert at MLT, so grain of salt 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 15, 2020, 4:55pm UTC](https://discuss.elastic.co/t/how-exactly-does-max-query-terms-works-in-a-mlt-query/233098/3 "2020-06-15T16:55:01Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
