# Need some help translating a string query to Query DSL and Java API

**URL:** <https://discuss.elastic.co/t/need-some-help-translating-a-string-query-to-query-dsl-and-java-api/3571>\
**Category:** Elasticsearch\
**Created:** [November 15, 2010, 4:39pm UTC](https://discuss.elastic.co/t/need-some-help-translating-a-string-query-to-query-dsl-and-java-api/3571 "2010-11-15T16:39:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattx](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@mattx](https://discuss.elastic.co/u/mattx)\
**Post date:** [November 15, 2010, 4:39pm UTC](https://discuss.elastic.co/t/need-some-help-translating-a-string-query-to-query-dsl-and-java-api/3571/1 "2010-11-15T16:39:52Z")

</div>

I just checked out lucene and then quickly noticed this cool project and jumped immediately to it.

First of all great work throughout.

I have a few of very basic questions.

I'm trying to use es to provide search of email contacts by address and name. This will be limited by user.

So the consumer might search for "matt" and I want to return any contacts with the name "Matthew X"or the address "mattx@wherever.com"

In the solr test page i was able to make the query "owner:asdf1234 AND (name:matt\* OR address:matt\*)"

I can also do this with the es java api like so

setQuery(QueryBuilders.queryString("owner:asdf1234 AND (name:_matt_ OR locator:_matt_")))  
.execute().actionGet();

My index also has more info in it than these three fields.

1. How do I write the DSL query for this string? I find it pretty confusing.
2. How do I write that same DSL query in the java API?
3. How do I limit the returned fields to just name and address?
4. How do I write that part of the query with the java API?

Thanks and sorry for this overloaded post.

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [November 17, 2010, 10:06am UTC](https://discuss.elastic.co/t/need-some-help-translating-a-string-query-to-query-dsl-and-java-api/3571/2 "2010-11-17T10:06:02Z")

</div>

Hi Matt

> In the solr test page i was able to make the query "owner:asdf1234 AND  
> (name:matt\* OR address:matt\*)"
> 
> 1. How do I write the DSL query for this string? I find it pretty confusing.

This is one way you could write it:

curl -XGET '[http://127.0.0.1:9200/myindex/contact/\_search](http://127.0.0.1:9200/myindex/contact/_search)' -d '  
{  
"fields" : ["name","address"],  
"query" : {  
"filtered" : {  
"filter" : {  
"term" : {  
"owner" : "asdf1234"  
}  
},  
"query" : {  
"query\_string" : {  
"query" : "matt\*",  
"fields" : ["name","address"],  
}  
}  
}  
}  
}  
'

> 1. How do I limit the returned fields to just name and address?

As above, I specify in "fields" which fields to return. However, that  
does predispose that the mapping for those fields specifies that they  
should be stored.

(see [Field data types | Elasticsearch Guide [8.11] | Elastic](http://www.elasticsearch.com/docs/elasticsearch/mapping/core_types/)  
for more details about mapping)

Also, note that I'm using a 'term' filter for owner. What that means is  
that the "asdf1234" will not be analyzed (ie broken up into terms).  
Instead, it will assume that "asdf1234" is a term in itself, and is  
different from eg "ASDF1234".

See here for more about analysis.  
[http://www.elasticsearch.com/docs/elasticsearch/index\_modules/analysis/](http://www.elasticsearch.com/docs/elasticsearch/index_modules/analysis/)

hth

clint

---

<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:16am UTC](https://discuss.elastic.co/t/need-some-help-translating-a-string-query-to-query-dsl-and-java-api/3571/3 "2017-07-06T04:16:22Z")

</div>

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


