Matt
Matt

Reputation: 1038

Serializing a lucene query into JSON?

Is there a best practice for serializing Lucene queries into JSON format? I saw elastic search query dsl, but it looks like it strays from lucene terminology. Also, Lucene appears to be moving away from maintaining serialization code.

I am looking to have a "standard" format in JSON. I need to be able to save a query, but when users are saving queries on the web UI, they are not entering title:Matrix. We also have to be able to search the saved searches, then edit the saved searches. It would be nice if there was a standard json format for representing a query. Specifically a lucene query.

Upvotes: 0

Views: 1275

Answers (2)

mindas
mindas

Reputation: 26713

I know very little of Elasticsearch but I'm guessing they are doing this (also) because they support extra query functionality other than Lucene normally does.

If you just need to marshall/unmarshall the query, just treat it whole as a single unescaped string. If you want to break it down to key-value (field name/value) elements, don't forget you can have conjuction/disjunction/etc. So this is not trivial.

As for your comment on serialization code - don't think this applies to queries. Query has abstract toString(String field) method which each query subclass should implement and provide a parseable string. toString() just calls that - and this hasn't change for a long while, nor I see this changing as Query does not provide any other way of getting String representation so changing it would break a lot of code.

Upvotes: 1

omu_negru
omu_negru

Reputation: 4770

Your standard query will have a tree-like structure, so i think translating that to Json should be trivial. Just create a recursive method that calls itself for boolean or dismax queries. The rest of the queries are (i think) terminal ones

Upvotes: 0

Related Questions