# SpanNearQuery bug?

**URL:** <https://discuss.elastic.co/t/spannearquery-bug/164416>\
**Category:** Elasticsearch\
**Created:** [January 16, 2019, 8:14am UTC](https://discuss.elastic.co/t/spannearquery-bug/164416 "2019-01-16T08:14:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bwats](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bwats/32/39817_2.png) [@bwats](https://discuss.elastic.co/u/bwats)\
**Post date:** [January 16, 2019, 8:14am UTC](https://discuss.elastic.co/t/spannearquery-bug/164416/1 "2019-01-16T08:14:09Z")

</div>

I have two fields, and would like to match terms in the field IF in the same position i.e. span near query with zero slop. However, the spannear query is matching WHEN IT SHOULDN'T - i.e. based on my own design AND certified by looking at the term\_vectors!

Here is an example of the incorrect matches I've found shouldn't match but does - I've abstracted to "field1/2" and "value1/2":  
{  
"query": {  
"span\_near" : {  
"clauses" : [  
{ "span\_term" : { "text.field1" : "value1" } },  
{ "field\_masking\_span": {  
"query": { "span\_term" : { "text.field2" : "value2" } }  
,  
"field": "text.field1"  
}  
}  
],  
"slop" : 0,  
"in\_order" : false  
}  
}  
}

However, this isn't working as expected and seems buggy - deep diving into one of the erroneous hits and using term\_vectors for the hit shows that the values are NOT in the same position!

They are next to each other - value1 is at position 165 in field1 and value2 is at 164 in field2

The term vectors returned by elasticsearch for the values using:  
curl -X GET "localhost:9200/test/\_doc/1/\_termvectors" -H 'Content-Type: application/json' -d'  
{  
"fields" : ["text.field1", "text.field2"],  
"offsets" : true,  
"positions" : true,  
"term\_statistics" : true,  
"field\_statistics" : true  
}  
'

are:  
"value1": {  
"doc\_freq": 61,  
"ttf": 87,  
"term\_freq": 2,  
"tokens": [  
{  
"position": 165,  
"start\_offset": 954,  
"end\_offset": 962  
},  
{  
"position": 431,  
"start\_offset": 2535,  
"end\_offset": 2543  
}  
]  
},

"value2": {  
"doc\_freq": 3029,  
"ttf": 72118,  
"term\_freq": 14,  
"tokens": [  
{  
"position": 33,  
"start\_offset": 184,  
"end\_offset": 187  
},  
{  
"position": 68,  
"start\_offset": 382,  
"end\_offset": 385  
},  
{  
"position": 69,  
"start\_offset": 386,  
"end\_offset": 389  
},  
{  
"position": 163,  
"start\_offset": 946,  
"end\_offset": 949  
},  
{  
"position": 164,  
"start\_offset": 950,  
"end\_offset": 953  
},  
{  
"position": 227,  
"start\_offset": 1354,  
"end\_offset": 1357  
},  
{  
"position": 228,  
"start\_offset": 1358,  
"end\_offset": 1361  
},  
{  
"position": 261,  
"start\_offset": 1522,  
"end\_offset": 1525  
},  
{  
"position": 262,  
"start\_offset": 1526,  
"end\_offset": 1529  
},  
{  
"position": 334,  
"start\_offset": 1958,  
"end\_offset": 1961  
},  
{  
"position": 335,  
"start\_offset": 1962,  
"end\_offset": 1965  
},  
{  
"position": 382,  
"start\_offset": 2257,  
"end\_offset": 2260  
},  
{  
"position": 383,  
"start\_offset": 2261,  
"end\_offset": 2264  
},  
{  
"position": 445,  
"start\_offset": 2629,  
"end\_offset": 2632  
}  
]  
},

---

<div class="post-metadata">

**Author:** ![bwats](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bwats/32/39817_2.png) [@bwats](https://discuss.elastic.co/u/bwats)\
**Post date:** [January 16, 2019, 8:17am UTC](https://discuss.elastic.co/t/spannearquery-bug/164416/2 "2019-01-16T08:17:00Z")

</div>

Sorry - I should say I'm using version 6.3 Elasticsearch as this is the highest version supported in our production env (AWS Elasticsearch service)

---

<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:** [February 13, 2019, 8:17am UTC](https://discuss.elastic.co/t/spannearquery-bug/164416/3 "2019-02-13T08:17:07Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
