Search in sources :

Example 71 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.

the class TestMoreLikeThis method testTopN.

public void testTopN() throws Exception {
    int numDocs = 100;
    int topN = 25;
    // add series of docs with terms of decreasing df
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    for (int i = 0; i < numDocs; i++) {
        addDoc(writer, generateStrSeq(0, i + 1));
    }
    IndexReader reader = writer.getReader();
    writer.close();
    // setup MLT query
    MoreLikeThis mlt = new MoreLikeThis(reader);
    Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
    mlt.setAnalyzer(analyzer);
    mlt.setMaxQueryTerms(topN);
    mlt.setMinDocFreq(1);
    mlt.setMinTermFreq(1);
    mlt.setMinWordLen(1);
    mlt.setFieldNames(new String[] { "text" });
    // perform MLT query
    String likeText = "";
    for (String text : generateStrSeq(0, numDocs)) {
        likeText += text + " ";
    }
    BooleanQuery query = (BooleanQuery) mlt.like("text", new StringReader(likeText));
    // check best terms are topN of highest idf
    Collection<BooleanClause> clauses = query.clauses();
    assertEquals("Expected" + topN + "clauses only!", topN, clauses.size());
    Term[] expectedTerms = new Term[topN];
    int idx = 0;
    for (String text : generateStrSeq(numDocs - topN, topN)) {
        expectedTerms[idx++] = new Term("text", text);
    }
    for (BooleanClause clause : clauses) {
        Term term = ((TermQuery) clause.getQuery()).getTerm();
        assertTrue(Arrays.asList(expectedTerms).contains(term));
    }
    // clean up
    reader.close();
    dir.close();
    analyzer.close();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) BooleanClause(org.apache.lucene.search.BooleanClause) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexReader(org.apache.lucene.index.IndexReader) StringReader(java.io.StringReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 72 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.

the class ComplexPhraseQueryParser method parse.

@Override
public Query parse(String query) throws ParseException {
    if (isPass2ResolvingPhrases) {
        MultiTermQuery.RewriteMethod oldMethod = getMultiTermRewriteMethod();
        try {
            // Temporarily force BooleanQuery rewrite so that Parser will
            // generate visible
            // collection of terms which we can convert into SpanQueries.
            // ConstantScoreRewrite mode produces an
            // opaque ConstantScoreQuery object which cannot be interrogated for
            // terms in the same way a BooleanQuery can.
            // QueryParser is not guaranteed threadsafe anyway so this temporary
            // state change should not
            // present an issue
            setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
            return super.parse(query);
        } finally {
            setMultiTermRewriteMethod(oldMethod);
        }
    }
    // First pass - parse the top-level query recording any PhraseQuerys
    // which will need to be resolved
    complexPhrases = new ArrayList<>();
    Query q = super.parse(query);
    // Perform second pass, using this QueryParser to parse any nested
    // PhraseQueries with different
    // set of syntax restrictions (i.e. all fields must be same)
    isPass2ResolvingPhrases = true;
    try {
        for (Iterator<ComplexPhraseQuery> iterator = complexPhrases.iterator(); iterator.hasNext(); ) {
            currentPhraseQuery = iterator.next();
            // in each phrase, now parse the contents between quotes as a
            // separate parse operation
            currentPhraseQuery.parsePhraseElements(this);
        }
    } finally {
        isPass2ResolvingPhrases = false;
    }
    return q;
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) RewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)

Example 73 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.

the class MultiFieldQueryParser method parse.

/**
   * Parses a query, searching on the fields specified.
   * Use this if you need to specify certain fields as required,
   * and others as prohibited.
   * <p>
   * Usage:
   * <pre class="prettyprint">
   * <code>
   * String[] query = {"query1", "query2", "query3"};
   * String[] fields = {"filename", "contents", "description"};
   * BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
   *                BooleanClause.Occur.MUST,
   *                BooleanClause.Occur.MUST_NOT};
   * MultiFieldQueryParser.parse(query, fields, flags, analyzer);
   * </code>
   * </pre>
   *<p>
   * The code above would construct a query:
   * <pre>
   * <code>
   * (filename:query1) +(contents:query2) -(description:query3)
   * </code>
   * </pre>
   *
   * @param queries Queries string to parse
   * @param fields Fields to search on
   * @param flags Flags describing the fields
   * @param analyzer Analyzer to use
   * @throws ParseException if query parsing fails
   * @throws IllegalArgumentException if the length of the queries, fields,
   *  and flags array differ
   */
public static Query parse(String[] queries, String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException {
    if (!(queries.length == fields.length && queries.length == flags.length))
        throw new IllegalArgumentException("queries, fields, and flags array have have different length");
    BooleanQuery.Builder bQuery = new BooleanQuery.Builder();
    for (int i = 0; i < fields.length; i++) {
        QueryParser qp = new QueryParser(fields[i], analyzer);
        Query q = qp.parse(queries[i]);
        if (// q never null, just being defensive
        q != null && (!(q instanceof BooleanQuery) || ((BooleanQuery) q).clauses().size() > 0)) {
            bQuery.add(q, flags[i]);
        }
    }
    return bQuery.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery)

Example 74 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.

the class MultiFieldQueryParser method parse.

/**
   * Parses a query which searches on the fields specified.
   * <p>
   * If x fields are specified, this effectively constructs:
   * <pre>
   * <code>
   * (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
   * </code>
   * </pre>
   * @param queries Queries strings to parse
   * @param fields Fields to search on
   * @param analyzer Analyzer to use
   * @throws ParseException if query parsing fails
   * @throws IllegalArgumentException if the length of the queries array differs
   *  from the length of the fields array
   */
public static Query parse(String[] queries, String[] fields, Analyzer analyzer) throws ParseException {
    if (queries.length != fields.length)
        throw new IllegalArgumentException("queries.length != fields.length");
    BooleanQuery.Builder bQuery = new BooleanQuery.Builder();
    for (int i = 0; i < fields.length; i++) {
        QueryParser qp = new QueryParser(fields[i], analyzer);
        Query q = qp.parse(queries[i]);
        if (// q never null, just being defensive
        q != null && (!(q instanceof BooleanQuery) || ((BooleanQuery) q).clauses().size() > 0)) {
            bQuery.add(q, BooleanClause.Occur.SHOULD);
        }
    }
    return bQuery.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery)

Example 75 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.

the class TestExtendedDismaxParser method containsClause.

private boolean containsClause(Query query, String field, String value, int boost, boolean fuzzy) {
    float queryBoost = 1f;
    if (query instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) query;
        query = bq.getQuery();
        queryBoost = bq.getBoost();
    }
    if (query instanceof BooleanQuery) {
        return containsClause((BooleanQuery) query, field, value, boost, fuzzy);
    }
    if (query instanceof DisjunctionMaxQuery) {
        return containsClause((DisjunctionMaxQuery) query, field, value, boost, fuzzy);
    }
    if (boost != queryBoost) {
        return false;
    }
    if (query instanceof TermQuery && !fuzzy) {
        return containsClause((TermQuery) query, field, value);
    }
    if (query instanceof FuzzyQuery && fuzzy) {
        return containsClause((FuzzyQuery) query, field, value);
    }
    return false;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)297 TermQuery (org.apache.lucene.search.TermQuery)176 Term (org.apache.lucene.index.Term)144 Query (org.apache.lucene.search.Query)129 BooleanClause (org.apache.lucene.search.BooleanClause)89 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)70 BoostQuery (org.apache.lucene.search.BoostQuery)58 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)52 PhraseQuery (org.apache.lucene.search.PhraseQuery)50 ArrayList (java.util.ArrayList)47 TopDocs (org.apache.lucene.search.TopDocs)47 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)43 WildcardQuery (org.apache.lucene.search.WildcardQuery)42 IndexSearcher (org.apache.lucene.search.IndexSearcher)40 IndexReader (org.apache.lucene.index.IndexReader)39 PrefixQuery (org.apache.lucene.search.PrefixQuery)39 Test (org.junit.Test)39 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)38 Document (org.apache.lucene.document.Document)36 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)33