Search in sources :

Example 1 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 2 with BooleanQuery

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

the class BoolQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
    booleanQueryBuilder.setDisableCoord(disableCoord);
    addBooleanClauses(context, booleanQueryBuilder, mustClauses, BooleanClause.Occur.MUST);
    addBooleanClauses(context, booleanQueryBuilder, mustNotClauses, BooleanClause.Occur.MUST_NOT);
    addBooleanClauses(context, booleanQueryBuilder, shouldClauses, BooleanClause.Occur.SHOULD);
    addBooleanClauses(context, booleanQueryBuilder, filterClauses, BooleanClause.Occur.FILTER);
    BooleanQuery booleanQuery = booleanQueryBuilder.build();
    if (booleanQuery.clauses().isEmpty()) {
        return new MatchAllDocsQuery();
    }
    final String minimumShouldMatch;
    if (context.isFilter() && this.minimumShouldMatch == null && shouldClauses.size() > 0) {
        minimumShouldMatch = "1";
    } else {
        minimumShouldMatch = this.minimumShouldMatch;
    }
    Query query = Queries.applyMinimumShouldMatch(booleanQuery, minimumShouldMatch);
    return adjustPureNegative ? fixNegativeQueryIfNeeded(query) : query;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 3 with BooleanQuery

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

the class CustomUnifiedHighlighterTests method testSentenceBoundedBreakIterator.

public void testSentenceBoundedBreakIterator() throws Exception {
    final String[] inputs = { "The quick brown fox in a long sentence with another quick brown fox. " + "Another sentence with brown fox." };
    final String[] outputs = { "The <b>quick</b> <b>brown</b>", "<b>fox</b> in a long", "with another <b>quick</b>", "<b>brown</b> <b>fox</b>.", "sentence with <b>brown</b>", "<b>fox</b>." };
    BooleanQuery query = new BooleanQuery.Builder().add(new TermQuery(new Term("text", "quick")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("text", "brown")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("text", "fox")), BooleanClause.Occur.SHOULD).build();
    assertHighlightOneDoc("text", inputs, new StandardAnalyzer(), query, Locale.ROOT, BoundedBreakIteratorScanner.getSentence(Locale.ROOT, 10), 0, outputs);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) TermQuery(org.apache.lucene.search.TermQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Term(org.apache.lucene.index.Term)

Example 4 with BooleanQuery

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

the class PercolateQueryBuilderTests method testCreateMultiDocumentSearcher.

public void testCreateMultiDocumentSearcher() throws Exception {
    int numDocs = randomIntBetween(2, 8);
    List<ParseContext.Document> docs = new ArrayList<>(numDocs);
    for (int i = 0; i < numDocs; i++) {
        docs.add(new ParseContext.Document());
    }
    Analyzer analyzer = new WhitespaceAnalyzer();
    ParsedDocument parsedDocument = new ParsedDocument(null, null, "_id", "_type", null, docs, null, null, null);
    IndexSearcher indexSearcher = PercolateQueryBuilder.createMultiDocumentSearcher(analyzer, parsedDocument);
    assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(numDocs));
    // ensure that any query get modified so that the nested docs are never included as hits:
    Query query = new MatchAllDocsQuery();
    BooleanQuery result = (BooleanQuery) indexSearcher.createNormalizedWeight(query, true).getQuery();
    assertThat(result.clauses().size(), equalTo(2));
    assertThat(result.clauses().get(0).getQuery(), sameInstance(query));
    assertThat(result.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
    assertThat(result.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.MUST_NOT));
}
Also used : WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ArrayList(java.util.ArrayList) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) ParseContext(org.elasticsearch.index.mapper.ParseContext)

Example 5 with BooleanQuery

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

the class PercolatorFieldMapperTests method testCreateCandidateQuery.

public void testCreateCandidateQuery() throws Exception {
    addQueryMapping();
    MemoryIndex memoryIndex = new MemoryIndex(false);
    memoryIndex.addField("field1", "the quick brown fox jumps over the lazy dog", new WhitespaceAnalyzer());
    memoryIndex.addField("field2", "some more text", new WhitespaceAnalyzer());
    memoryIndex.addField("_field3", "unhide me", new WhitespaceAnalyzer());
    memoryIndex.addField("field4", "123", new WhitespaceAnalyzer());
    memoryIndex.addField(new LongPoint("number_field", 10L), new WhitespaceAnalyzer());
    IndexReader indexReader = memoryIndex.createSearcher().getIndexReader();
    BooleanQuery candidateQuery = (BooleanQuery) fieldType.createCandidateQuery(indexReader);
    assertEquals(2, candidateQuery.clauses().size());
    assertEquals(Occur.SHOULD, candidateQuery.clauses().get(0).getOccur());
    TermInSetQuery termsQuery = (TermInSetQuery) candidateQuery.clauses().get(0).getQuery();
    PrefixCodedTerms terms = termsQuery.getTermData();
    assertThat(terms.size(), equalTo(14L));
    PrefixCodedTerms.TermIterator termIterator = terms.iterator();
    assertTermIterator(termIterator, "_field3me", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "_field3unhide", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field1brown", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field1dog", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field1fox", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field1jumps", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field1lazy", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field1over", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field1quick", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field1the", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field2more", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field2some", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field2text", fieldType.queryTermsField.name());
    assertTermIterator(termIterator, "field4123", fieldType.queryTermsField.name());
    assertEquals(Occur.SHOULD, candidateQuery.clauses().get(1).getOccur());
    assertEquals(new TermQuery(new Term(fieldType.extractionResultField.name(), EXTRACTION_FAILED)), candidateQuery.clauses().get(1).getQuery());
}
Also used : WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) PrefixCodedTerms(org.apache.lucene.index.PrefixCodedTerms) IndexReader(org.apache.lucene.index.IndexReader) LongPoint(org.apache.lucene.document.LongPoint) Term(org.apache.lucene.index.Term)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)299 TermQuery (org.apache.lucene.search.TermQuery)178 Term (org.apache.lucene.index.Term)146 Query (org.apache.lucene.search.Query)130 BooleanClause (org.apache.lucene.search.BooleanClause)90 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)71 BoostQuery (org.apache.lucene.search.BoostQuery)58 PhraseQuery (org.apache.lucene.search.PhraseQuery)52 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)52 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 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)40 IndexReader (org.apache.lucene.index.IndexReader)39 PrefixQuery (org.apache.lucene.search.PrefixQuery)39 Test (org.junit.Test)39 Document (org.apache.lucene.document.Document)36 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)33