# Doing an AND Query within a span\_near, e.g., foo WITHIN 1 (biz AND buz)

**URL:** <https://discuss.elastic.co/t/doing-an-and-query-within-a-span-near-e-g-foo-within-1-biz-and-buz/20274>\
**Category:** Elasticsearch\
**Created:** [October 16, 2014, 3:56am UTC](https://discuss.elastic.co/t/doing-an-and-query-within-a-span-near-e-g-foo-within-1-biz-and-buz/20274 "2014-10-16T03:56:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Sander](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_sander/32/838_2.png) [@Michael\_Sander](https://discuss.elastic.co/u/Michael_Sander)\
**Post date:** [October 16, 2014, 3:56am UTC](https://discuss.elastic.co/t/doing-an-and-query-within-a-span-near-e-g-foo-within-1-biz-and-buz/20274/1 "2014-10-16T03:56:31Z")

</div>

Hi All,

I posted this stackoverflow question (copied below) and would be grateful  
for any input.

> <https://stackoverflow.com/questions/26395923/elasticsearch-span-near-with-a-large-slop-within-a-span-near-with-a-small-slop>

_Original SO Question:_

How would you build a json Elasticsearch query that looks like this (in  
english):

foo WITHIN 1 (biz AND buz)

## I expect the query to return documents where both biz and buz exist, and also, where the word foois adjacent to one of those words.

_My Original Solution_

One way would be to put use span\_near. Then for its first clause, use the  
foo term, and for its second clause, use a boolean AND. However, in  
Elasticsearch, you cannot put booleans within spans, you can only put other  
spans within spans. Accordingly, you must simulate the boolean AND with  
another span\_near with a large slop.

The solution I tried is:

{'span\_near': {'clauses': [{'span\_term': {'text': 'foo'}},  
{'span\_near': {'clauses': [{'span\_term': {'text': 'biz'}},  
{'span\_term': {'text': 'buz'}}],  
'in\_order': False,  
'slop': 1000000}}],  
'in\_order': False,  
'slop': 0}}

## Notice that we simulate AND with a slop of 1000000 (effectively infinite for my domain). Unfortunately, the above query _does not work_. Instead, the above query returns all documents with the words foo, biz, and buz, and where the word foo occurs between the occurrences of biz and buz.

_Another Solution, but Onerous_

I think one solution would be to convert the original query into

(biz AND buz) AND ((foo WITHIN 1 biz) OR (foo WITHIN 1 buz))

This seems very difficult to implement as one would need to parse for AND keywords  
within a span\_near operation and do the necessary conversions. Any other  
ideas?

_Note_: I am using Elasticsearch, but this question would apply equally to  
Lucene using their Java primitives.

--  
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/d2f12ba6-9596-4b87-a57b-219b83ec83f8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d2f12ba6-9596-4b87-a57b-219b83ec83f8%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:55am UTC](https://discuss.elastic.co/t/doing-an-and-query-within-a-span-near-e-g-foo-within-1-biz-and-buz/20274/2 "2017-07-06T00:55:53Z")

</div>


