# Query Syntax for proximity searches

**URL:** <https://discuss.elastic.co/t/query-syntax-for-proximity-searches/23140>\
**Category:** Elasticsearch\
**Created:** [April 7, 2015, 11:20pm UTC](https://discuss.elastic.co/t/query-syntax-for-proximity-searches/23140 "2015-04-07T23:20:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bvnagaraju](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@bvnagaraju](https://discuss.elastic.co/u/bvnagaraju)\
**Post date:** [April 7, 2015, 11:20pm UTC](https://discuss.elastic.co/t/query-syntax-for-proximity-searches/23140/1 "2015-04-07T23:20:44Z")

</div>

Can some one help me getting these query syntax for below queries

how to find documents that has united and states but not with in 30 words ?(united  
AND states NOT WITHIN 30 WORDS)?

how to find the documents that has united before america ?(united BEFORE  
america)

how to find the documents that has united followed by america?(united  
FOLLOWEDBY america )

how to find the documents that war and peace near novel or book?(war AND  
peace) NEAR (novel OR book)

--  
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/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [April 8, 2015, 4:35am UTC](https://discuss.elastic.co/t/query-syntax-for-proximity-searches/23140/2 "2015-04-08T04:35:28Z")

</div>

Hi ,

how to find documents that has united and states but not with in 30  
words ?(united  
AND states NOT WITHIN 30 WORDS)?  
Use "united state"~30 in query string query or you can directly use slop in  
phrase match query.

For second and the third , you need to use the above example with a high  
slop value.

Last one , you need to expand that to query of above format.

You can find more information here -

> **[Query string query | Elasticsearch Guide \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html)**

Thanks  
Vineeth Mohan,  
Elasticsearch consultant,  
[qbox.io](http://qbox.io) ( Elasticsearch service provider [http://qbox.io/](http://qbox.io/))

On Wed, Apr 8, 2015 at 4:50 AM, bvnrwork [budda08nitt@gmail.com](mailto:budda08nitt@gmail.com) wrote:

> Can some one help me getting these query syntax for below queries
> 
> how to find documents that has united and states but not with in 30 words  
> ?(united AND states NOT WITHIN 30 WORDS)?
> 
> how to find the documents that has united before america ?(united BEFORE  
> america)
> 
> how to find the documents that has united followed by america?(united  
> FOLLOWEDBY america )
> 
> how to find the documents that war and peace near novel or book?(war AND  
> peace) NEAR (novel OR book)
> 
> --  
> 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/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%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/CAGdPd5n7WesXvrnebTOob2-um2THDDQTnLXEmAanUCwM%3DPiMYA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5n7WesXvrnebTOob2-um2THDDQTnLXEmAanUCwM%3DPiMYA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bvnagaraju](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@bvnagaraju](https://discuss.elastic.co/u/bvnagaraju)\
**Post date:** [April 8, 2015, 12:38pm UTC](https://discuss.elastic.co/t/query-syntax-for-proximity-searches/23140/3 "2015-04-08T12:38:03Z")

</div>

Thank you , can you clarify this , do we need to use not operator for  
getting "united and states not with in 30 words "

I tested it works to get united and states with in 30 words but not for"  
united and states not with in 30 words"

united AND states NOT WITHIN 30 WORDS

On Wednesday, 8 April 2015 00:35:35 UTC-4, vineeth mohan wrote:

> Hi ,
> 
> how to find documents that has united and states but not with in 30 words  
> ?(united AND states NOT WITHIN 30 WORDS)?  
> Use "united state"~30 in query string query or you can directly use slop  
> in phrase match query.
> 
> For second and the third , you need to use the above example with a high  
> slop value.
> 
> Last one , you need to expand that to query of above format.
> 
> You can find more information here -  
> [Query string query | Elasticsearch Guide [8.11] | Elastic](http://www.elastic.co/guide/en/elasticsearch/reference/1.x/query-dsl-query-string-query.html)
> 
> Thanks  
> Vineeth Mohan,  
> Elasticsearch consultant,  
> [qbox.io](http://qbox.io) ( Elasticsearch service provider [http://qbox.io/](http://qbox.io/))
> 
> On Wed, Apr 8, 2015 at 4:50 AM, bvnrwork \<[budda...@gmail.com](mailto:budda...@gmail.com) \<javascript:\>
> 
> > wrote:
> 
> > Can some one help me getting these query syntax for below queries
> > 
> > how to find documents that has united and states but not with in 30 words  
> > ?(united AND states NOT WITHIN 30 WORDS)?
> > 
> > how to find the documents that has united before america ?(united BEFORE  
> > america)
> > 
> > how to find the documents that has united followed by america?(united  
> > FOLLOWEDBY america )
> > 
> > how to find the documents that war and peace near novel or book?(war AND  
> > peace) NEAR (novel OR book)
> > 
> > --  
> > 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/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%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/5569730d-a90f-4ace-b5c9-d294165814db%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5569730d-a90f-4ace-b5c9-d294165814db%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bvnagaraju](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@bvnagaraju](https://discuss.elastic.co/u/bvnagaraju)\
**Post date:** [April 9, 2015, 3:26am UTC](https://discuss.elastic.co/t/query-syntax-for-proximity-searches/23140/4 "2015-04-09T03:26:43Z")

</div>

Actually I wrote the below queries , let me know if they are correct

1. "United States" ~ 3 - to get documents with United States with in 30  
words  
2)"United States" NOT "United States" ~ 30 - to get documents which has  
united states not with in 30 words  
3)war AND peace ~ (novel OR book) - to get documents which has war and  
peace and near to the words novel or book  
4)china AND India ~ World to get documents which has both china and india  
and near to the word "World"

What I dont know is to specify the order of the words in the queries

how to write queries to get documents which has united followed by states

how to write queries to get documents which has china before India

On Wednesday, 8 April 2015 00:35:35 UTC-4, vineeth mohan wrote:

> Hi ,
> 
> how to find documents that has united and states but not with in 30 words  
> ?(united AND states NOT WITHIN 30 WORDS)?  
> Use "united state"~30 in query string query or you can directly use slop  
> in phrase match query.
> 
> For second and the third , you need to use the above example with a high  
> slop value.
> 
> Last one , you need to expand that to query of above format.
> 
> You can find more information here -  
> [Query string query | Elasticsearch Guide [8.11] | Elastic](http://www.elastic.co/guide/en/elasticsearch/reference/1.x/query-dsl-query-string-query.html)
> 
> Thanks  
> Vineeth Mohan,  
> Elasticsearch consultant,  
> [qbox.io](http://qbox.io) ( Elasticsearch service provider [http://qbox.io/](http://qbox.io/))
> 
> On Wed, Apr 8, 2015 at 4:50 AM, bvnrwork \<[budda...@gmail.com](mailto:budda...@gmail.com) \<javascript:\>
> 
> > wrote:
> 
> > Can some one help me getting these query syntax for below queries
> > 
> > how to find documents that has united and states but not with in 30 words  
> > ?(united AND states NOT WITHIN 30 WORDS)?
> > 
> > how to find the documents that has united before america ?(united BEFORE  
> > america)
> > 
> > how to find the documents that has united followed by america?(united  
> > FOLLOWEDBY america )
> > 
> > how to find the documents that war and peace near novel or book?(war AND  
> > peace) NEAR (novel OR book)
> > 
> > --  
> > 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/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/9bbb528f-32ad-4fb9-9c6a-a5f116d5ac35%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/0e2a1e11-c453-4154-ab7e-44683c2781c3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0e2a1e11-c453-4154-ab7e-44683c2781c3%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:20am UTC](https://discuss.elastic.co/t/query-syntax-for-proximity-searches/23140/5 "2017-07-06T00:20:41Z")

</div>


