# Search in the same phrase

**URL:** <https://discuss.elastic.co/t/search-in-the-same-phrase/9783>\
**Category:** Elasticsearch\
**Created:** [November 21, 2012, 7:22pm UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783 "2012-11-21T19:22:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loic\_Bertron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loic_bertron/32/1794_2.png) [@Loic\_Bertron](https://discuss.elastic.co/u/Loic_Bertron)\
**Post date:** [November 21, 2012, 7:22pm UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/1 "2012-11-21T19:22:05Z")

</div>

Hey,

I want to offert to my client the ability to look for two words in the same  
phrase, in the same field.  
Is there a way to do it easily in ElasticSearch ?

Thanks

Loïc

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [November 21, 2012, 7:41pm UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/2 "2012-11-21T19:41:57Z")

</div>

Hello Loïc,

There are a few options for phrase searches. The Lucene syntax supports  
phrases enclosed in double quotes:  
[http://lucene.apache.org/core/4\_0\_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#Terms](http://lucene.apache.org/core/4_0_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#Terms)

In Elasticsearch, the Match query supports phrases:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

A more powerful option (and more complex) are the various Span queries:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Cheers,

Ivan

On Wed, Nov 21, 2012 at 11:22 AM, Loïc Bertron [loic.bertron@gmail.com](mailto:loic.bertron@gmail.com)wrote:

> Hey,
> 
> I want to offert to my client the ability to look for two words in the  
> same phrase, in the same field.  
> Is there a way to do it easily in Elasticsearch ?
> 
> Thanks
> 
> Loïc
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Loic\_Bertron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loic_bertron/32/1794_2.png) [@Loic\_Bertron](https://discuss.elastic.co/u/Loic_Bertron)\
**Post date:** [November 21, 2012, 8:10pm UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/3 "2012-11-21T20:10:53Z")

</div>

Thanks for your answer.

I considered these options, but my case is specific. Span queries require  
slop parameter, which indicate distance between 2 terms. But sometimes, you  
could have a sentence with 3 words, or even a sentence with a lot more. So  
i can't really use this query. This is almost the same concept for phrase  
query with ES.  
My query would be more like: Term\_a and Term\_b between Beginning of field  
and "." char or between 2 "." char.

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 22, 2012, 3:31am UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/4 "2012-11-22T03:31:29Z")

</div>

Storing each sentence in a different instance of the field and using  
span\_near query is the only solution I can think of at the moment. Here is  
an example of how it can be used:

> <https://gist.github.com/imotov/5af5db1b08784ddf9724>

On Wednesday, November 21, 2012 3:10:53 PM UTC-5, Loïc Bertron wrote:

> Thanks for your answer.
> 
> I considered these options, but my case is specific. Span queries require  
> slop parameter, which indicate distance between 2 terms. But sometimes, you  
> could have a sentence with 3 words, or even a sentence with a lot more. So  
> i can't really use this query. This is almost the same concept for phrase  
> query with ES.  
> My query would be more like: Term\_a and Term\_b between Beginning of field  
> and "." char or between 2 "." char.

--

---

<div class="post-metadata">

**Author:** ![Loic\_Bertron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loic_bertron/32/1794_2.png) [@Loic\_Bertron](https://discuss.elastic.co/u/Loic_Bertron)\
**Post date:** [November 22, 2012, 2:59pm UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/5 "2012-11-22T14:59:49Z")

</div>

Thanks a lot Igor.  
Once again you're the man !

Just one quick question about your code : you're using position\_offset\_gap  
in your mapping. What is it for exactly ?

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 22, 2012, 3:21pm UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/6 "2012-11-22T15:21:03Z")

</div>

Excellent question! The position\_offset\_gap parameter is the most important  
part here, actually. It has to be bigger than expected number of terms in  
your longest sentence. It basically specifies the distance between the last  
word of one instance of a field and the first word of the next instance of  
that field. So, if one instance ends with "... lived a hobbit." and another  
starts with "Not a nasty, ..." the distance between the term "hobbit" and  
the term "not" will be whatever the position\_offset\_gap specifies. During  
search you should specify span smaller than position\_offset\_gap, this way  
span will not match words that are indexed in different instances of the  
field.

On Thursday, November 22, 2012 9:59:49 AM UTC-5, Loïc Bertron wrote:

> Thanks a lot Igor.  
> Once again you're the man !
> 
> Just one quick question about your code : you're using position\_offset\_gap  
> in your mapping. What is it for exactly ?

--

---

<div class="post-metadata">

**Author:** ![Loic\_Bertron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loic_bertron/32/1794_2.png) [@Loic\_Bertron](https://discuss.elastic.co/u/Loic_Bertron)\
**Post date:** [November 22, 2012, 4:08pm UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/7 "2012-11-22T16:08:39Z")

</div>

Nice 🙂

If a document match a mapping like this :

"mappings": {  
"doc": {  
"properties": {  
"paragraph" : {  
"type": "multi-field"  
"fields": {  
"sentence" : {  
"type": "string",  
"position\_offset\_gap": 256  
}  
}  
}  
}  
}  
}

So, if i understand, using your logic, i could allow my users to look for 2  
terms in the same sentence, but i can also allow users to look for 2 terms  
in the same paragraph in omitting the slop parameter and with the same  
query, right ?

And the last dummy question, i promise : is there an impact using  
"position\_offset\_gap" for my document in terms of size in the index or any  
other impact? It won't make any difference if i have tons of "sentence" in  
my docs otherwise that is normal weight ?

Thanks again Igor. That's a nice option. It could be a nice idea to add it  
to the doc.

--

---

<div class="post-metadata">

**Author:** ![Loic\_Bertron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loic_bertron/32/1794_2.png) [@Loic\_Bertron](https://discuss.elastic.co/u/Loic_Bertron)\
**Post date:** [November 22, 2012, 4:14pm UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/8 "2012-11-22T16:14:41Z")

</div>

Nice 🙂

If a document match a mapping like this :

"mappings": {  
"doc": {  
"properties": {  
"paragraph" : {  
"sentence" : {  
"type": "string",  
"position\_offset\_gap": 256  
}

So, if i understand, using your logic, i could allow my users to look for 2  
terms in the same sentence, but i can also allow users to look for 2 terms  
in the same paragraph in omitting the slop parameter and with the same  
query, right ?

And the last dummy question, i promise : is there an impact using  
"position\_offset\_gap" for my document in terms of size in the index or any  
other impact? It won't make any difference if i have tons of "sentence" in  
my docs otherwise that is normal weight ?

Thanks again Igor. That's a nice option. It could be a nice idea to add it  
to the doc.

--

---

<div class="post-metadata">

**Author:** ![Loic\_Bertron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loic_bertron/32/1794_2.png) [@Loic\_Bertron](https://discuss.elastic.co/u/Loic_Bertron)\
**Post date:** [November 22, 2012, 4:16pm UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/9 "2012-11-22T16:16:04Z")

</div>

Nice 🙂

If a document match a mapping like this :

"mappings": {  
"doc": {  
"properties": {  
"paragraph" : {  
"sentence" : {  
"type": "string",  
"position\_offset\_gap": 256  
}  
}  
}  
}  
}

So, if i understand, using your logic, i could allow my users to look for 2  
terms in the same sentence, but i can also allow users to look for 2 terms  
in the same paragraph in omitting the slop parameter and with the same  
query, right ?

And the last dummy question, i promise : is there an impact using  
"position\_offset\_gap" for my document in terms of size in the index or any  
other impact? It won't make any difference if i have tons of "sentence" in  
my docs otherwise that is normal weight ?

Thanks again Igor. That's a nice option. It could be a nice idea to add it  
to the doc.

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 23, 2012, 1:45am UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/10 "2012-11-23T01:45:34Z")

</div>

That mapping is not going to work because all it essentially does is rename  
field "sentence" into "paragraph.sentence". If you need two levels, you  
will have to do something more complicated than this. For example, you can  
make sentence a nested object[http://www.elasticsearch.org/guide/reference/mapping/nested-type.html](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html).  
Then for paragraph search, you will use normal boolean search wrapped into nested  
query[http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html) and  
for sentence search you can use span query wrapped into nested query.  
Another way of doing this is to index text twice: divided by sentences and  
divided by paragraphs and use span query with one field or with another  
depending on how you want search to work.

The position\_offset\_gap just increments the token position, so I cannot  
think of any reason why it could have an adverse effect.

On Thursday, November 22, 2012 11:16:04 AM UTC-5, Loïc Bertron wrote:

> Nice 🙂
> 
> If a document match a mapping like this :
> 
> "mappings": {  
> "doc": {  
> "properties": {  
> "paragraph" : {  
> "sentence" : {  
> "type": "string",  
> "position\_offset\_gap": 256  
> }  
> }  
> }  
> }  
> }
> 
> So, if i understand, using your logic, i could allow my users to look for  
> 2  
> terms in the same sentence, but i can also allow users to look for 2 terms  
> in the same paragraph in omitting the slop parameter and with the same  
> query, right ?
> 
> And the last dummy question, i promise : is there an impact using  
> "position\_offset\_gap" for my document in terms of size in the index or any  
> other impact? It won't make any difference if i have tons of "sentence" in  
> my docs otherwise that is normal weight ?
> 
> Thanks again Igor. That's a nice option. It could be a nice idea to add it  
> to the doc.

--

---

<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, 3:03am UTC](https://discuss.elastic.co/t/search-in-the-same-phrase/9783/11 "2017-07-06T03:03:13Z")

</div>


