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.nearinfinity.blur.search.SuperQueryTest.java

License:Apache License

@Test
public void testAggregateScoreTypes() throws Exception {
    IndexSearcher searcher = createSearcher();
    BooleanQuery booleanQuery = new BooleanQuery();
    booleanQuery.add(wrapSuper(PERSON_NAME, NAME1, ScoreType.AGGREGATE), Occur.SHOULD);
    booleanQuery.add(wrapSuper(ADDRESS_STREET, STREET1, ScoreType.AGGREGATE), Occur.MUST);
    TopDocs topDocs = searcher.search(booleanQuery, 10);
    printTopDocs(topDocs);/*w  w  w .j a  va 2  s .co m*/
    assertEquals(3, topDocs.totalHits);
    assertEquals(3.30, topDocs.scoreDocs[0].score, 0.01);
    assertEquals(2.20, topDocs.scoreDocs[1].score, 0.01);
    assertEquals(0.55, topDocs.scoreDocs[2].score, 0.01);
}

From source file:com.nearinfinity.blur.search.SuperQueryTest.java

License:Apache License

@Test
public void testBestScoreTypes() throws Exception {
    IndexSearcher searcher = createSearcher();
    BooleanQuery booleanQuery = new BooleanQuery();
    booleanQuery.add(wrapSuper(PERSON_NAME, NAME1, ScoreType.BEST), Occur.SHOULD);
    booleanQuery.add(wrapSuper(ADDRESS_STREET, STREET1, ScoreType.BEST), Occur.MUST);
    TopDocs topDocs = searcher.search(booleanQuery, 10);
    assertEquals(3, topDocs.totalHits);//w  w w .j  a  va2  s  . c o m
    printTopDocs(topDocs);
    assertEquals(2.20, topDocs.scoreDocs[0].score, 0.01);
    assertEquals(2.20, topDocs.scoreDocs[1].score, 0.01);
    assertEquals(0.55, topDocs.scoreDocs[2].score, 0.01);
}

From source file:com.nearinfinity.blur.search.SuperQueryTest.java

License:Apache License

@Test
public void testConstantScoreTypes() throws Exception {
    IndexSearcher searcher = createSearcher();
    BooleanQuery booleanQuery = new BooleanQuery();
    booleanQuery.add(wrapSuper(PERSON_NAME, NAME1, ScoreType.CONSTANT), Occur.SHOULD);
    booleanQuery.add(wrapSuper(ADDRESS_STREET, STREET1, ScoreType.CONSTANT), Occur.MUST);
    TopDocs topDocs = searcher.search(booleanQuery, 10);
    assertEquals(3, topDocs.totalHits);/*from  w w w.j a  v  a2 s  . c  om*/
    printTopDocs(topDocs);
    assertEquals(2.0, topDocs.scoreDocs[0].score, 0.01);
    assertEquals(2.0, topDocs.scoreDocs[1].score, 0.01);
    assertEquals(0.5, topDocs.scoreDocs[2].score, 0.01);
}

From source file:com.nearinfinity.blur.search.SuperQueryTest.java

License:Apache License

@Test
public void testSuperScoreTypes() throws Exception {
    IndexSearcher searcher = createSearcher();
    BooleanQuery booleanQuery = new BooleanQuery();
    booleanQuery.add(wrapSuper(PERSON_NAME, NAME1, ScoreType.SUPER), Occur.SHOULD);
    booleanQuery.add(wrapSuper(ADDRESS_STREET, STREET1, ScoreType.SUPER), Occur.MUST);
    TopDocs topDocs = searcher.search(booleanQuery, 10);
    assertEquals(3, topDocs.totalHits);//from   w  w w  . j av a 2s.  c  o  m
    printTopDocs(topDocs);
    assertEquals(3.10, topDocs.scoreDocs[0].score, 0.01);
    assertEquals(3.00, topDocs.scoreDocs[1].score, 0.01);
    assertEquals(0.75, topDocs.scoreDocs[2].score, 0.01);
}

From source file:com.nearinfinity.blur.search.SuperQueryTest.java

License:Apache License

@Test
public void testSuperScoreTypesWithFacet() throws Exception {
    IndexSearcher searcher = createSearcher();
    BooleanQuery booleanQuery = new BooleanQuery();
    booleanQuery.add(wrapSuper(PERSON_NAME, NAME1, ScoreType.SUPER), Occur.SHOULD);
    booleanQuery.add(wrapSuper(ADDRESS_STREET, STREET1, ScoreType.SUPER), Occur.MUST);

    BooleanQuery f1 = new BooleanQuery();
    f1.add(new TermQuery(new Term(PERSON_NAME, NAME1)), Occur.MUST);
    f1.add(new TermQuery(new Term(PERSON_NAME, NAME2)), Occur.MUST);

    Query[] facets = new Query[] { new SuperQuery(f1, ScoreType.CONSTANT) };
    AtomicLongArray counts = new AtomicLongArray(facets.length);
    FacetQuery query = new FacetQuery(booleanQuery, facets, counts);

    TopDocs topDocs = searcher.search(query, 10);
    assertEquals(3, topDocs.totalHits);// w  ww .ja va 2 s  .c o m
    printTopDocs(topDocs);
    assertEquals(3.10, topDocs.scoreDocs[0].score, 0.01);
    assertEquals(3.00, topDocs.scoreDocs[1].score, 0.01);
    assertEquals(0.75, topDocs.scoreDocs[2].score, 0.01);
}

From source file:com.netflix.exhibitor.core.index.QueryBuilder.java

License:Apache License

public Query build(Type type) {
    Preconditions.checkArgument(queries.size() > 0, "Query is empty");

    if (queries.size() == 1) {
        return queries.iterator().next();
    }/*from   ww  w. j av a 2 s  . c o  m*/

    BooleanQuery query = new BooleanQuery();
    for (Query q : queries) {
        query.add(q, (type == Type.AND) ? BooleanClause.Occur.MUST : BooleanClause.Occur.SHOULD);
    }

    return query;
}

From source file:com.netspective.sparx.navigate.fts.FullTextSearchPage.java

License:Open Source License

public Query parseQuery(NavigationContext nc, SearchExpression expression) throws ParseException {
    if (expression.isSearchWithinPreviousResults()) {
        final FullTextSearchResults searchResults = getSearchResultsManager().getActiveUserSearchResults(this,
                nc);/*w w  w  .  j  a  va  2s . c  o  m*/
        if (searchResults == null)
            getLog().error("Attempting to search within a search but there are no active search results");
        else {
            BooleanQuery booleanQuery = new BooleanQuery();
            booleanQuery.add(searchResults.getQuery(), true, false);
            booleanQuery.add(parseQuery(expression.getExprText()), true, false);
            return booleanQuery;
        }
    }

    final Query advancedQuery = expression.getAdvancedQuery();
    return advancedQuery != null ? advancedQuery : parseQuery(expression.getExprText());
}

From source file:com.netspective.sparx.navigate.fts.SearchHitsTemplateRenderer.java

License:Open Source License

public Query getAdvancedSearchQuery(NavigationContext nc) {
    final ServletRequest request = nc.getRequest();
    // we create a hidden field in the advanced data dialog telling us we have advanced data
    if (request.getParameter("hasAdvancedData") == null)
        return null;

    final FullTextSearchPage searchPage = (FullTextSearchPage) nc.getActivePage();
    final String[] defaultFieldNames = searchPage.getDefaultAdvancedSearchFieldNames();

    final String allWordsParamValue = request.getParameter("all");
    final String exactPhraseParamValue = request.getParameter("phrase");
    final String atLeastOneWordParamValue = request.getParameter("one");

    BooleanQuery advancedQuery = new BooleanQuery();

    if (allWordsParamValue != null && allWordsParamValue.trim().length() > 0) {
        Query query = getAllWordsQuery(nc, defaultFieldNames, allWordsParamValue);
        advancedQuery.add(query, true, false);
    }//from   w  ww  . j  a va 2  s  . c  o  m

    if (exactPhraseParamValue != null && exactPhraseParamValue.trim().length() > 0) {
        Query query = getExactPhraseQuery(nc, defaultFieldNames, exactPhraseParamValue);
        advancedQuery.add(query, true, false);
    }

    if (atLeastOneWordParamValue != null && atLeastOneWordParamValue.trim().length() > 0) {
        Query query = getAtLeastOneWordQuery(nc, defaultFieldNames, atLeastOneWordParamValue);
        advancedQuery.add(query, true, false);
    }

    String[] advancedSearchFieldNames = searchPage.getAdvancedSearchFieldNames();
    for (int i = 0; i < advancedSearchFieldNames.length; i++) {
        final String fieldName = advancedSearchFieldNames[i];
        final String fieldParamValue = request.getParameter("field_" + fieldName);
        if (fieldParamValue != null && fieldParamValue.trim().length() > 0) {
            final Query fieldQuery = getFieldQuery(nc, fieldName, fieldParamValue);
            advancedQuery.add(fieldQuery, true, false);
        }
    }

    return advancedQuery;
}

From source file:com.netspective.sparx.navigate.fts.SearchHitsTemplateRenderer.java

License:Open Source License

protected Query getAtLeastOneWordQuery(final NavigationContext nc, final String[] defaultFieldNames,
        final String atLeastOneWordParamValue) {
    final BooleanQuery atLeastOneWordQuery = new BooleanQuery();
    for (int i = 0; i < defaultFieldNames.length; i++) {
        String[] words = TextUtils.getInstance().split(atLeastOneWordParamValue, " ", true);
        if (words.length == 1)
            atLeastOneWordQuery.add(new TermQuery(new Term(defaultFieldNames[i], words[0])), false, false);
        else {//from w  ww.  j  a  v a 2 s .c  om
            BooleanQuery wordGroup = new BooleanQuery();
            for (int j = 0; j < words.length; j++)
                wordGroup.add(new TermQuery(new Term(defaultFieldNames[i], words[j])), false, false);
            atLeastOneWordQuery.add(wordGroup, false, false);
        }
    }
    return atLeastOneWordQuery;
}

From source file:com.netspective.sparx.navigate.fts.SearchHitsTemplateRenderer.java

License:Open Source License

protected Query getExactPhraseQuery(final NavigationContext nc, final String[] defaultFieldNames,
        final String exactPhraseParamValue) {
    final BooleanQuery exactPhraseQuery = new BooleanQuery();
    for (int i = 0; i < defaultFieldNames.length; i++) {
        final PhraseQuery phraseQuery = new PhraseQuery();
        final String[] words = TextUtils.getInstance().split(exactPhraseParamValue, " ", true);
        for (int j = 0; j < words.length; j++)
            phraseQuery.add(new Term(defaultFieldNames[i], words[j]));
        exactPhraseQuery.add(phraseQuery, false, false);
    }/*from  w w  w.ja  v a2s  . c  om*/
    return exactPhraseQuery;
}