# Need help on QueryBuilder

**URL:** <https://discuss.elastic.co/t/need-help-on-querybuilder/22578>\
**Category:** Elasticsearch\
**Created:** [March 9, 2015, 7:13am UTC](https://discuss.elastic.co/t/need-help-on-querybuilder/22578 "2015-03-09T07:13:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vijayakumari\_B\_N](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@Vijayakumari\_B\_N](https://discuss.elastic.co/u/Vijayakumari_B_N)\
**Post date:** [March 9, 2015, 7:13am UTC](https://discuss.elastic.co/t/need-help-on-querybuilder/22578/1 "2015-03-09T07:13:26Z")

</div>

Hi,

I want to build Query for the requirement where i have 3 check-boxes in  
input(For 3 different attributes), i have to search input text matching in  
the selected check-box.

I want to dynamically build the query if attribute1 is selected search in  
attribute1, if attribute2 is selected search in attribute2 etc..

I am trying to build query, which matches the input text with must in both  
the attributes, it is not returning any results. If i use Multimatch, i am  
able to fetch results.

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

```
        if (searchIn.contains("keywords")) {
        boolQueryBuilder.must(QueryBuilders.matchQuery("keywords", 

```

inputText));  
}  
if (searchIn.contains("symptom")) {  
boolQueryBuilder.must(QueryBuilders.matchQuery("symptom",  
inputText));  
}

Match Query :  
{  
"bool" : {  
"must" : [ {  
"match" : {  
"keywords" : {  
"query" : "Holi",  
"type" : "boolean"  
}  
}  
}, {  
"match" : {  
"symptom" : {  
"query" : "Holi",  
"type" : "boolean"  
}  
}  
} ]  
}  
}

Multimatch query :  
{  
"multi\_match" : {  
"query" : "Holi",  
"fields" : ["keywordsField", "symptomField", ""]  
}

Can someone please help me.

Thanks,  
Vijaya

--  
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/3d4d8dc6-dc5e-428c-87a5-fc893a7d5e59%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3d4d8dc6-dc5e-428c-87a5-fc893a7d5e59%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Nh\_t\_Quang\_Phan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nh_t_quang_phan/32/843_2.png) [@Nh\_t\_Quang\_Phan](https://discuss.elastic.co/u/Nh_t_Quang_Phan)\
**Post date:** [March 10, 2015, 10:33am UTC](https://discuss.elastic.co/t/need-help-on-querybuilder/22578/2 "2015-03-10T10:33:11Z")

</div>

"Must" query like AND condition. Your query look like : "keywords" field  
must match "Holi" AND "symptom" field must match "Holi". If you want OR  
condition, use "Should" query instead.

On Monday, March 9, 2015 at 2:13:26 PM UTC+7, Vijayakumari B N wrote:

> Hi,
> 
> I want to build Query for the requirement where i have 3 check-boxes in  
> input(For 3 different attributes), i have to search input text matching in  
> the selected check-box.
> 
> I want to dynamically build the query if attribute1 is selected search in  
> attribute1, if attribute2 is selected search in attribute2 etc..
> 
> I am trying to build query, which matches the input text with must in both  
> the attributes, it is not returning any results. If i use Multimatch, i am  
> able to fetch results.
> 
> BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
> 
> ```
> if (searchIn.contains("keywords")) {
> boolQueryBuilder.must(QueryBuilders.matchQuery("keywords", 
> 
> ```
> 
> inputText));  
> }  
> if (searchIn.contains("symptom")) {  
> boolQueryBuilder.must(QueryBuilders.matchQuery("symptom",  
> inputText));  
> }
> 
> Match Query :  
> {  
> "bool" : {  
> "must" : [ {  
> "match" : {  
> "keywords" : {  
> "query" : "Holi",  
> "type" : "boolean"  
> }  
> }  
> }, {  
> "match" : {  
> "symptom" : {  
> "query" : "Holi",  
> "type" : "boolean"  
> }  
> }  
> } ]  
> }  
> }
> 
> Multimatch query :  
> {  
> "multi\_match" : {  
> "query" : "Holi",  
> "fields" : ["keywordsField", "symptomField", ""]  
> }
> 
> Can someone please help me.
> 
> Thanks,  
> Vijaya

--  
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/6e4b0a3d-7d06-4e07-a9bf-4debb879f275%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6e4b0a3d-7d06-4e07-a9bf-4debb879f275%40googlegroups.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:27am UTC](https://discuss.elastic.co/t/need-help-on-querybuilder/22578/3 "2017-07-06T00:27:38Z")

</div>

Closed on July 6, 2017, 12:27am UTC


