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

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

Introduction

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

Prototype

BooleanQuery

Source Link

Usage

From source file:com.sindicetech.siren.qparser.keyword.builders.BooleanQueryNodeBuilder.java

License:Open Source License

public Query build(final QueryNode queryNode) throws QueryNodeException {
    final BooleanQueryNode booleanNode = (BooleanQueryNode) queryNode;

    final BooleanQuery bQuery = new BooleanQuery();
    final List<QueryNode> children = booleanNode.getChildren();

    if (children != null) {

        for (final QueryNode child : children) {
            final Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);

            if (obj != null) {
                final Query query = (Query) obj;

                try {
                    final QueryNode mod;
                    if (child instanceof DatatypeQueryNode) {
                        mod = ((DatatypeQueryNode) child).getChild();
                    } else {
                        mod = child;//from   w w  w.ja  v a 2 s. c  o m
                    }
                    bQuery.add(query, getModifierValue(mod));

                } catch (final TooManyClauses ex) {

                    throw new QueryNodeException(new MessageImpl(QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES,
                            BooleanQuery.getMaxClauseCount(),
                            queryNode.toQueryString(new EscapeQuerySyntaxImpl())), ex);

                }

            }

        }

    }

    return bQuery;

}

From source file:com.sindicetech.siren.search.node.TestBooleanQuery.java

License:Open Source License

@Test
public void testReqTuple() throws CorruptIndexException, IOException {
    for (int i = 0; i < 10; i++) {
        this.addDocument("<subj> <aaa> <bbb> . <subj> <ccc> <ddd> . ");
        this.addDocument("<subj> <aaa> <bbb> . ");
    }//from w w  w  . ja  v  a  2 s .c o  m

    final Query nested1 = tuple().with(nbq(must("aaa")).bound(1, 1)).with(nbq(must("bbb")).bound(2, 2))
            .getLuceneProxyQuery();

    final Query nested2 = tuple().with(nbq(must("ccc")).bound(1, 1)).with(nbq(must("ddd")).bound(2, 2))
            .getLuceneProxyQuery();

    final BooleanQuery q = new BooleanQuery();
    q.add(nested1, Occur.MUST);
    q.add(nested2, Occur.MUST);

    assertEquals(10, searcher.search(q, 10).totalHits);
}

From source file:com.sindicetech.siren.search.node.TestBooleanQuery.java

License:Open Source License

@Test
public void testReqOptTuple() throws CorruptIndexException, IOException {
    for (int i = 0; i < 10; i++) {
        this.addDocument("<subj> <aaa> <bbb> . <subj> <ccc> <ddd> . ");
        this.addDocument("<subj> <aaa> <bbb> . ");
    }/* w w  w.  ja  v  a 2 s . c  om*/

    final Query nested1 = tuple().with(nbq(must("aaa")).bound(1, 1)).with(nbq(must("bbb")).bound(2, 2))
            .getLuceneProxyQuery();

    final Query nested2 = tuple().with(nbq(must("ccc")).bound(1, 1)).with(nbq(must("ddd")).bound(2, 2))
            .getLuceneProxyQuery();

    final BooleanQuery q = new BooleanQuery();
    q.add(nested1, Occur.MUST);
    q.add(nested2, Occur.SHOULD);

    assertEquals(20, searcher.search(q, 10).totalHits);
}

From source file:com.sindicetech.siren.search.node.TestBooleanQuery.java

License:Open Source License

@Test
public void testReqExclTuple() throws CorruptIndexException, IOException {
    for (int i = 0; i < 10; i++) {
        this.addDocument("<subj> <aaa> <bbb> . <subj> <ccc> <ddd> . <subj> <eee> <fff> . ");
        this.addDocument("<subj> <aaa> <bbb> . <subj> <ccc> <ddd> . <subj> <eee> <ggg> . ");
    }/*from  w  w  w.  j a  va 2 s  .c o  m*/

    final Query nested1 = tuple().with(nbq(must("eee")).bound(1, 1)).with(nbq(must("ggg")).bound(2, 2))
            .getLuceneProxyQuery();

    final Query nested2 = tuple().with(nbq(must("aaa")).bound(1, 1)).with(nbq(must("bbb")).bound(2, 2))
            .getLuceneProxyQuery();

    final Query nested3 = tuple().with(nbq(must("ccc")).bound(1, 1)).with(nbq(must("ddd")).bound(2, 2))
            .getLuceneProxyQuery();

    final BooleanQuery q = new BooleanQuery();
    q.add(nested1, Occur.MUST_NOT);
    q.add(nested2, Occur.MUST);
    q.add(nested3, Occur.MUST);

    assertEquals(10, searcher.search(q, 10).totalHits);
}

From source file:com.sindicetech.siren.search.node.TestBooleanQuery.java

License:Open Source License

@Test
public void testReqExclCell() throws CorruptIndexException, IOException {
    for (int i = 0; i < 10; i++) {
        this.addDocument("<subj> <aaa> <bbb> . <subj> <ccc> <ddd> . <subj> <eee> <fff> . ");
        this.addDocument("<subj> <aaa> <bbb> . <subj> <ccc> <ddd> . <subj> <eee> <ggg> . ");
    }//from   w  w  w . ja  v a 2 s .  co  m

    final Query nested1 = nbq(must("aaa")).bound(1, 1).getLuceneProxyQuery();
    final Query nested2 = nbq(must("bbb")).bound(2, 2).getLuceneProxyQuery();
    final Query nested3 = nbq(must("ggg")).bound(2, 2).getLuceneProxyQuery();

    final BooleanQuery q = new BooleanQuery();
    q.add(nested3, Occur.MUST_NOT);
    q.add(nested1, Occur.MUST);
    q.add(nested2, Occur.MUST);

    assertEquals(10, searcher.search(q, 10).totalHits);
}

From source file:com.sindicetech.siren.search.node.TestLuceneProxyNodeQuery.java

License:Open Source License

@Test
public void testBoost() throws Exception {
    final float boost = 2.5f;

    this.addDocument("\"aaa ccc\" \"one five\" . \"aaa bbb\" \"ccc eee\" .");

    BooleanQuery bq1 = new BooleanQuery();
    NodeTermQuery tq = new NodeTermQuery(new Term(DEFAULT_TEST_FIELD, "one"));
    tq.setBoost(boost);//from w w w  .ja  v a2  s . c  om
    bq1.add(new LuceneProxyNodeQuery(tq), Occur.MUST);
    bq1.add(new LuceneProxyNodeQuery(new NodeTermQuery(new Term(DEFAULT_TEST_FIELD, "five"))), Occur.MUST);

    BooleanQuery bq2 = new BooleanQuery();
    tq = new NodeTermQuery(new Term(DEFAULT_TEST_FIELD, "one"));
    LuceneProxyNodeQuery dq = new LuceneProxyNodeQuery(tq);
    dq.setBoost(boost);
    bq2.add(dq, Occur.MUST);
    bq2.add(new LuceneProxyNodeQuery(new NodeTermQuery(new Term(DEFAULT_TEST_FIELD, "five"))), Occur.MUST);

    assertScoreEquals(bq1, bq2);
}

From source file:com.sindicetech.siren.search.node.TestNodeBooleanScorer.java

License:Open Source License

/**
 * <code>bbb cell(+ddd +eee)</code>
 *//*from   w  w w . ja v  a  2s. co  m*/
@Test
public void testParenthesisMust() throws IOException {
    this.addDocument("\"bbb\" . \"ddd eee\" . ");

    final Query nested = nbq(must("ddd"), must("eee")).getLuceneProxyQuery();
    final BooleanQuery bq = new BooleanQuery();
    bq.add(new TermQuery(new Term(DEFAULT_TEST_FIELD, "aaa")), BooleanClause.Occur.SHOULD);
    bq.add(nested, BooleanClause.Occur.SHOULD);
    assertEquals(1, searcher.search(bq, 10).totalHits);
}

From source file:com.sindicetech.siren.search.node.TestNodeBooleanScorer.java

License:Open Source License

/**
 * <code>aaa +cell(ddd eee)</code>
 *//*from www.  ja v a 2s.c  om*/
@Test
public void testParenthesisMust2() throws IOException {
    this.addDocument("\"bbb\" . \"ddd eee\" . ");

    final Query nested = nbq(should("ddd"), should("eee")).getLuceneProxyQuery();
    final BooleanQuery bq = new BooleanQuery();
    bq.add(new TermQuery(new Term(DEFAULT_TEST_FIELD, "bbb")), BooleanClause.Occur.SHOULD);
    bq.add(nested, BooleanClause.Occur.MUST);
    assertEquals(1, searcher.search(bq, 10).totalHits);
}

From source file:com.sindicetech.siren.search.node.TestNodeBooleanScorer.java

License:Open Source License

/**
 * <code>cell(ddd ccc) cell(eee ccc)</code>
 *//*from   www. j  a  v  a 2 s  .c o  m*/
@Test
public void testParenthesisShould() throws IOException {
    this.addDocument("\"bbb\" . \"ddd eee\" . ");

    final Query nested1 = nbq(should("ddd"), should("ccc")).getLuceneProxyQuery();
    final Query nested2 = nbq(should("eee"), should("ccc")).getLuceneProxyQuery();

    final BooleanQuery q = new BooleanQuery();
    q.add(nested1, BooleanClause.Occur.SHOULD);
    q.add(nested2, BooleanClause.Occur.SHOULD);
    assertEquals(1, searcher.search(q, 10).totalHits);
}

From source file:com.sindicetech.siren.search.node.TestTupleQuery.java

License:Open Source License

/**
 * <code>aaa ({[+ddd] [+eee]})</code>
 * Here, the keywords are mandatory in the cell, but each cell is optional in
 * the tuple. So, even the first document (having only one matching cell)
 * should match.// w ww  .  j  a va  2s  . com
 */
@Test
public void testParenthesisMust() throws IOException {
    this.addDocument("\"bbb\" . \"ccc\" \"ddd bbb\" . ");
    this.addDocument("\"bbb\" . \"ccc eee\" \"ddd bbb\" . ");
    this.addDocument("\"bbb\" . ");

    final Query query = tuple().optional(nbq(must("ddd")), nbq(must("eee"))).getLuceneProxyQuery();

    final BooleanQuery q = new BooleanQuery();
    q.add(new TermQuery(new Term(DEFAULT_TEST_FIELD, "aaa")), BooleanClause.Occur.SHOULD);
    q.add(query, BooleanClause.Occur.SHOULD);
    assertEquals(2, searcher.search(q, 100).totalHits);
}