Example usage for org.apache.lucene.search BooleanQuery toString

List of usage examples for org.apache.lucene.search BooleanQuery toString

Introduction

In this page you can find the example usage for org.apache.lucene.search BooleanQuery toString.

Prototype

@Override
public String toString(String field) 

Source Link

Document

Prints a user-readable version of this query.

Usage

From source file:aos.lucene.search.msc.QueryParserTest.java

License:Apache License

public void testToString() throws Exception {
    BooleanQuery query = new BooleanQuery();
    query.add(new FuzzyQuery(new Term("field", "kountry")), BooleanClause.Occur.MUST);
    query.add(new TermQuery(new Term("title", "western")), BooleanClause.Occur.SHOULD);
    assertEquals("both kinds", "+kountry~0.5 title:western", query.toString("field"));
}

From source file:org.mulgara.resolver.lucene.FullTextStringIndex.java

License:Mozilla Public License

/**
 * Find a string within the fulltext string pool. The search is based on the
 * {@link StandardAnalyzer} used to add the string.
 *
 * @param subject subject; may be null/*from w  w  w.j a  va  2s .c o m*/
 * @param predicate predicate; may be null
 * @param literal literal to be searched via the analyzer; may be null
 * @return Object containing the hits
 * @throws FullTextStringIndexException IOException occurs on reading index
 */
public Hits find(String subject, String predicate, String literal) throws FullTextStringIndexException {
    Query query;

    if (subject == null && predicate == null && literal == null) {
        query = new MatchAllDocsQuery();
    } else {
        BooleanQuery bQuery = new BooleanQuery();
        query = bQuery;

        // debug logging
        if (logger.isDebugEnabled()) {
            logger.debug("Searching the fulltext string index pool with  subject :" + subject + " predicate :"
                    + predicate + " literal :" + literal);
        }

        if (subject != null) {
            TermQuery tSubject = new TermQuery(new Term(SUBJECT_KEY, subject));
            if (literal != null)
                tSubject.setBoost(0); // if scoring, don't affect the score
            bQuery.add(tSubject, BooleanClause.Occur.MUST);
        }

        if (predicate != null) {
            TermQuery tPredicate = new TermQuery(new Term(PREDICATE_KEY, predicate));
            if (literal != null)
                tPredicate.setBoost(0); // if scoring, don't affect the score
            bQuery.add(tPredicate, BooleanClause.Occur.MUST);
        }

        if (literal != null) {
            Query qliteral = null;

            try {
                // Are we performing a reverse string lookup?
                if (enableReverseTextIndex && isLeadingWildcard(literal)) {
                    literal = reverseLiteralSearch(literal);
                    QueryParser parser = new QueryParser(LUCENE_VERSION, REVERSE_LITERAL_KEY, analyzer);
                    qliteral = parser.parse(literal);
                } else {
                    QueryParser parser = new QueryParser(LUCENE_VERSION, LITERAL_KEY, analyzer);
                    qliteral = parser.parse(literal);
                }
            } catch (ParseException ex) {
                logger.error("Unable to parse query '" + literal + "'", ex);
                throw new FullTextStringIndexException("Unable to parse query '" + literal + "'", ex);
            }

            bQuery.add(qliteral, BooleanClause.Occur.MUST);
        }

        // debug logging
        if (literal != null && logger.isDebugEnabled()) {
            if ((literal.startsWith("*") || literal.startsWith("?")) && enableReverseTextIndex) {
                logger.debug("Searching the fulltext string index pool with parsed query as "
                        + bQuery.toString(REVERSE_LITERAL_KEY));
            } else {
                logger.debug("Searching the fulltext string index pool with parsed query as "
                        + bQuery.toString(LITERAL_KEY));
            }
        }
    }

    //Perform query
    return find(query);
}