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


(mattx) #1

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.


(Clinton Gormley) #2

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' -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 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/

hth

clint


(system) #3