Search in sources :

Example 6 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_booleanQueryWithMustNot.

public void testExtractQueryMetadata_booleanQueryWithMustNot() {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    builder.add(termQuery1, BooleanClause.Occur.MUST_NOT);
    PhraseQuery phraseQuery = new PhraseQuery("_field", "_term1", "term2");
    builder.add(phraseQuery, BooleanClause.Occur.SHOULD);
    BooleanQuery booleanQuery = builder.build();
    Result result = analyze(booleanQuery);
    assertThat(result.verified, is(false));
    List<Term> terms = new ArrayList<>(result.terms);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(phraseQuery.getTerms()[0].field()));
    assertThat(terms.get(0).bytes(), equalTo(phraseQuery.getTerms()[0].bytes()));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 7 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_matchAllDocsQuery.

public void testExtractQueryMetadata_matchAllDocsQuery() {
    expectThrows(UnsupportedQueryException.class, () -> analyze(new MatchAllDocsQuery()));
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("field", "value")), BooleanClause.Occur.MUST);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    Result result = analyze(builder.build());
    assertThat(result.verified, is(false));
    assertTermsEqual(result.terms, new Term("field", "value"));
    builder = new BooleanQuery.Builder();
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    BooleanQuery bq1 = builder.build();
    expectThrows(UnsupportedQueryException.class, () -> analyze(bq1));
    builder = new BooleanQuery.Builder();
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST_NOT);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    BooleanQuery bq2 = builder.build();
    expectThrows(UnsupportedQueryException.class, () -> analyze(bq2));
    builder = new BooleanQuery.Builder();
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    BooleanQuery bq3 = builder.build();
    expectThrows(UnsupportedQueryException.class, () -> analyze(bq3));
    builder = new BooleanQuery.Builder();
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST_NOT);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    BooleanQuery bq4 = builder.build();
    expectThrows(UnsupportedQueryException.class, () -> analyze(bq4));
    builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("field", "value")), BooleanClause.Occur.SHOULD);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    BooleanQuery bq5 = builder.build();
    expectThrows(UnsupportedQueryException.class, () -> analyze(bq5));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 8 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.

the class ElasticsearchAssertions method assertBooleanSubQuery.

public static <T extends Query> T assertBooleanSubQuery(Query query, Class<T> subqueryType, int i) {
    assertThat(query, instanceOf(BooleanQuery.class));
    BooleanQuery q = (BooleanQuery) query;
    assertThat(q.clauses().size(), greaterThan(i));
    assertThat(q.clauses().get(i).getQuery(), instanceOf(subqueryType));
    return subqueryType.cast(q.clauses().get(i).getQuery());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery)

Example 9 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.

the class XMoreLikeThisTests 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);
    mlt.setAnalyzer(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
    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
    List<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();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) MoreLikeThis(org.apache.lucene.queries.mlt.MoreLikeThis) Term(org.apache.lucene.index.Term) 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 10 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.

the class ExistsQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(ExistsQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    String fieldPattern = queryBuilder.fieldName();
    Collection<String> fields = context.getQueryShardContext().simpleMatchToIndexNames(fieldPattern);
    if (getCurrentTypes().length == 0) {
        assertThat(query, instanceOf(MatchNoDocsQuery.class));
        MatchNoDocsQuery matchNoDocsQuery = (MatchNoDocsQuery) query;
        assertThat(matchNoDocsQuery.toString(null), containsString("Missing types in \"exists\" query."));
    } else if (fields.size() == 1) {
        assertThat(query, instanceOf(ConstantScoreQuery.class));
        ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
        assertThat(constantScoreQuery.getQuery(), instanceOf(TermQuery.class));
        TermQuery termQuery = (TermQuery) constantScoreQuery.getQuery();
        assertEquals(fields.iterator().next(), termQuery.getTerm().text());
    } else {
        assertThat(query, instanceOf(ConstantScoreQuery.class));
        ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
        assertThat(constantScoreQuery.getQuery(), instanceOf(BooleanQuery.class));
        BooleanQuery booleanQuery = (BooleanQuery) constantScoreQuery.getQuery();
        assertThat(booleanQuery.clauses().size(), equalTo(fields.size()));
        for (int i = 0; i < fields.size(); i++) {
            BooleanClause booleanClause = booleanQuery.clauses().get(i);
            assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
        }
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)218 TermQuery (org.apache.lucene.search.TermQuery)132 Term (org.apache.lucene.index.Term)114 Query (org.apache.lucene.search.Query)91 BooleanClause (org.apache.lucene.search.BooleanClause)59 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)52 BoostQuery (org.apache.lucene.search.BoostQuery)49 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)45 PhraseQuery (org.apache.lucene.search.PhraseQuery)43 PrefixQuery (org.apache.lucene.search.PrefixQuery)39 TopDocs (org.apache.lucene.search.TopDocs)38 IndexReader (org.apache.lucene.index.IndexReader)37 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)34 IndexSearcher (org.apache.lucene.search.IndexSearcher)33 WildcardQuery (org.apache.lucene.search.WildcardQuery)32 Document (org.apache.lucene.document.Document)30 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)28 ArrayList (java.util.ArrayList)27 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)27 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)26