# Exact phrase matching in any word order, but restricts words not in query

**URL:** <https://discuss.elastic.co/t/exact-phrase-matching-in-any-word-order-but-restricts-words-not-in-query/12825>\
**Category:** Elasticsearch\
**Created:** [July 17, 2013, 12:47pm UTC](https://discuss.elastic.co/t/exact-phrase-matching-in-any-word-order-but-restricts-words-not-in-query/12825 "2013-07-17T12:47:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![waysidekoi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/waysidekoi/32/2225_2.png) [@waysidekoi](https://discuss.elastic.co/u/waysidekoi)\
**Post date:** [July 17, 2013, 12:47pm UTC](https://discuss.elastic.co/t/exact-phrase-matching-in-any-word-order-but-restricts-words-not-in-query/12825/1 "2013-07-17T12:47:25Z")

</div>

I would like to query for 'apple pie' and return the result 'apple pie' as  
well as 'pie apple', but I want to restrict any results such as 'apple pie  
crumb cake' or 'brown apple pie'. Is there some kind of: 'result must  
match query exactly' or some way to use the score as an indicator for a  
perfect/exact match?

Default operator 'and' is perfect, but it also retrieves results that do  
not match exactly.

'not\_analyze' or tokenizing the fields as 'keyword' doesn't allow me to  
query for the item in reverse, ie: 'apple pie' works, but 'pie apple' does  
not.

Is the solution to tokenize the input such that each permutation of the  
string is indexed as a keyword? ie- 'apple pie crumb cake' as:  
'apple cake pie crumb'  
'apple cake crumb pie'  
etc.

or is there a way to filter queries such that they only return results that  
match every word in the query and rejects results that contain excess words  
not found in the original query? ie - 'exact\_match': 'true'

Wayne

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [July 18, 2013, 3:58pm UTC](https://discuss.elastic.co/t/exact-phrase-matching-in-any-word-order-but-restricts-words-not-in-query/12825/2 "2013-07-18T15:58:04Z")

</div>

Wayne,

This is why I use the Java API and not the REST API. Using Java, I can more  
easily pre-process the search to construct the proper query, and then I can  
much more easily pre-process the responses to handle cases such as yours,  
or for a very smooth and nice multi-field hierarchical facet response  
(which is not in ES currently, but my add-on isn't "hacky at all!).

In your case, you could analyze your search term. Then, for each search hit  
you could analyze the response field. If the search hit doesn't analyze to  
the same number of terms that the search had, you would throw away  
("filter") that match. That would give you an exact match as you describe.

Good luck!  
Brian

On Wednesday, July 17, 2013 8:47:25 AM UTC-4, Wayne Yang wrote:

> I would like to query for 'apple pie' and return the result 'apple pie' as  
> well as 'pie apple', but I want to restrict any results such as 'apple pie  
> crumb cake' or 'brown apple pie'. Is there some kind of: 'result must  
> match query exactly' or some way to use the score as an indicator for a  
> perfect/exact match?
> 
> Default operator 'and' is perfect, but it also retrieves results that do  
> not match exactly.
> 
> 'not\_analyze' or tokenizing the fields as 'keyword' doesn't allow me to  
> query for the item in reverse, ie: 'apple pie' works, but 'pie apple' does  
> not.
> 
> Is the solution to tokenize the input such that each permutation of the  
> string is indexed as a keyword? ie- 'apple pie crumb cake' as:  
> 'apple cake pie crumb'  
> 'apple cake crumb pie'  
> etc.
> 
> or is there a way to filter queries such that they only return results  
> that match every word in the query and rejects results that contain excess  
> words not found in the original query? ie - 'exact\_match': 'true'
> 
> Wayne

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![roytmana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roytmana/32/44855_2.png) [@roytmana](https://discuss.elastic.co/u/roytmana)\
**Post date:** [July 19, 2013, 12:35am UTC](https://discuss.elastic.co/t/exact-phrase-matching-in-any-word-order-but-restricts-words-not-in-query/12825/3 "2013-07-19T00:35:23Z")

</div>

One solution would be to calculate number of words in your document and store it as a field. Then count number of qords in your query and add count equality to your query.

To be consistent you would need to use analyzer to count number of terms in your document and store it and the same analyzer in your query and to count terms in your phrase

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:25am UTC](https://discuss.elastic.co/t/exact-phrase-matching-in-any-word-order-but-restricts-words-not-in-query/12825/4 "2017-07-06T02:25:34Z")

</div>


