# Running into a strange bug while implementing has\_parent filter

**URL:** <https://discuss.elastic.co/t/running-into-a-strange-bug-while-implementing-has-parent-filter/6859>\
**Category:** Elasticsearch\
**Created:** [March 1, 2012, 9:41pm UTC](https://discuss.elastic.co/t/running-into-a-strange-bug-while-implementing-has-parent-filter/6859 "2012-03-01T21:41:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hao\_Lian](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@Hao\_Lian](https://discuss.elastic.co/u/Hao_Lian)\
**Post date:** [March 1, 2012, 9:41pm UTC](https://discuss.elastic.co/t/running-into-a-strange-bug-while-implementing-has-parent-filter/6859/1 "2012-03-01T21:41:23Z")

</div>

Hello!

We're trying to implement a has\_parent filter, and we're running into  
a strange problem where this query works:

{  
"filter": {  
"has\_parent": {  
"query": {  
"term": {  
"sId": "095520ef2aaa5d1e14d46296fdb2464f9b21dcaf"  
}  
},  
"type": "changeset"  
}  
},  
"query": {  
"term": {  
"ixRepo": 330  
}  
}  
}

but this query does not:

{  
"filter": {  
"has\_parent": {  
"query": {  
"term": {  
"ixHostedDB": -1  
}  
},  
"type": "changeset"  
}  
},  
"query": {  
"term": {  
"ixRepo": 330  
}  
}  
}

First, let me explain how the filter works a little: Using the above  
query as an example, it should return all documents that not only have  
a parent type of "changeset" but also match the inner query. So the  
first query works fine: it returns a bunch of documents with a  
changeset parent that has sId=0955[snip].

However, the second query returns zero documents even though we know  
there are successful matches in the index, i.e. documents that  
definitely have a changeset parent with ixHostedDB definitely set to  
-1. As far as we can tell, the two fields (sId and ixHostedDB) only  
differ by their types: sId is mapped as a string and ixHostedDB is  
mapped as an integer. Here's our mapping:

{  
"kiln-2-8" : {  
"changesetrepo" : {  
"\_source" : {  
"enabled" : false  
},  
"\_routing" : {  
"required" : true  
},  
"properties" : {  
"ixRepo" : {  
"type" : "integer"  
}  
},  
"\_parent" : {  
"type" : "changeset"  
}  
}  
"changeset" : {  
"properties" : {  
"id" : {  
"type" : "string"  
},  
"dt" : {  
"format" : "dateOptionalTime",  
"type" : "date"  
},  
"sDescription" : {  
"store" : "yes",  
"analyzer" : "snowball",  
"term\_vector" : "with\_positions\_offsets",  
"type" : "string"  
},  
"ixHostedDB" : {  
"type" : "integer"  
},  
"sParentId1" : {  
"type" : "string"  
},  
"sParentId2" : {  
"type" : "string"  
},  
"sBranch" : {  
"type" : "string"  
},  
"sId" : {  
"type" : "string"  
},  
"sExtra" : {  
"type" : "string"  
},  
"sAuthor" : {  
"type" : "string"  
}  
}  
}  
}  
}

And here is our code for the has\_parent filter, plus some basic tests,  
squashed into one commit:  
[https://github.com/hlian/elasticsearch/commit/8f9bbaa0897678e7f2e82d1e90b23f3add20e164](https://github.com/hlian/elasticsearch/commit/8f9bbaa0897678e7f2e82d1e90b23f3add20e164)

The code's still incomplete (we'd like to implement the Query  
equivalents as well and clean up the tests), but if you guys could  
take a look at it and see if you spot any bugs, that'd be great.

We've tried stepping through the ES and Lucene source code to no  
avail. The one interesting thing we found is that, inside  
TermQuery#TermWeight's constructor in org.apache.lucene.search,  
"r.docFreq(term)" always returns 0 for the failing query, where term  
corresponds to "ixHostedDB:-1". Whereas it returns positive values for  
the successful query, i.e. when term corresponds to "sId:0955[snip]"  
or "sAuthor:hao" or basically anything except ixHostedDB. We think  
that the docFreq being zero is related to our lack of results, but  
again we're new to both ES and Lucene's code so we're not sure.

It's additionally strange because it passes our unit tests, which  
tests among other things a simplified example of the query that isn't  
working (the one with ixHostedDB).

Anyway, any feedback would be appreciated!

Thanks,  
Hao.

---

<div class="post-metadata">

**Author:** ![Hao\_Lian](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@Hao\_Lian](https://discuss.elastic.co/u/Hao_Lian)\
**Post date:** [March 2, 2012, 3:50pm UTC](https://discuss.elastic.co/t/running-into-a-strange-bug-while-implementing-has-parent-filter/6859/2 "2012-03-02T15:50:00Z")

</div>

On Thu, Mar 1, 2012 at 4:41 PM, Hao Lian [hao@fogcreek.com](mailto:hao@fogcreek.com) wrote:

> Hello!
> 
> We're trying to implement a has\_parent filter, and we're running into  
> a strange problem where this query works:  
> [snip]

Hi! Follow-up post: we found the bug; we were missing a call to  
QueryParseContext. Sorry for the noise!

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 3, 2012, 10:02pm UTC](https://discuss.elastic.co/t/running-into-a-strange-bug-while-implementing-has-parent-filter/6859/3 "2012-03-03T22:02:38Z")

</div>

Cool. I haven't looked at the implementation yet…, ping me when you think its ready.

On Friday, March 2, 2012 at 5:50 PM, Hao Lian wrote:

> On Thu, Mar 1, 2012 at 4:41 PM, Hao Lian \<[hao@fogcreek.com](mailto:hao@fogcreek.com) ([mailto:hao@fogcreek.com](mailto:hao@fogcreek.com))\> wrote:
> 
> > Hello!
> > 
> > We're trying to implement a has\_parent filter, and we're running into  
> > a strange problem where this query works:  
> > [snip]
> 
> Hi! Follow-up post: we found the bug; we were missing a call to  
> QueryParseContext. Sorry for the noise!

---

<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, 3:37am UTC](https://discuss.elastic.co/t/running-into-a-strange-bug-while-implementing-has-parent-filter/6859/4 "2017-07-06T03:37:28Z")

</div>

Closed on July 6, 2017, 3:37am UTC


