Search in sources :

Example 6 with ConstantScoreQuery

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

the class TypeFieldTypeTests method testTermsQuery.

public void testTermsQuery() throws Exception {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
    IndexReader reader = openReaderWithNewType("my_type", w);
    TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType();
    ft.setName(TypeFieldMapper.NAME);
    Query query = ft.termQuery("my_type", null);
    assertEquals(new MatchAllDocsQuery(), query.rewrite(reader));
    // Make sure that Lucene actually simplifies the query when there is a single type
    Query userQuery = new PhraseQuery("body", "quick", "fox");
    Query filteredQuery = new BooleanQuery.Builder().add(userQuery, Occur.MUST).add(query, Occur.FILTER).build();
    Query rewritten = new IndexSearcher(reader).rewrite(filteredQuery);
    assertEquals(userQuery, rewritten);
    // ... and does not rewrite it if there is more than one type
    reader.close();
    reader = openReaderWithNewType("my_type2", w);
    Query expected = new ConstantScoreQuery(new BooleanQuery.Builder().add(new TermQuery(new Term(TypeFieldMapper.NAME, "my_type")), Occur.SHOULD).build());
    assertEquals(expected, query.rewrite(reader));
    BytesRef[] types = new BytesRef[] { new BytesRef("my_type"), new BytesRef("my_type2"), new BytesRef("my_type3") };
    // the query should match all documents
    query = new TypeFieldMapper.TypesQuery(types);
    assertEquals(new MatchAllDocsQuery(), query.rewrite(reader));
    reader.close();
    reader = openReaderWithNewType("unknown_type", w);
    // the query cannot rewrite to a match all docs sinc unknown_type is not queried.
    query = new TypeFieldMapper.TypesQuery(types);
    expected = new ConstantScoreQuery(new BooleanQuery.Builder().add(new TermQuery(new Term(TypeFieldMapper.CONTENT_TYPE, types[0])), Occur.SHOULD).add(new TermQuery(new Term(TypeFieldMapper.CONTENT_TYPE, types[1])), Occur.SHOULD).build());
    rewritten = query.rewrite(reader);
    assertEquals(expected, rewritten);
    // make sure that redundant types does not rewrite to MatchAllDocsQuery
    query = new TypeFieldMapper.TypesQuery(new BytesRef("my_type"), new BytesRef("my_type"), new BytesRef("my_type"));
    expected = new ConstantScoreQuery(new BooleanQuery.Builder().add(new TermQuery(new Term(TypeFieldMapper.CONTENT_TYPE, "my_type")), Occur.SHOULD).build());
    rewritten = query.rewrite(reader);
    assertEquals(expected, rewritten);
    IOUtils.close(reader, w, dir);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 7 with ConstantScoreQuery

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

the class BoolQueryBuilderTests method testDefaultMinShouldMatch.

public void testDefaultMinShouldMatch() throws Exception {
    // Queries have a minShouldMatch of 0
    BooleanQuery bq = (BooleanQuery) parseQuery(boolQuery().must(termQuery("foo", "bar"))).toQuery(createShardContext());
    assertEquals(0, bq.getMinimumNumberShouldMatch());
    bq = (BooleanQuery) parseQuery(boolQuery().should(termQuery("foo", "bar"))).toQuery(createShardContext());
    assertEquals(0, bq.getMinimumNumberShouldMatch());
    // Filters have a minShouldMatch of 0/1
    ConstantScoreQuery csq = (ConstantScoreQuery) parseQuery(constantScoreQuery(boolQuery().must(termQuery("foo", "bar")))).toQuery(createShardContext());
    bq = (BooleanQuery) csq.getQuery();
    assertEquals(0, bq.getMinimumNumberShouldMatch());
    csq = (ConstantScoreQuery) parseQuery(constantScoreQuery(boolQuery().should(termQuery("foo", "bar")))).toQuery(createShardContext());
    bq = (BooleanQuery) csq.getQuery();
    assertEquals(1, bq.getMinimumNumberShouldMatch());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery)

Example 8 with ConstantScoreQuery

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

the class CustomQueryParserIT method testConstantScoreParsesFilter.

//see #11120
public void testConstantScoreParsesFilter() throws Exception {
    Query q = constantScoreQuery(new DummyQueryBuilder()).toQuery(queryShardContext());
    Query inner = ((ConstantScoreQuery) q).getQuery();
    assertThat(inner, instanceOf(DummyQueryParserPlugin.DummyQuery.class));
    assertEquals(true, ((DummyQueryParserPlugin.DummyQuery) inner).isFilter);
}
Also used : Query(org.apache.lucene.search.Query) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) QueryBuilders.constantScoreQuery(org.elasticsearch.index.query.QueryBuilders.constantScoreQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery)

Example 9 with ConstantScoreQuery

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

the class TermsQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(TermsQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    if (queryBuilder.termsLookup() == null && (queryBuilder.values() == null || queryBuilder.values().isEmpty())) {
        assertThat(query, instanceOf(MatchNoDocsQuery.class));
        MatchNoDocsQuery matchNoDocsQuery = (MatchNoDocsQuery) query;
        assertThat(matchNoDocsQuery.toString(), containsString("No terms supplied for \"terms\" query."));
    } else if (queryBuilder.termsLookup() != null && randomTerms.size() == 0) {
        assertThat(query, instanceOf(MatchNoDocsQuery.class));
        MatchNoDocsQuery matchNoDocsQuery = (MatchNoDocsQuery) query;
        assertThat(matchNoDocsQuery.toString(), containsString("No terms supplied for \"terms\" query."));
    } else {
        assertThat(query, either(instanceOf(TermInSetQuery.class)).or(instanceOf(PointInSetQuery.class)).or(instanceOf(ConstantScoreQuery.class)));
        if (query instanceof ConstantScoreQuery) {
            assertThat(((ConstantScoreQuery) query).getQuery(), instanceOf(BooleanQuery.class));
        }
        // we only do the check below for string fields (otherwise we'd have to decode the values)
        if (queryBuilder.fieldName().equals(INT_FIELD_NAME) || queryBuilder.fieldName().equals(DOUBLE_FIELD_NAME) || queryBuilder.fieldName().equals(BOOLEAN_FIELD_NAME) || queryBuilder.fieldName().equals(DATE_FIELD_NAME)) {
            return;
        }
        // expected returned terms depending on whether we have a terms query or a terms lookup query
        List<Object> terms;
        if (queryBuilder.termsLookup() != null) {
            terms = randomTerms;
        } else {
            terms = queryBuilder.values();
        }
        TermInSetQuery expected = new TermInSetQuery(queryBuilder.fieldName(), terms.stream().filter(Objects::nonNull).map(Object::toString).map(BytesRef::new).collect(Collectors.toList()));
        assertEquals(expected, query);
    }
}
Also used : MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with ConstantScoreQuery

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

the class NestedSortingTests method getTopDocs.

private TopDocs getTopDocs(IndexSearcher searcher, IndexFieldData<?> indexFieldData, String missingValue, MultiValueMode sortMode, int n, boolean reverse) throws IOException {
    Query parentFilter = new TermQuery(new Term("__type", "parent"));
    Query childFilter = new TermQuery(new Term("__type", "child"));
    XFieldComparatorSource nestedComparatorSource = indexFieldData.comparatorSource(missingValue, sortMode, createNested(searcher, parentFilter, childFilter));
    Query query = new ConstantScoreQuery(parentFilter);
    Sort sort = new Sort(new SortField("f", nestedComparatorSource, reverse));
    return searcher.search(query, n, sort);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term) XFieldComparatorSource(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource)

Aggregations

ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)68 BooleanQuery (org.apache.lucene.search.BooleanQuery)43 TermQuery (org.apache.lucene.search.TermQuery)42 Query (org.apache.lucene.search.Query)40 Term (org.apache.lucene.index.Term)30 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)19 BoostQuery (org.apache.lucene.search.BoostQuery)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 ArrayList (java.util.ArrayList)13 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)13 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)12 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)11 Document (org.apache.lucene.document.Document)10 Test (org.junit.Test)10 Sort (org.apache.lucene.search.Sort)9 TopDocs (org.apache.lucene.search.TopDocs)9 StringField (org.apache.lucene.document.StringField)8 BooleanClause (org.apache.lucene.search.BooleanClause)8 RegexpQuery (org.apache.lucene.search.RegexpQuery)8 SortField (org.apache.lucene.search.SortField)8