# Filter search with drill down approach using Java API

**URL:** <https://discuss.elastic.co/t/filter-search-with-drill-down-approach-using-java-api/14446>\
**Category:** Elasticsearch\
**Created:** [November 18, 2013, 11:30am UTC](https://discuss.elastic.co/t/filter-search-with-drill-down-approach-using-java-api/14446 "2013-11-18T11:30:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kavesh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kavesh](https://discuss.elastic.co/u/kavesh)\
**Post date:** [November 18, 2013, 11:30am UTC](https://discuss.elastic.co/t/filter-search-with-drill-down-approach-using-java-api/14446/1 "2013-11-18T11:30:12Z")

</div>

Hi All,

I need a help on drill down approach as i was successfully able to do the process at multi level search. The problem currently i am facing is my query gets generated twice for same option.

For eg : if i search on **Price -\> Title -\> Color** the generated query is  
  
**"bool" : {  
"must" : [ {  
"range" : {  
"Price" : {  
"from" : "52468",  
"to" : "62353",  
"include\_lower" : true,  
"include\_upper" : false  
}  
}  
}, {  
"term" : {  
"title" : "abc"  
}  
}, {  
"term" : {  
"color" : "white"  
}  
}]  
}**

the above query provides me a successful result.

If i change the color from white to red then the result is returned null and the query generated is

**"bool" : {  
"must" : [ {  
"range" : {  
"Price" : {  
"from" : "52468",  
"to" : "62353",  
"include\_lower" : true,  
"include\_upper" : false  
}  
}  
}, {  
"term" : {  
"title" : "abc"  
}  
}, {  
"term" : {  
"color" : "white"  
}  
}, {  
"term" : {  
"color" : "red"  
}  
} ]  
}**

The query which is expected is

 **"bool" : { "must" : [{ "range" : { "Price" : { "from" : "52468", "to" : "62353", "include\_lower" : true, "include\_upper" : false } } }, { "term" : { "title" : "abc" } }, { "term" : { "color" : "red" } }] }**

I tried using a json object which i was able to replace the color with red then the problem which occurred is i could not convert the **string to QueryBuilder**. I used **WrapperQuery** to convert string i got a encrypted data which i could not track forward it for query processing.

I have used **BoolQueryBuilder** for my approach.

Thanks

---

<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 18, 2013, 11:23pm UTC](https://discuss.elastic.co/t/filter-search-with-drill-down-approach-using-java-api/14446/2 "2013-11-18T23:23:09Z")

</div>

Why not create a new QueryBuilder? QueryBuilder instances should not be  
preserved after it builds a query.

--  
Ivan

On Mon, Nov 18, 2013 at 3:30 AM, kavesh [kavesh.shetgaonkar@privail.com](mailto:kavesh.shetgaonkar@privail.com)wrote:

> Hi All,
> 
> I need a help on drill down approach as i was successfully able to do the  
> process at multi level search. The problem currently i am facing is my  
> query  
> gets generated twice for same option.
> 
> For eg : if i search on _Price -\> Title -\> Color_ the generated query is  
> \*  
> "bool" : {  
> "must" : [ {  
> "range" : {  
> "Price" : {  
> "from" : "52468",  
> "to" : "62353",  
> "include\_lower" : true,  
> "include\_upper" : false  
> }  
> }  
> }, {  
> "term" : {  
> "title" : "abc"  
> }  
> }, {  
> "term" : {  
> "color" : "white"  
> }  
> }]  
> }\*
> 
> the above query provides me a successful result.
> 
> If i change the color from white to red then the result is returned null  
> and  
> the query generated is
> 
> - "bool" : {  
> "must" : [ {  
> "range" : {  
> "Price" : {  
> "from" : "52468",  
> "to" : "62353",  
> "include\_lower" : true,  
> "include\_upper" : false  
> }  
> }  
> }, {  
> "term" : {  
> "title" : "abc"  
> }  
> }, {  
> "term" : {  
> "color" : "white"  
> }  
> }, {  
> "term" : {  
> "color" : "red"  
> }  
> } ]  
> }\*
> 
> The query which is expected is
> 
> - 
> 
> "bool" : {  
> "must" : [ {  
> "range" : {  
> "Price" : {  
> "from" : "52468",  
> "to" : "62353",  
> "include\_lower" : true,  
> "include\_upper" : false  
> }  
> }  
> }, {  
> "term" : {  
> "title" : "abc"  
> }  
> }, {  
> "term" : {  
> "color" : "red"  
> }  
> }]  
> }\*
> 
> I tried using a json object which i was able to replace the color with red  
> then the problem which occurred is i could not convert the _string to  
> QueryBuilder_. I used _WrapperQuery_ to convert string i got a encrypted  
> data which i could not track forward it for query processing.
> 
> I have used _BoolQueryBuilder_ for my approach.
> 
> Thanks
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Filter-search-with-drill-down-approach-using-Java-API-tp4044468.html](http://elasticsearch-users.115913.n3.nabble.com/Filter-search-with-drill-down-approach-using-Java-API-tp4044468.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![kavesh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kavesh](https://discuss.elastic.co/u/kavesh)\
**Post date:** [November 19, 2013, 5:17am UTC](https://discuss.elastic.co/t/filter-search-with-drill-down-approach-using-java-api/14446/3 "2013-11-19T05:17:39Z")

</div>

HI Ivan,

Can you please let me know why we should not preserve the QueryBuilder instances? QueryBuilder will always give me result for the selected value.

Can you please give an idea how to go about.

My requirement is if i am searching on title then all results with title should be displayed. if i am adding a filter saying color then the result should be title+color combined result should be displayed.

Thanks

---

<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 19, 2013, 7:07am UTC](https://discuss.elastic.co/t/filter-search-with-drill-down-approach-using-java-api/14446/4 "2013-11-19T07:07:06Z")

</div>

Why not reuse a QueryBuilder instance? Because you will find yourself in  
the exact situation you are in. The Builder design pattern in general is  
not meant for reuse.

Without looking at your code, I am assuming you are adding must clauses to  
a BoolQueryBuilder via the must() method:  
[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java#L53-56](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java#L53-56)

Internally the mustClauses are a list, not some type of map with the field  
name used as a key. There is no remove method, resetting of the clauses, or  
direct accesses to the clauses. That would not be part of the Builder  
design pattern.

Cheers.

Ivan

On Mon, Nov 18, 2013 at 9:17 PM, kavesh [kavesh.shetgaonkar@privail.com](mailto:kavesh.shetgaonkar@privail.com)wrote:

> HI Ivan,
> 
> Can you please let me know why we should not preserve the QueryBuilder  
> instances? QueryBuilder will always give me result for the selected value.
> 
> Can you please give an idea how to go about.
> 
> My requirement is if i am searching on title then all results with title  
> should be displayed. if i am adding a filter saying color then the result  
> should be title+color combined result should be displayed.
> 
> Thanks
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Filter-search-with-drill-down-approach-using-Java-API-tp4044468p4044509.html](http://elasticsearch-users.115913.n3.nabble.com/Filter-search-with-drill-down-approach-using-Java-API-tp4044468p4044509.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![kavesh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kavesh](https://discuss.elastic.co/u/kavesh)\
**Post date:** [November 19, 2013, 10:45am UTC](https://discuss.elastic.co/t/filter-search-with-drill-down-approach-using-java-api/14446/5 "2013-11-19T10:45:14Z")

</div>

Can you please let me know what is the best possible solution that i can use for my approach. As i have tried with lot of procedures and dint come up to the solution yet.

Thanks

---

<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 19, 2013, 5:30pm UTC](https://discuss.elastic.co/t/filter-search-with-drill-down-approach-using-java-api/14446/6 "2013-11-19T17:30:00Z")

</div>

I never said to not use the QueryBuilders\*, \*I said to not RE-use them. Do  
not preserve them or change them after a query, simply create a new one.

--  
Ivan

On Tue, Nov 19, 2013 at 2:45 AM, kavesh [kavesh.shetgaonkar@privail.com](mailto:kavesh.shetgaonkar@privail.com)wrote:

> Thanks Ivan. For the clarification on why not to use QueryBuilder.
> 
> Can you please let me know what is the best possible solution that i can  
> use  
> for my approach. As i have tried with lot of procedures and dint come up to  
> the solution yet.
> 
> Thanks
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Filter-search-with-drill-down-approach-using-Java-API-tp4044468p4044529.html](http://elasticsearch-users.115913.n3.nabble.com/Filter-search-with-drill-down-approach-using-Java-API-tp4044468p4044529.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:06am UTC](https://discuss.elastic.co/t/filter-search-with-drill-down-approach-using-java-api/14446/7 "2017-07-06T02:06:11Z")

</div>


