ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Using the Lucene Query Parser Without Lucene
Pages: 1, 2, 3, 4

Query Interpreter

Lucene parses the query string and produces an object representation; this is the input for QueryInterpreter. Object representation is a composition made from types extending Lucene's Query class. Each reflects part of the query string. The following table shows selected query types. Some represent restrictions; others are logical conditions.

Query Type Description
TermQuery Matches documents containing a specific term.
BooleanQuery Represents combinations of other queries.
WildcardQuery Criteria with wildcards like * or ?.
PhraseQuery Sequence of terms.
PrefixQuery Terms with a prefix.
RangeQuery Property value range.

For example, the parser reads "name:John" and creates TermQuery. Now you know everything that is necessary to start implementing QueryInterpreter.

First, we create a template method to understand what information the TermQuery type represents:

import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;

class QueryInterpreter {

    void parse(Query query) {
        if (query instanceof TermQuery) {
            Term term = query.getTerm();
            String fieldName = term.field();
            String fieldText = term.text();
            System.out.println("TermQuery ["+fieldName+":"+fieldText+"]");
        } else {
            throw new IllegalArgumentException("Unsupported Query type [" + query.getClass() + "]");
        }
    }
}

As you see, TermQuery consists of the name field and the expected value. In the preceding code, the QueryInterpreter is able to recognize only simple queries, like "name:John." More complicated restrictions are not supported in the example, but of course it could be extended to support more complex forms. However, that is beyond the scope of this article.

Our next step is to invoke QueryBuilder. We start with an interface:

import org.apache.lucene.search.TermQuery;

public interface IQueryBuilder {
    void termQuery(TermQuery query);
}

import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;

public class QueryInterpreter {

    private final IQueryBuilder queryBuilder;

    public QueryInterpreter(IQueryBuilder queryBuilder) {
        this.queryBuilder = queryBuilder;
    }

    private void interpret(TermQuery query) {
        this.queryBuilder.termQuery(query);
    }

    public void parse(Query query) {
        if (query instanceof TermQuery) {
            System.out.println("TermQuery");
            interpret((TermQuery) query);
        } else {
            throw new IllegalArgumentException("Unsupported Query type [" + query.getClass() + "]");
        }
    }
}

For every TermQuery occurrence, QueryInterpreter invokes IQueryBuilder.termQuery method. This is somehow similar to how SAX parser works with XML input events.

Pages: 1, 2, 3, 4

Next Pagearrow