[ 
https://issues.apache.org/jira/browse/LUCENE-2868?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12981774#action_12981774
 ] 

Earwin Burrfoot commented on LUCENE-2868:
-----------------------------------------

We here use an intermediate query AST, with a number of walkers that do synonym 
substitution, optimization, caching, rewriting for multiple fields, and finally 
- generating a tree of Lucene Queries.

I can share a generic reflection-based visitor that's somewhat more handy than 
default visitor pattern in java.
Usage looks roughly like: 
{code}
class ToStringWalker extends DispatchingVisitor<String> { // String here stands 
for the type of walk result
  String visit(TermQuery q) {
    return "{term: " + q.getTerm() + "}";
  }

  String visit(BooleanQuery q) {
    StringBuffer buf = new StringBuffer();
    buf.append("{boolean: ");
    for (BooleanQuery.Clause clause: q.clauses()) {
      buf.append(dispatch(clause.getQuery()).append(", "); // Here we 
    }
    buf.append("}");
    return buf.toString();
  }

  String visit(SpanQuery q) { // Runs for all SpanQueries
    .....
  }

  String visit(Query q) { // Runs for all Queries not covered by a more exact 
visit() method 
    ......
  }
}

Query query = ...;
String stringRepresentation = new ToStringWalker().dispatch(query);
{code}

dispatch() checks its parameter runtime type, picks a visit()'s most close 
overload (according to java rules for compile-time overloaded method 
resolution), and invokes it.

> It should be easy to make use of TermState; rewritten queries should be 
> shared automatically
> --------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-2868
>                 URL: https://issues.apache.org/jira/browse/LUCENE-2868
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: Query/Scoring
>            Reporter: Karl Wright
>         Attachments: query-rewriter.patch
>
>
> When you have the same query in a query hierarchy multiple times, tremendous 
> savings can now be had if the user knows enough to share the rewritten 
> queries in the hierarchy, due to the TermState addition.  But this is clumsy 
> and requires a lot of coding by the user to take advantage of.  Lucene should 
> be smart enough to share the rewritten queries automatically.
> This can be most readily (and powerfully) done by introducing a new method to 
> Query.java:
> Query rewriteUsingCache(IndexReader indexReader)
> ... and including a caching implementation right in Query.java which would 
> then work for all.  Of course, all callers would want to use this new method 
> rather than the current rewrite().

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


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

Reply via email to