# SpanNotQuery issues

**URL:** <https://discuss.elastic.co/t/spannotquery-issues/8803>\
**Category:** Elasticsearch\
**Created:** [August 21, 2012, 6:59pm UTC](https://discuss.elastic.co/t/spannotquery-issues/8803 "2012-08-21T18:59:25Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [August 21, 2012, 6:59pm UTC](https://discuss.elastic.co/t/spannotquery-issues/8803/1 "2012-08-21T18:59:25Z")

</div>

Is anyone using SpanNotQuerys? Judging by a recent issue that was  
never uncovered until now, I am assuming not:

> <https://github.com/elastic/elasticsearch/issues/2192>

The exclude portion of my SpanNotQuerys are having no effect on the query.

Given a document { "title": "Handheld Apple iPad 2 Wi Fi tablet iOS 5  
64 GB 9.7 black [Buy.com](http://Buy.com)" }

These two queries return the same results:

SpanNotQuery  
{  
"query" : {  
"span\_not" : {  
"include" : {  
"span\_term" : {  
"title" : "ipad"  
}  
},  
"exclude" : {  
"span\_term" : {  
"title" : "black"  
}  
}  
}  
}  
}

SpanTermQuery  
{  
"query" : {  
"span\_term" : {  
"TitleString\_en" : {  
"value" : "ipad",  
"boost" : 1.0  
}  
}  
}  
}

Explain between the two queries is identical:  
9.180814 = fieldWeight(title:spanNot(ipad, black) in 531555), product of:  
0.70710677 = tf(phraseFreq=0.5)  
7.4192176 = idf(title: ipad=9370)  
1.75 = fieldNorm(field=title, doc=531555)

SpanNotQueries in Lucene are working perfectly: spanNot(title:ipad,  
title:black). I haven't traced through the code in ElasticSearch, but  
the code seems to be creating the correct Lucene class. Anyone  
successfully using SpanNotQuerys?

Cheers,

Ivan

--

---

<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:** [August 21, 2012, 8:58pm UTC](https://discuss.elastic.co/t/spannotquery-issues/8803/2 "2012-08-21T20:58:28Z")

</div>

Reproducible issue: [SpanNotQuery · GitHub](https://gist.github.com/d3f9f82ec6c95c11585e)

On Tue, Aug 21, 2012 at 11:59 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Is anyone using SpanNotQuerys? Judging by a recent issue that was  
> never uncovered until now, I am assuming not:  
> [Incorrect application of boost in SpanNotQueryBuilder · Issue #2192 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2192)
> 
> The exclude portion of my SpanNotQuerys are having no effect on the query.
> 
> Given a document { "title": "Handheld Apple iPad 2 Wi Fi tablet iOS 5  
> 64 GB 9.7 black [Buy.com](http://Buy.com)" }
> 
> These two queries return the same results:
> 
> SpanNotQuery  
> {  
> "query" : {  
> "span\_not" : {  
> "include" : {  
> "span\_term" : {  
> "title" : "ipad"  
> }  
> },  
> "exclude" : {  
> "span\_term" : {  
> "title" : "black"  
> }  
> }  
> }  
> }  
> }
> 
> SpanTermQuery  
> {  
> "query" : {  
> "span\_term" : {  
> "TitleString\_en" : {  
> "value" : "ipad",  
> "boost" : 1.0  
> }  
> }  
> }  
> }
> 
> Explain between the two queries is identical:  
> 9.180814 = fieldWeight(title:spanNot(ipad, black) in 531555), product of:  
> 0.70710677 = tf(phraseFreq=0.5)  
> 7.4192176 = idf(title: ipad=9370)  
> 1.75 = fieldNorm(field=title, doc=531555)
> 
> SpanNotQueries in Lucene are working perfectly: spanNot(title:ipad,  
> title:black). I haven't traced through the code in Elasticsearch, but  
> the code seems to be creating the correct Lucene class. Anyone  
> successfully using SpanNotQuerys?
> 
> Cheers,
> 
> Ivan

--

---

<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:** [August 22, 2012, 6:01am UTC](https://discuss.elastic.co/t/spannotquery-issues/8803/3 "2012-08-22T06:01:09Z")

</div>

Slight error on my part. Without even realizing it, I was using a  
custom query parser in Lucene that handled SpanNotQueries differently.  
The queries work as expected in Elasticsearch, true to the Lucene  
standard.

--  
Ivan

On Tue, Aug 21, 2012 at 1:58 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Reproducible issue: [SpanNotQuery · GitHub](https://gist.github.com/d3f9f82ec6c95c11585e)
> 
> On Tue, Aug 21, 2012 at 11:59 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> 
> > Is anyone using SpanNotQuerys? Judging by a recent issue that was  
> > never uncovered until now, I am assuming not:  
> > [Incorrect application of boost in SpanNotQueryBuilder · Issue #2192 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2192)
> > 
> > The exclude portion of my SpanNotQuerys are having no effect on the query.
> > 
> > Given a document { "title": "Handheld Apple iPad 2 Wi Fi tablet iOS 5  
> > 64 GB 9.7 black [Buy.com](http://Buy.com)" }
> > 
> > These two queries return the same results:
> > 
> > SpanNotQuery  
> > {  
> > "query" : {  
> > "span\_not" : {  
> > "include" : {  
> > "span\_term" : {  
> > "title" : "ipad"  
> > }  
> > },  
> > "exclude" : {  
> > "span\_term" : {  
> > "title" : "black"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > SpanTermQuery  
> > {  
> > "query" : {  
> > "span\_term" : {  
> > "TitleString\_en" : {  
> > "value" : "ipad",  
> > "boost" : 1.0  
> > }  
> > }  
> > }  
> > }
> > 
> > Explain between the two queries is identical:  
> > 9.180814 = fieldWeight(title:spanNot(ipad, black) in 531555), product of:  
> > 0.70710677 = tf(phraseFreq=0.5)  
> > 7.4192176 = idf(title: ipad=9370)  
> > 1.75 = fieldNorm(field=title, doc=531555)
> > 
> > SpanNotQueries in Lucene are working perfectly: spanNot(title:ipad,  
> > title:black). I haven't traced through the code in Elasticsearch, but  
> > the code seems to be creating the correct Lucene class. Anyone  
> > successfully using SpanNotQuerys?
> > 
> > Cheers,
> > 
> > Ivan

--

---

<div class="post-metadata">

**Author:** ![Jade\_Tremblay](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jade\_Tremblay](https://discuss.elastic.co/u/Jade_Tremblay)\
**Post date:** [November 6, 2014, 3:12pm UTC](https://discuss.elastic.co/t/spannotquery-issues/8803/4 "2014-11-06T15:12:39Z")

</div>

Hello Ivan,

I know this post is pretty old.  
I am definitely puzzled with the gist that you provided.  
Why is there 2 matches?  
"exclude" : {  
"span\_term" : {  
"field1" : "dog"  
}  
}  
I though we should exclude match with dog...  
Could you please point me to proper information to understand what is  
happening?

Thx,  
Jade

Le mercredi 22 août 2012 02:01:09 UTC-4, Ivan Brusic a écrit :

> Slight error on my part. Without even realizing it, I was using a  
> custom query parser in Lucene that handled SpanNotQueries differently.  
> The queries work as expected in Elasticsearch, true to the Lucene  
> standard.
> 
> --  
> Ivan
> 
> On Tue, Aug 21, 2012 at 1:58 PM, Ivan Brusic \<[iv...@brusic.com](mailto:iv...@brusic.com)  
> \<javascript:\>\> wrote:
> 
> > Reproducible issue: [SpanNotQuery · GitHub](https://gist.github.com/d3f9f82ec6c95c11585e)
> > 
> > On Tue, Aug 21, 2012 at 11:59 AM, Ivan Brusic \<[iv...@brusic.com](mailto:iv...@brusic.com)  
> > \<javascript:\>\> wrote:
> > 
> > > Is anyone using SpanNotQuerys? Judging by a recent issue that was  
> > > never uncovered until now, I am assuming not:  
> > > [Incorrect application of boost in SpanNotQueryBuilder · Issue #2192 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2192)
> > > 
> > > The exclude portion of my SpanNotQuerys are having no effect on the  
> > > query.
> > > 
> > > Given a document { "title": "Handheld Apple iPad 2 Wi Fi tablet iOS 5  
> > > 64 GB 9.7 black [Buy.com](http://Buy.com)" }
> > > 
> > > These two queries return the same results:
> > > 
> > > SpanNotQuery  
> > > {  
> > > "query" : {  
> > > "span\_not" : {  
> > > "include" : {  
> > > "span\_term" : {  
> > > "title" : "ipad"  
> > > }  
> > > },  
> > > "exclude" : {  
> > > "span\_term" : {  
> > > "title" : "black"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > SpanTermQuery  
> > > {  
> > > "query" : {  
> > > "span\_term" : {  
> > > "TitleString\_en" : {  
> > > "value" : "ipad",  
> > > "boost" : 1.0  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > Explain between the two queries is identical:  
> > > 9.180814 = fieldWeight(title:spanNot(ipad, black) in 531555), product  
> > > of:  
> > > 0.70710677 = tf(phraseFreq=0.5)  
> > > 7.4192176 = idf(title: ipad=9370)  
> > > 1.75 = fieldNorm(field=title, doc=531555)
> > > 
> > > SpanNotQueries in Lucene are working perfectly: spanNot(title:ipad,  
> > > title:black). I haven't traced through the code in Elasticsearch, but  
> > > the code seems to be creating the correct Lucene class. Anyone  
> > > successfully using SpanNotQuerys?
> > > 
> > > Cheers,
> > > 
> > > Ivan

--  
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/0b81d1aa-35e4-468d-8e66-6a8b50029e6c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0b81d1aa-35e4-468d-8e66-6a8b50029e6c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2014, 3:43pm UTC](https://discuss.elastic.co/t/spannotquery-issues/8803/5 "2014-11-06T15:43:19Z")

</div>

Pretty old indeed. As explained briefly, I was migrating a Lucene system to  
Elasticsearch and did not understand why the span not queries were not  
working, only to discover we had a custom parser, to support syntaxes such  
as the one you are expecting.

Span nots are tricky in Lucene, but basically you are looking for when  
"dog" is not a near "quick" near a "dog". Sounds confusing because it is.

I noticed you +1 the pull request I submitted which will probably help your  
use case. The code should work as is without a need to merge in the current  
codebase, but I guess it would help. Stuck on a laptop with Java 6, so I  
can no longer build Elasticsearch. Will try to find the time.

Cheers,

Ivan

On Thu, Nov 6, 2014 at 10:12 AM, Jade Tremblay [jadetremblay.ing@gmail.com](mailto:jadetremblay.ing@gmail.com)  
wrote:

> Hello Ivan,
> 
> I know this post is pretty old.  
> I am definitely puzzled with the gist that you provided.  
> Why is there 2 matches?  
> "exclude" : {  
> "span\_term" : {  
> "field1" : "dog"  
> }  
> }  
> I though we should exclude match with dog...  
> Could you please point me to proper information to understand what is  
> happening?
> 
> Thx,  
> Jade
> 
> Le mercredi 22 août 2012 02:01:09 UTC-4, Ivan Brusic a écrit :
> 
> > Slight error on my part. Without even realizing it, I was using a  
> > custom query parser in Lucene that handled SpanNotQueries differently.  
> > The queries work as expected in Elasticsearch, true to the Lucene  
> > standard.
> > 
> > --  
> > Ivan
> > 
> > On Tue, Aug 21, 2012 at 1:58 PM, Ivan Brusic [iv...@brusic.com](mailto:iv...@brusic.com) wrote:
> > 
> > > Reproducible issue: [SpanNotQuery · GitHub](https://gist.github.com/d3f9f82ec6c95c11585e)
> > > 
> > > On Tue, Aug 21, 2012 at 11:59 AM, Ivan Brusic [iv...@brusic.com](mailto:iv...@brusic.com)  
> > > wrote:
> > > 
> > > > Is anyone using SpanNotQuerys? Judging by a recent issue that was  
> > > > never uncovered until now, I am assuming not:  
> > > > [Incorrect application of boost in SpanNotQueryBuilder · Issue #2192 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2192)
> > > > 
> > > > The exclude portion of my SpanNotQuerys are having no effect on the  
> > > > query.
> > > > 
> > > > Given a document { "title": "Handheld Apple iPad 2 Wi Fi tablet iOS 5  
> > > > 64 GB 9.7 black [Buy.com](http://Buy.com)" }
> > > > 
> > > > These two queries return the same results:
> > > > 
> > > > SpanNotQuery  
> > > > {  
> > > > "query" : {  
> > > > "span\_not" : {  
> > > > "include" : {  
> > > > "span\_term" : {  
> > > > "title" : "ipad"  
> > > > }  
> > > > },  
> > > > "exclude" : {  
> > > > "span\_term" : {  
> > > > "title" : "black"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > SpanTermQuery  
> > > > {  
> > > > "query" : {  
> > > > "span\_term" : {  
> > > > "TitleString\_en" : {  
> > > > "value" : "ipad",  
> > > > "boost" : 1.0  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > Explain between the two queries is identical:  
> > > > 9.180814 = fieldWeight(title:spanNot(ipad, black) in 531555), product  
> > > > of:  
> > > > 0.70710677 = tf(phraseFreq=0.5)  
> > > > 7.4192176 = idf(title: ipad=9370)  
> > > > 1.75 = fieldNorm(field=title, doc=531555)
> > > > 
> > > > SpanNotQueries in Lucene are working perfectly: spanNot(title:ipad,  
> > > > title:black). I haven't traced through the code in Elasticsearch, but  
> > > > the code seems to be creating the correct Lucene class. Anyone  
> > > > successfully using SpanNotQuerys?
> > > > 
> > > > Cheers,
> > > > 
> > > > Ivan
> 
> --  
> 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/0b81d1aa-35e4-468d-8e66-6a8b50029e6c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0b81d1aa-35e4-468d-8e66-6a8b50029e6c%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/0b81d1aa-35e4-468d-8e66-6a8b50029e6c%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/0b81d1aa-35e4-468d-8e66-6a8b50029e6c%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/CALY%3DcQCbVdBcZxVeDQLSUaPOGScaZMit6soFyteSymaT3y3-1g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCbVdBcZxVeDQLSUaPOGScaZMit6soFyteSymaT3y3-1g%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Jade\_Tremblay](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jade\_Tremblay](https://discuss.elastic.co/u/Jade_Tremblay)\
**Post date:** [November 6, 2014, 7:56pm UTC](https://discuss.elastic.co/t/spannotquery-issues/8803/6 "2014-11-06T19:56:56Z")

</div>

@Ivan

I've pick you git push and integrated it into ElasticSearch source  
tag:v1.3.5  
After a rebuild, it seems to work perfectly (I am still trying to find the  
maximum values for pre and post, no luck so far).

I've been able to figure out how span\_not works with this post.  
I add it here if someone else is looking to understand why the actual gist  
return 2 results.

> <https://stackoverflow.com/questions/24260103/spannotquery-giving-unexpected-results-exclude-is-ignored>

Thanks for the gist and the push request!

Cheers,  
Jade

--  
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/776c24aa-0a11-46ce-8c81-5f78e4c187dc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/776c24aa-0a11-46ce-8c81-5f78e4c187dc%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 7, 2014, 4:16pm UTC](https://discuss.elastic.co/t/spannotquery-issues/8803/7 "2014-11-07T16:16:18Z")

</div>

Yes, that post explained it a lot better than I wanted to. 🙂 But basically  
yes, the exclude portion is only as part of an existing span, but a single  
span term is not really a span.

Ultimately, span queries are not very flexible since they do not analyze  
terms, which is why I suspect there are rarely any questions about them  
(and perhaps why my PR is in limbo). Phrase matches might work better, but  
they do not support in order slop.

--  
Ivan

On Thu, Nov 6, 2014 at 2:56 PM, Jade Tremblay [jadetremblay.ing@gmail.com](mailto:jadetremblay.ing@gmail.com)  
wrote:

> @Ivan
> 
> I've pick you git push and integrated it into Elasticsearch source  
> tag:v1.3.5  
> After a rebuild, it seems to work perfectly (I am still trying to find the  
> maximum values for pre and post, no luck so far).
> 
> I've been able to figure out how span\_not works with this post.  
> I add it here if someone else is looking to understand why the actual gist  
> return 2 results.
> 
> [lucene - SpanNotQuery giving unexpected results (exclude is ignored) - Stack Overflow](http://stackoverflow.com/questions/24260103/spannotquery-giving-unexpected-results-exclude-is-ignored)
> 
> Thanks for the gist and the push request!
> 
> Cheers,  
> Jade
> 
> --  
> 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/776c24aa-0a11-46ce-8c81-5f78e4c187dc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/776c24aa-0a11-46ce-8c81-5f78e4c187dc%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/776c24aa-0a11-46ce-8c81-5f78e4c187dc%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/776c24aa-0a11-46ce-8c81-5f78e4c187dc%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/CALY%3DcQAipZAp7fTBTp8PRy\_RBORkmTTZJdDipqqeiB3HnUHu3g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAipZAp7fTBTp8PRy_RBORkmTTZJdDipqqeiB3HnUHu3g%40mail.gmail.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:51am UTC](https://discuss.elastic.co/t/spannotquery-issues/8803/8 "2017-07-06T00:51:28Z")

</div>


