# Most common adjacent words

**URL:** <https://discuss.elastic.co/t/most-common-adjacent-words/22319>\
**Category:** Elasticsearch\
**Created:** [February 22, 2015, 11:42pm UTC](https://discuss.elastic.co/t/most-common-adjacent-words/22319 "2015-02-22T23:42:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarib](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jarib/32/882_2.png) [@jarib](https://discuss.elastic.co/u/jarib)\
**Post date:** [February 22, 2015, 11:42pm UTC](https://discuss.elastic.co/t/most-common-adjacent-words/22319/1 "2015-02-22T23:42:12Z")

</div>

Hello,

Does elasticsearch have the ability to return the most common _adjacent_  
words for a given search query?

That is, given some documents:

{"text": "To be or not to be, that is the question"}  
{"text": "We know what we are, but know not what we may be"}  
{"text": "If music be the food of love, play on."}

If I search for "be", I'd like to get back something like this:

{ "to be": 2 }  
{ "may be": 1 }  
{ "be or": 1 }  
{ "music be": 1 }  
{ "be the": 1 }

I was looking at the phrase suggester, but couldn't make it do this (it  
seems adamant about correcting the input text).  
If there's no way to do this currently, would it be feasible to write a  
plugin to do so?

Any advice is much appreciated.

Jari

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1f4a0ffa-b78c-426c-bc9a-76b068833544%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1f4a0ffa-b78c-426c-bc9a-76b068833544%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 23, 2015, 4:01am UTC](https://discuss.elastic.co/t/most-common-adjacent-words/22319/2 "2015-02-23T04:01:45Z")

</div>

I don't see a way to do exactly what you are looking for.  
But, with a little effort on client you could give a try to the highlighting feature which could give something similar.

Or may be an aggregation with a first level agg as a filter for the term, then a Terms agg on the field but with a shingle analyzer,  
Might give some results.

HTH.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

> Le 23 févr. 2015 à 00:42, jari@holderdeord.no a écrit :
> 
> Hello,
> 
> Does elasticsearch have the ability to return the most common _adjacent_ words for a given search query?
> 
> That is, given some documents:
> 
> {"text": "To be or not to be, that is the question"}  
> {"text": "We know what we are, but know not what we may be"}  
> {"text": "If music be the food of love, play on."}
> 
> If I search for "be", I'd like to get back something like this:
> 
> { "to be": 2 }  
> { "may be": 1 }  
> { "be or": 1 }  
> { "music be": 1 }  
> { "be the": 1 }
> 
> I was looking at the phrase suggester, but couldn't make it do this (it seems adamant about correcting the input text).  
> If there's no way to do this currently, would it be feasible to write a plugin to do so?
> 
> Any advice is much appreciated.
> 
> ## Jari
> 
> 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).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1f4a0ffa-b78c-426c-bc9a-76b068833544%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1f4a0ffa-b78c-426c-bc9a-76b068833544%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8DC12885-C3E7-438C-BAE2-4784FFECC14B%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/8DC12885-C3E7-438C-BAE2-4784FFECC14B%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jarib](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jarib/32/882_2.png) [@jarib](https://discuss.elastic.co/u/jarib)\
**Post date:** [February 23, 2015, 11:10am UTC](https://discuss.elastic.co/t/most-common-adjacent-words/22319/3 "2015-02-23T11:10:50Z")

</div>

Thanks for the suggestion.

I tried your second idea but it seems like running a terms aggregation on  
my shingles text field is a bit too much for ES.  
Even if it did work, it wouldn't have given me any data on adjacency /  
proximity.

[FIELDDATA] Data too large, data for [text.shingles] would be larger than  
limit of [623326003/594.4mb]]

{  
"size": 0,  
"aggregations": {  
"myAggregation": {  
"filter": {  
"query": {  
"query\_string": {  
"default\_field": "text",  
"query": "foo"  
}  
}  
},  
"aggregations": {  
"combos": {  
"terms": { "field": "text.shingles" }  
}  
}  
}  
}  
}

On Monday, February 23, 2015 at 5:01:59 AM UTC+1, David Pilato wrote:

> I don't see a way to do exactly what you are looking for.  
> But, with a little effort on client you could give a try to the  
> highlighting feature which could give something similar.
> 
> Or may be an aggregation with a first level agg as a filter for the term,  
> then a Terms agg on the field but with a shingle analyzer,  
> Might give some results.
> 
> HTH.
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 23 févr. 2015 à 00:42, ja...@holderdeord.no \<javascript:\> a écrit :
> 
> Hello,
> 
> Does elasticsearch have the ability to return the most common _adjacent_  
> words for a given search query?
> 
> That is, given some documents:
> 
> {"text": "To be or not to be, that is the question"}  
> {"text": "We know what we are, but know not what we may be"}  
> {"text": "If music be the food of love, play on."}
> 
> If I search for "be", I'd like to get back something like this:
> 
> { "to be": 2 }  
> { "may be": 1 }  
> { "be or": 1 }  
> { "music be": 1 }  
> { "be the": 1 }
> 
> I was looking at the phrase suggester, but couldn't make it do this (it  
> seems adamant about correcting the input text).  
> If there's no way to do this currently, would it be feasible to write a  
> plugin to do so?
> 
> Any advice is much appreciated.
> 
> Jari
> 
> --  
> 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/1f4a0ffa-b78c-426c-bc9a-76b068833544%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1f4a0ffa-b78c-426c-bc9a-76b068833544%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/1f4a0ffa-b78c-426c-bc9a-76b068833544%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1f4a0ffa-b78c-426c-bc9a-76b068833544%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c8c42fde-3b85-44a9-8802-b4c6e4fc0545%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c8c42fde-3b85-44a9-8802-b4c6e4fc0545%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:30am UTC](https://discuss.elastic.co/t/most-common-adjacent-words/22319/4 "2017-07-06T00:30:51Z")

</div>


