Search in sources :

Example 6 with MatchNoDocsQuery

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

the class FunctionScoreEquivalenceTests method testMinScoreAllExcluded.

public void testMinScoreAllExcluded() throws Exception {
    Term term = randomTerm();
    Query query = new TermQuery(term);
    FunctionScoreQuery fsq = new FunctionScoreQuery(query, null, Float.POSITIVE_INFINITY, null, Float.POSITIVE_INFINITY);
    assertSameScores(new MatchNoDocsQuery(), fsq);
    FiltersFunctionScoreQuery ffsq = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, CombineFunction.MULTIPLY);
    assertSameScores(new MatchNoDocsQuery(), ffsq);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) Term(org.apache.lucene.index.Term)

Example 7 with MatchNoDocsQuery

use of org.apache.lucene.search.MatchNoDocsQuery 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 8 with MatchNoDocsQuery

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

the class MultiPhrasePrefixQuery method rewrite.

@Override
public Query rewrite(IndexReader reader) throws IOException {
    Query rewritten = super.rewrite(reader);
    if (rewritten != this) {
        return rewritten;
    }
    if (termArrays.isEmpty()) {
        return new MatchNoDocsQuery();
    }
    MultiPhraseQuery.Builder query = new MultiPhraseQuery.Builder();
    query.setSlop(slop);
    int sizeMinus1 = termArrays.size() - 1;
    for (int i = 0; i < sizeMinus1; i++) {
        query.add(termArrays.get(i), positions.get(i));
    }
    Term[] suffixTerms = termArrays.get(sizeMinus1);
    int position = positions.get(sizeMinus1);
    ObjectHashSet<Term> terms = new ObjectHashSet<>();
    for (Term term : suffixTerms) {
        getPrefixTerms(terms, term, reader);
        if (terms.size() > maxExpansions) {
            break;
        }
    }
    if (terms.isEmpty()) {
        // which rewrites query with an empty reader.
        return new BooleanQuery.Builder().add(query.build(), BooleanClause.Occur.MUST).add(Queries.newMatchNoDocsQuery("No terms supplied for " + MultiPhrasePrefixQuery.class.getName()), BooleanClause.Occur.MUST).build();
    }
    query.add(terms.toArray(Term.class), position);
    return query.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ObjectHashSet(com.carrotsearch.hppc.ObjectHashSet) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Term(org.apache.lucene.index.Term)

Example 9 with MatchNoDocsQuery

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

the class SliceBuilder method toFilter.

public Query toFilter(QueryShardContext context, int shardId, int numShards) {
    final MappedFieldType type = context.fieldMapper(field);
    if (type == null) {
        throw new IllegalArgumentException("field " + field + " not found");
    }
    boolean useTermQuery = false;
    if (UidFieldMapper.NAME.equals(field)) {
        useTermQuery = true;
    } else if (type.hasDocValues() == false) {
        throw new IllegalArgumentException("cannot load numeric doc values on " + field);
    } else {
        IndexFieldData ifm = context.getForField(type);
        if (ifm instanceof IndexNumericFieldData == false) {
            throw new IllegalArgumentException("cannot load numeric doc values on " + field);
        }
    }
    if (numShards == 1) {
        return useTermQuery ? new TermsSliceQuery(field, id, max) : new DocValuesSliceQuery(field, id, max);
    }
    if (max >= numShards) {
        // the number of slices is greater than the number of shards
        // in such case we can reduce the number of requested shards by slice
        // first we check if the slice is responsible of this shard
        int targetShard = id % numShards;
        if (targetShard != shardId) {
            // the shard is not part of this slice, we can skip it.
            return new MatchNoDocsQuery("this shard is not part of the slice");
        }
        // compute the number of slices where this shard appears
        int numSlicesInShard = max / numShards;
        int rest = max % numShards;
        if (rest > targetShard) {
            numSlicesInShard++;
        }
        if (numSlicesInShard == 1) {
            // this shard has only one slice so we must check all the documents
            return new MatchAllDocsQuery();
        }
        // get the new slice id for this shard
        int shardSlice = id / numShards;
        return useTermQuery ? new TermsSliceQuery(field, shardSlice, numSlicesInShard) : new DocValuesSliceQuery(field, shardSlice, numSlicesInShard);
    }
    // the number of shards is greater than the number of slices
    // check if the shard is assigned to the slice
    int targetSlice = shardId % max;
    if (id != targetSlice) {
        // the shard is not part of this slice, we can skip it.
        return new MatchNoDocsQuery("this shard is not part of the slice");
    }
    return new MatchAllDocsQuery();
}
Also used : MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 10 with MatchNoDocsQuery

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

the class NestedQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    ObjectMapper nestedObjectMapper = context.getObjectMapper(path);
    if (nestedObjectMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new IllegalStateException("[" + NAME + "] failed to find nested object under path [" + path + "]");
        }
    }
    if (!nestedObjectMapper.nested().isNested()) {
        throw new IllegalStateException("[" + NAME + "] nested object under path [" + path + "] is not of nested type");
    }
    final BitSetProducer parentFilter;
    Query innerQuery;
    ObjectMapper objectMapper = context.nestedScope().getObjectMapper();
    if (objectMapper == null) {
        parentFilter = context.bitsetFilter(Queries.newNonNestedFilter());
    } else {
        parentFilter = context.bitsetFilter(objectMapper.nestedTypeFilter());
    }
    try {
        context.nestedScope().nextLevel(nestedObjectMapper);
        innerQuery = this.query.toQuery(context);
    } finally {
        context.nestedScope().previousLevel();
    }
    // in its child space
    if (new NestedHelper(context.getMapperService()).mightMatchNonNestedDocs(innerQuery, path)) {
        innerQuery = Queries.filtered(innerQuery, nestedObjectMapper.nestedTypeFilter());
    }
    return new ESToParentBlockJoinQuery(innerQuery, parentFilter, scoreMode, objectMapper == null ? null : objectMapper.fullPath());
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) NestedHelper(org.elasticsearch.index.search.NestedHelper) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Aggregations

MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)42 Query (org.apache.lucene.search.Query)25 BooleanQuery (org.apache.lucene.search.BooleanQuery)14 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 TermQuery (org.apache.lucene.search.TermQuery)11 Term (org.apache.lucene.index.Term)9 ArrayList (java.util.ArrayList)7 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)7 Directory (org.apache.lucene.store.Directory)7 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)7 Document (org.apache.lucene.document.Document)6 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 IndexReader (org.apache.lucene.index.IndexReader)5 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)5 List (java.util.List)4 DirectoryReader (org.apache.lucene.index.DirectoryReader)4 BytesRef (org.apache.lucene.util.BytesRef)4 LongPoint (org.apache.lucene.document.LongPoint)3 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)3