On Sat, Jan 25, 2014 at 4:29 AM, Olivier Binda <olivier.bi...@wanadoo.fr> wrote:
> I would like to serialize a query into a string (A) and then to unserialize
> it back into a query (B)
>
> I guess that a solution is
> A) query.toString()
> B) StandardQueryParser().parse(query,"")

If your custom query parser uses the new query parser framework, then
the easiest thing to do is to use QueryNode everywhere.

When you are about to do a search, convert to Query by passing it
through the latter parts of the QueryParser framework (the processor
pipeline and the node factory.)

To convert QueryNode to String, write a "SyntaxFormatter" (the missing
interface of the new query parser framework) with formatters for each
type of node which you can expect to occur in your queries. Since
default operator behaviour is implemented in the processor pipeline,
it becomes a non-issue.

The strings you get still won't be exactly what you might have written
originally, but the way we look at is that it reformats the query to
your preferred style.

The hardest part I found was formatting fields sensibly, because the
QueryNode tree structure doesn't quite match what you're writing in
the string queries.

e.g. you might normally write this:
  text:( cat or dog )

The SyntaxParser will probably convert this to something like this:
  GroupQueryNode(
    BooleanQueryNode(OR,
      FieldQueryNode(text:cat),
      FieldQueryNode(text:dog)))

If you write your SyntaxFormatter naïvely, you will get:
  text:cat or text:dog

With some trickery, you can detect when a BooleanQueryNode contains
multiple queries with the same field and move the field and colon
outside the parentheses when you format it, to get back the more
compact style.

Another (possibly more pure) solution would be to make your
SyntaxParser output an AST closer to your real syntax and then use
that everywhere instead. Then you would have to write a transform from
that to QueryNode and then use the same method to get from QueryNode
to Query.

TX

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org
For additional commands, e-mail: java-user-h...@lucene.apache.org

Reply via email to