# Logic for selecting terms w max\_expansions and phrase\_prefix query

**URL:** <https://discuss.elastic.co/t/logic-for-selecting-terms-w-max-expansions-and-phrase-prefix-query/53785>\
**Category:** Elasticsearch\
**Created:** [June 23, 2016, 2:18pm UTC](https://discuss.elastic.co/t/logic-for-selecting-terms-w-max-expansions-and-phrase-prefix-query/53785 "2016-06-23T14:18:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marinoc](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@marinoc](https://discuss.elastic.co/u/marinoc)\
**Post date:** [June 23, 2016, 2:18pm UTC](https://discuss.elastic.co/t/logic-for-selecting-terms-w-max-expansions-and-phrase-prefix-query/53785/1 "2016-06-23T14:18:50Z")

</div>

Is there a certain logic as to how max\_expansions works in a phrase\_prefix search? If we run this search multiple times, we get a different number of results.

If I look at the query sent to Lucene, I can see that the expansions contain different sets of terms each time I run the query. Does this seem right? Are the expansion terms chosen randomly?

We're using the following query w phrase\_prefix:

```
> {  
> "query":{  
> "bool":{  
> "must":[  
> {  
> "bool":{  
> "should":[  
> {  
> "match":{  
> "field1":{  
> "query":"justin",
> "type":"phrase_prefix",
> "max_expansions":250
> }
> }
> },
> {  
> "terms":{  
> "field1":[  
> "justin"
> ]
> }
> }
> ],
> "minimum_should_match":"1"
> }
> },
> {  
> "bool":{  
> "should":[  
> {  
> "match":{  
> "field2":{  
> "query":"900",
> "type":"phrase_prefix",
> "max_expansions":250
> }
> }
> },
> {  
> "match":{  
> "field3":{  
> "query":"900",
> "type":"phrase_prefix",
> "max_expansions":250
> }
> }
> }
> ],
> "minimum_should_match":"1"
> }
> },
> {  
> "bool":{  
> "should":[  
> {  
> "match":{  
> "field4":{  
> "query":"whirlpool",
> "type":"phrase_prefix",
> "max_expansions":250
> }
> }
> },
> {  
> "terms":{  
> "field5":[  
> "12012096",
> "12319482",
> "12243232",
> "12297888"
> ]
> }
> }
> ],
> "minimum_should_match":"1"
> }
> }
> ],
> "filter":{  
> "term":{  
> "field6":"F"
> }
> }
> }
> }
> }
```

---

<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:** [July 5, 2017, 10:41pm UTC](https://discuss.elastic.co/t/logic-for-selecting-terms-w-max-expansions-and-phrase-prefix-query/53785/2 "2017-07-05T22:41:04Z")

</div>


