# Elastic Search Java AND Text Query

**URL:** https://discuss.elastic.co/t/elastic-search-java-and-text-query/12719
**Category:** Elasticsearch
**Created:** [July 9, 2013, 12:35pm UTC](https://discuss.elastic.co/t/elastic-search-java-and-text-query/12719 "2013-07-09T12:35:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fegeilert](https://avatars.discourse-cdn.com/v4/letter/f/fbc32d/32.png) [@fegeilert](https://discuss.elastic.co/u/fegeilert)
#### Post date: [July 9, 2013, 12:35pm UTC](https://discuss.elastic.co/t/elastic-search-java-and-text-query/12719/1 "2013-07-09T12:35:57Z")

</div>

Hello,  
I am currently working on a project using elastic search.  
The code for this project is written in java.  
My problem is the following:  
I want to create two text (sub-)queries in one query using  
a Boolean query. However the two queries are always  
combined using a logical OR function. Is there a way to  
combine the queries using AND?

The current code looks somewhat like the following:  
SearchResponse response = client.prepareSearch("myindex")  
.setTypes("mymapping")  
.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
.setQuery(QueryBuilders.boolQuery()  
.must(QueryBuilders.textQuery("field1", "value1"))  
.must(QueryBuilders.textQuery("field2", "value2")))  
.setFrom(start).setSize(offset)  
.addField("field1")  
.addField("field2")  
.execute()  
.actionGet();

Thanks in advance!

--  
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: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 9, 2013, 1:26pm UTC](https://discuss.elastic.co/t/elastic-search-java-and-text-query/12719/2 "2013-07-09T13:26:53Z")

</div>

What do you mean?  
With the code you provided, you need to satisfy both conditions. So it is to me somehow a AND query, right?

On a side note, prefer using matchQuery instead of textQuery (deprecated).

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 9 juil. 2013 à 14:35, [fegeilert@googlemail.com](mailto:fegeilert@googlemail.com) a écrit :

> Hello,  
> I am currently working on a project using Elasticsearch.  
> The code for this project is written in java.  
> My problem is the following:  
> I want to create two text (sub-)queries in one query using  
> a Boolean query. However the two queries are always  
> combined using a logical OR function. Is there a way to  
> combine the queries using AND?
> 
> The current code looks somewhat like the following:  
> SearchResponse response = client.prepareSearch("myindex")  
> .setTypes("mymapping")  
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery(QueryBuilders.boolQuery()  
> .must(QueryBuilders.textQuery("field1", "value1"))  
> .must(QueryBuilders.textQuery("field2", "value2")))  
> .setFrom(start).setSize(offset)  
> .addField("field1")  
> .addField("field2")  
> .execute()  
> .actionGet();
> 
> Thanks in advance!
> 
> --  
> 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:27am UTC](https://discuss.elastic.co/t/elastic-search-java-and-text-query/12719/3 "2017-07-06T02:27:28Z")

</div>


