alvgarvilla
alvgarvilla

Reputation: 1086

Complex Lucene queries in ElasticSearch

We've been using Lucene for the last years and we are about to migrate to ElasticSearch. We have lot of Lucene queries (complex ones) that we want to reuse. I've seen in ElasticSearch the "query_string" API (https://www.elastic.co/guide/en/elasticsearch/reference/6.3/query-dsl-query-string-query.html#query-string-syntax). It works pretty good for simple queries, but it doesn't when using functions like spanNear or spanOr. See below a part of a query:

spanNear([spanOr([ADDRESS:13, ADDRESS:13a, ADDRESS:13b, ADDRESS:13c, ADDRESS:13d, ADDRESS:13e, ADDRESS:13f, ADDRESS:13g, ADDRESS:13h, ADDRESS:13i, ADDRESS:13j, ADDRESS:13k, ADDRESS:13l, ADDRESS:13m, ADDRESS:13n, ADDRESS:13r, ADDRESS:13s, ADDRESS:13u, ADDRESS:13v, ADDRESS:13w, ADDRESS:13x, ADDRESS:13y, ADDRESS:13z]), [ADDRESS:carlton, ADDRESS:chalton, ADDRESS:charaton, ADDRESS:charleton, ADDRESS:charlon, ADDRESS:charlson, ADDRESS:charlston, ADDRESS:charlton, ADDRESS:charltons, ADDRESS:charlvon, ADDRESS:charston, ADDRESS:charton, ADDRESS:chorlton, ADDRESS:harlton, ADDRESS:sharlton]))

Do you know which is the best way of using these kind of queries without rewriting them into ES format? We use the Lucene Java libraries for generating the queries in case it helps. Thanks in advance.

Upvotes: 2

Views: 795

Answers (1)

femtoRgon
femtoRgon

Reputation: 33341

It sounds like you are getting the toString from your query, and hoping the ElasticSearch will parse it. Bad news, there is nothing in Elasticsearch which will parse the output of Query.toString. There is also nothing in Lucene that will parse it. For simple queries it may spit out a parseable query string, but that is never guaranteed. Query.toString() is for debugging, not serializing.

To run these queries in elasticsearch, the API supports the full compliment of span queries, including span_near and span_or

There are also query parsers which do have syntax supporting span queries such as Surround and Complex Phrase. ElasticSearch doesn't have them out of the box, though, but could be an option if you are willing to do the leg work to put together a plugin to use them in ElasticSearch (a process I've seen references to, and it looks quite manageable, but which I am not really familiar with).

Upvotes: 1

Related Questions