# Trouble formulating a query with Java API

**URL:** https://discuss.elastic.co/t/trouble-formulating-a-query-with-java-api/20982
**Category:** Elasticsearch
**Created:** [November 28, 2014, 6:11pm UTC](https://discuss.elastic.co/t/trouble-formulating-a-query-with-java-api/20982 "2014-11-28T18:11:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Maarten\_Roosendaal](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@Maarten\_Roosendaal](https://discuss.elastic.co/u/Maarten_Roosendaal)
#### Post date: [November 28, 2014, 6:11pm UTC](https://discuss.elastic.co/t/trouble-formulating-a-query-with-java-api/20982/1 "2014-11-28T18:11:59Z")

</div>

Hi,

I have the following (json) query i use:  
{  
"fields": ["ID","ID2"],  
"query": {  
"filtered": {  
"query": {  
"multi\_match": {  
"query": "",  
"fields": ["FIELD1.not\_analyzed","FIELD2.partial"]  
}  
},  
"filter": {  
"bool": {  
"must": [  
{ "term" : { "FIELD2": "No" } },  
{ "term" : {"FIELD3": "Yes" } }  
]  
}  
}  
}  
},  
"sort": [  
{  
"SCORE": {  
"order": "desc"  
}  
}  
]  
}

but my attempts a building the same query with the Java API hasn't been  
fruitful.The goal is to search for a match based on a searchterm in several  
fields and some fields are more important than others.

I know the basic setup:

.client  
.prepareSearch()  
.setSearchType(SearchType.QUERY\_AND\_FETCH)  
.setQuery(QueryBuilders.multiMatchQuery(searchterm, "FIELD1.not\_analyzed",  
"FIELD2.partial"))  
.addSort("SCORE", SortOrder.DESC)  
.setSize(10)  
.execute()  
.actionGet();

but there are no results while the json query has a returnvalue. So 2  
questions:

1. i could use some help formulating the right Java query
2. why is the json return stuff and the java query not?

Thanks,  
Maarten

--  
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/166a43b2-d92c-4307-abed-2ce95ae87fb2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/166a43b2-d92c-4307-abed-2ce95ae87fb2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@roger](https://discuss.elastic.co/u/roger)
#### Post date: [December 1, 2014, 1:20pm UTC](https://discuss.elastic.co/t/trouble-formulating-a-query-with-java-api/20982/2 "2014-12-01T13:20:45Z")

</div>

You can use the toString() method of the SearchRequestBuilder to see the  
generated query. With your example it was:

{  
"size" : 10,  
"query" : {  
"multi\_match" : {  
"query" : "searchterm",  
"fields" : ["FIELD1.not\_analyzed", "FIELD2.partial"]  
}  
},  
"sort" : [ {  
"SCORE" : {  
"order" : "desc"  
}  
} ]  
}

This query looks ok. Are you not receiving any results? Not even the  
"total" value?

2014-11-28 16:11 GMT-02:00 Maarten Roosendaal [mroosendaal14@gmail.com](mailto:mroosendaal14@gmail.com):

> Hi,
> 
> I have the following (json) query i use:  
> {  
> "fields": ["ID","ID2"],  
> "query": {  
> "filtered": {  
> "query": {  
> "multi\_match": {  
> "query": "",  
> "fields": ["FIELD1.not\_analyzed","FIELD2.partial"]  
> }  
> },  
> "filter": {  
> "bool": {  
> "must": [  
> { "term" : { "FIELD2": "No" } },  
> { "term" : {"FIELD3": "Yes" } }  
> ]  
> }  
> }  
> }  
> },  
> "sort": [  
> {  
> "SCORE": {  
> "order": "desc"  
> }  
> }  
> ]  
> }
> 
> but my attempts a building the same query with the Java API hasn't been  
> fruitful.The goal is to search for a match based on a searchterm in several  
> fields and some fields are more important than others.
> 
> I know the basic setup:
> 
> .client  
> .prepareSearch()  
> .setSearchType(SearchType.QUERY\_AND\_FETCH)  
> .setQuery(QueryBuilders.multiMatchQuery(searchterm, "FIELD1.not\_analyzed",  
> "FIELD2.partial"))  
> .addSort("SCORE", SortOrder.DESC)  
> .setSize(10)  
> .execute()  
> .actionGet();
> 
> but there are no results while the json query has a returnvalue. So 2  
> questions:
> 
> 1. i could use some help formulating the right Java query
> 2. why is the json return stuff and the java query not?
> 
> Thanks,  
> Maarten
> 
> --  
> 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/166a43b2-d92c-4307-abed-2ce95ae87fb2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/166a43b2-d92c-4307-abed-2ce95ae87fb2%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/166a43b2-d92c-4307-abed-2ce95ae87fb2%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/166a43b2-d92c-4307-abed-2ce95ae87fb2%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/CAJp2532Z8bataicKD%2BrUECd4yoJkXZd47MMVssgoE%3DK8opXhCg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAJp2532Z8bataicKD%2BrUECd4yoJkXZd47MMVssgoE%3DK8opXhCg%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:46am UTC](https://discuss.elastic.co/t/trouble-formulating-a-query-with-java-api/20982/3 "2017-07-06T00:46:50Z")

</div>


