# Match a phrase keep in order, but allow slop requires use of Spans right?

**URL:** <https://discuss.elastic.co/t/match-a-phrase-keep-in-order-but-allow-slop-requires-use-of-spans-right/10102>\
**Category:** Elasticsearch\
**Created:** [December 18, 2012, 1:36am UTC](https://discuss.elastic.co/t/match-a-phrase-keep-in-order-but-allow-slop-requires-use-of-spans-right/10102 "2012-12-18T01:36:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [December 18, 2012, 1:36am UTC](https://discuss.elastic.co/t/match-a-phrase-keep-in-order-but-allow-slop-requires-use-of-spans-right/10102/1 "2012-12-18T01:36:41Z")

</div>

I wanted to check my understanding of phrase queries vs. span queries.  
My problem is that given the simple terms the user enters, I want to  
check for phrases made from those terms with some slop, BUT MAINTAIN THE  
ORDER OF THE TERMS.  
Is my understanding correct, I can NOT use match\_phrase, because once I  
start playing with a slop, I may end up scoring an out of order phrase.  
Is the only choice in my case a span\_near?  
Another question: Are my chains of span\_nears (see below) the only way  
to get this kind of scoring?

I want what can be summarized as  
terms are good, in fact they are required;  
a 2-word phrase is better;  
longer phrases even better;  
Just all the terms only beat even a short 2-word phrase when there is  
_really_ a lot of them. Short phrases beat longer phrases similarly -  
only when there are really a lot of them.

If the words are: aaa bbb ccc  
I might generate a LUCENE query like

+aaa +bbb +ccc "aaa bbb"^100~6 "aaa bbb ccc"^10000~6

"aaa 1 2 3 4 5 6 7 8 9 10 11 ... 110 111 112 bbb" is a matching  
document, but  
"aaa 1 2 3 4 5 bbb" is better, but  
"aaa 1 2 3 bbb 1 2 3 4 5 ccc" is lots (i.e. 100x!) better (each term  
within 6 of its neighbor).

When using Lucene (I thought) I found that phrase queries with slop do  
not limit themselves to preserving the order.  
The Lucene PhraseQuery spells this out in the discussion of slop.  
[https://lucene.apache.org/core/3\_6\_1/api/core/org/apache/lucene/search/PhraseQuery.html#setSlop(int)](https://lucene.apache.org/core/3_6_1/api/core/org/apache/lucene/search/PhraseQuery.html#setSlop%28int%29)

So I switched to SpanNearQuery, one for each phrase - a 2-term  
span\_near, a 3-term span\_near, etc.  
It has all of what I need preserve order, specify a slop, and a boost.

When I ported to ES I used the same span near query.  
[http://www.elasticsearch.org/guide/reference/query-dsl/span-near-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/span-near-query.html)

Am I missing something, can I use match\_phrase or some other query in ES  
to get what I want?  
Is my manual application of phrase boost really necessary?

From  
[http://www.elasticsearch.org/guide/reference/query-dsl/match-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/match-query.html)  
I see slop and order are only addressed by the sentence  
"A phrase query maintains order of the terms up to a configurable |slop|  
(which defaults to 0)."

That sounds like it will allow out of order which matches my  
understanding of Lucene PhraseQuery.

Suggestions or comments about the use of chains of span\_nears would be  
appreciated.

-Paul

--

---

<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:59am UTC](https://discuss.elastic.co/t/match-a-phrase-keep-in-order-but-allow-slop-requires-use-of-spans-right/10102/2 "2017-07-06T02:59:32Z")

</div>


