# Lucene style queries from the Java API?

**URL:** <https://discuss.elastic.co/t/lucene-style-queries-from-the-java-api/3406>\
**Category:** Elasticsearch\
**Created:** [October 4, 2010, 4:23pm UTC](https://discuss.elastic.co/t/lucene-style-queries-from-the-java-api/3406 "2010-10-04T16:23:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@Jake](https://discuss.elastic.co/u/Jake)\
**Post date:** [October 4, 2010, 4:23pm UTC](https://discuss.elastic.co/t/lucene-style-queries-from-the-java-api/3406/1 "2010-10-04T16:23:20Z")

</div>

I'm using version 0.11 and the Java API to search on my 'title' field like so:

SearchResponse response = client.prepareSearch("documents")  
.setQuery(QueryBuilders.termQuery("title", "jakarta apache"))  
.execute()  
.actionGet();

How would I represent the following Lucene style query in the Java API?

title:"jakarta apache" NOT title:"Apache Lucene"

[jake]

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [October 6, 2010, 6:08am UTC](https://discuss.elastic.co/t/lucene-style-queries-from-the-java-api/3406/2 "2010-10-06T06:08:39Z")

</div>

Hi,  
I think you should be fine using QueryBulders.boolQuery()  
Regards,  
Lukas

On Mon, Oct 4, 2010 at 6:23 PM, Jake [jakebartlett@gmail.com](mailto:jakebartlett@gmail.com) wrote:

> I'm using version 0.11 and the Java API to search on my 'title' field like  
> so:
> 
> SearchResponse response = client.prepareSearch("documents")  
> .setQuery(QueryBuilders.termQuery("title", "jakarta  
> apache"))  
> .execute()  
> .actionGet();
> 
> How would I represent the following Lucene style query in the Java API?
> 
> title:"jakarta apache" NOT title:"Apache Lucene"
> 
> ## [jake]
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Lucene-style-queries-from-the-Java-API-tp1630758p1630758.html](http://elasticsearch-users.115913.n3.nabble.com/Lucene-style-queries-from-the-Java-API-tp1630758p1630758.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 6, 2010, 2:42pm UTC](https://discuss.elastic.co/t/lucene-style-queries-from-the-java-api/3406/3 "2010-10-06T14:42:35Z")

</div>

Hey,

You have two options. The first is to use a queryString or field queries  
and provide the query you provided. It will be parsed into the proper Lucene  
Query structure. Another optino is to use what Lukas suggested and build  
that query yourself using the boolQuery option (which maps to Lucene  
BooleanQuery). All the queries are listed in the query\_dsl page (  
[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/)), and  
you can find all of them in the QueryBuilders and FilterBuilders factories.

-shay.banon

On Wed, Oct 6, 2010 at 8:08 AM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:

> Hi,  
> I think you should be fine using QueryBulders.boolQuery()  
> Regards,  
> Lukas
> 
> On Mon, Oct 4, 2010 at 6:23 PM, Jake [jakebartlett@gmail.com](mailto:jakebartlett@gmail.com) wrote:
> 
> > I'm using version 0.11 and the Java API to search on my 'title' field like  
> > so:
> > 
> > SearchResponse response = client.prepareSearch("documents")  
> > .setQuery(QueryBuilders.termQuery("title", "jakarta  
> > apache"))  
> > .execute()  
> > .actionGet();
> > 
> > How would I represent the following Lucene style query in the Java API?
> > 
> > title:"jakarta apache" NOT title:"Apache Lucene"
> > 
> > ## [jake]
> > 
> > View this message in context:  
> > [http://elasticsearch-users.115913.n3.nabble.com/Lucene-style-queries-from-the-Java-API-tp1630758p1630758.html](http://elasticsearch-users.115913.n3.nabble.com/Lucene-style-queries-from-the-Java-API-tp1630758p1630758.html)  
> > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 4:18am UTC](https://discuss.elastic.co/t/lucene-style-queries-from-the-java-api/3406/4 "2017-07-06T04:18:21Z")

</div>

Closed on July 6, 2017, 4:18am UTC


