# Java Query API

**URL:** <https://discuss.elastic.co/t/java-query-api/8433>\
**Category:** Elasticsearch\
**Created:** [July 17, 2012, 10:41am UTC](https://discuss.elastic.co/t/java-query-api/8433 "2012-07-17T10:41:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duy\_Do](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/duy_do/32/18114_2.png) [@Duy\_Do](https://discuss.elastic.co/u/Duy_Do)\
**Post date:** [July 17, 2012, 10:41am UTC](https://discuss.elastic.co/t/java-query-api/8433/1 "2012-07-17T10:41:40Z")

</div>

Hi,

I'm very newbie to ElasticSearch. I use Java API to build the bool query as  
following:

{  
"bool" : {  
"must" : [ {  
"text" : {  
"content" : {  
"query" : "404 get",  
"type" : "phrase",  
"operator" : "AND"  
}  
}  
}, {  
"terms" : {  
"host" : ["localhost"]  
}  
} ]  
}  
}

I got no doc if doing the query with Java API but doing the query with  
curl, some docs returned.

```
SearchResponse response = client.prepareSearch(getIndices())
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setQuery(query).addFields(returnFields()).setFrom(from)
                .setSize(size).execute().actionGet();

```

What's wrong with my java code?

Thanks,  
Duy

---

<div class="post-metadata">

**Author:** ![Vinicius\_Carvalho](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinicius_carvalho/32/770_2.png) [@Vinicius\_Carvalho](https://discuss.elastic.co/u/Vinicius_Carvalho)\
**Post date:** [July 17, 2012, 2:35pm UTC](https://discuss.elastic.co/t/java-query-api/8433/2 "2012-07-17T14:35:24Z")

</div>

try this instead:

QueryBuilder query = boolQuery().must(textQuery("content", "404 get"  
).type(Type.PHRASE).operator(Operator.AND)).should(termsQuery("host",  
"localhost"));

SearchResponse response = client.prepareSearch(getIndices())  
.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
.setQuery(query).addFields(returnFields()).setFrom(from)  
.setSize(size).execute().actionGet();  
Cheers

On Tuesday, July 17, 2012 6:41:40 AM UTC-4, Duy Do wrote:

> Hi,
> 
> I'm very newbie to Elasticsearch. I use Java API to build the bool query  
> as following:
> 
> {  
> "bool" : {  
> "must" : [ {  
> "text" : {  
> "content" : {  
> "query" : "404 get",  
> "type" : "phrase",  
> "operator" : "AND"  
> }  
> }  
> }, {  
> "terms" : {  
> "host" : ["localhost"]  
> }  
> } ]  
> }  
> }
> 
> I got no doc if doing the query with Java API but doing the query with  
> curl, some docs returned.
> 
> ```
> SearchResponse response = client.prepareSearch(getIndices())
> .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
> 
> ```
> 
> .setQuery(query).addFields(returnFields()).setFrom(from)  
> .setSize(size).execute().actionGet();
> 
> What's wrong with my java code?
> 
> Thanks,  
> Duy

---

<div class="post-metadata">

**Author:** ![Duy\_Do](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/duy_do/32/18114_2.png) [@Duy\_Do](https://discuss.elastic.co/u/Duy_Do)\
**Post date:** [July 17, 2012, 4:48pm UTC](https://discuss.elastic.co/t/java-query-api/8433/3 "2012-07-17T16:48:22Z")

</div>

It works. Thanks Vinicius.

On Tue, Jul 17, 2012 at 9:35 PM, Vinicius Carvalho \<  
[viniciusccarvalho@gmail.com](mailto:viniciusccarvalho@gmail.com)\> wrote:

> try this instead:
> 
> QueryBuilder query = boolQuery().must(textQuery("content", "404 get"  
> ).type(Type.PHRASE).operator(Operator.AND)).should(termsQuery("host",  
> "localhost"));
> 
> SearchResponse response = client.prepareSearch(\*\*getIndices())  
> .setSearchType(SearchType.DFS\_**QUERY\_THEN\_FETCH)  
> .setQuery(query).addFields(**  
> returnFields()).setFrom(from)  
> .setSize(size).execute().\*\*actionGet();  
> Cheers
> 
> On Tuesday, July 17, 2012 6:41:40 AM UTC-4, Duy Do wrote:
> 
> > Hi,
> > 
> > I'm very newbie to Elasticsearch. I use Java API to build the bool query  
> > as following:
> > 
> > {  
> > "bool" : {  
> > "must" : [ {  
> > "text" : {  
> > "content" : {  
> > "query" : "404 get",  
> > "type" : "phrase",  
> > "operator" : "AND"  
> > }  
> > }  
> > }, {  
> > "terms" : {  
> > "host" : ["localhost"]  
> > }  
> > } ]  
> > }  
> > }
> > 
> > I got no doc if doing the query with Java API but doing the query with  
> > curl, some docs returned.
> > 
> > ```
> > SearchResponse response = client.prepareSearch(**getIndices())
> > .setSearchType(SearchType.DFS_**QUERY_THEN_FETCH)
> > .setQuery(query).addFields(**
> > 
> > ```
> > 
> > returnFields()).setFrom(from)  
> > .setSize(size).execute().\*\*actionGet();
> > 
> > What's wrong with my java code?
> > 
> > Thanks,  
> > Duy

--  
Duy Do

---

<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:20am UTC](https://discuss.elastic.co/t/java-query-api/8433/4 "2017-07-06T03:20:06Z")

</div>


