Doing an AND Query within a span_near, e.g., foo WITHIN 1 (biz AND buz)

Hi All,

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

Original SO Question:

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

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

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
To view this discussion on the web visit
For more options, visit