Advice on building a query dynamically


(Jondow) #1

Hi all,

I'm looking for some advice on how to build a query dynamically. So
what I mean by that is that our web application interface allows the
user to create a filter of the information in our database, which is
effectively an ES query on the indexed data from the database.

Doing this dynamically using the filter criteria that are available to
the user is conceptually simple but I'm not sure how to convert that
to an ES query. We have two options:

  1. Use the Lucene query syntax e.g. hostname:Blah - the problem here
    is determining all the options for Lucene query syntax ... the docs
    page here http://lucene.apache.org/java/3_4_0/queryparsersyntax.html
    doesn't provide clues as to how to accomplish the many options made
    available via the ES Query DSL.

  2. Use the ES DSL. Here the documentation is good, but how to combine
    multiple criteria in a single DSL document becomes more of a
    challenge.

Has anybody had to solve this before? Does anybody have any good
suggestions here?

Thanks in advance,
Darryl Pentz


(Jussi Arpalahti) #2

On 24 October 2011 12:29, Jondow djpentz@gmail.com wrote:

Hi all,

I'm looking for some advice on how to build a query dynamically. So
what I mean by that is that our web application interface allows the
user to create a filter of the information in our database, which is
effectively an ES query on the indexed data from the database.

Doing this dynamically using the filter criteria that are available to
the user is conceptually simple but I'm not sure how to convert that
to an ES query. We have two options:

  1. Use the Lucene query syntax e.g. hostname:Blah - the problem here
    is determining all the options for Lucene query syntax ... the docs
    page here http://lucene.apache.org/java/3_4_0/queryparsersyntax.html
    doesn't provide clues as to how to accomplish the many options made
    available via the ES Query DSL.

  2. Use the ES DSL. Here the documentation is good, but how to combine
    multiple criteria in a single DSL document becomes more of a
    challenge.

Has anybody had to solve this before? Does anybody have any good
suggestions here?

Hi.

Since I used pyes (python elastic search module) this might not be relevant
for you, but this is what I did:

params =
{
"searches" : [
{"TermQuery" :
[
["_type", "doctype"],
["field", "content"]
]
}]
}

search_args = []
for search in params["searches"]:
for term, arg in search.get("TermQuery", []):
search_args.append(pyes.TermQuery(term, arg))
query = pyes.BoolQuery(must=search_args)

Since pyes takes care of generating Query DSL from Query objects, I can
build the query from user input or (like in this case) parameter dictionary.
If you need to stick to javascript, perhaps you need some kind of JSON
templating instead of Query class instances..

Best,
Jussi


(system) #3