Search in sources :

Example 1 with Query

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

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

the class ExistsQueryBuilder method newFilter.

public static Query newFilter(QueryShardContext context, String fieldPattern) {
    final FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldMapper.FieldNamesFieldType) context.getMapperService().fullName(FieldNamesFieldMapper.NAME);
    if (fieldNamesFieldType == null) {
        // can only happen when no types exist, so no docs exist either
        return Queries.newMatchNoDocsQuery("Missing types in \"" + NAME + "\" query.");
    }
    final Collection<String> fields;
    if (context.getObjectMapper(fieldPattern) != null) {
        // the _field_names field also indexes objects, so we don't have to
        // do any more work to support exists queries on whole objects
        fields = Collections.singleton(fieldPattern);
    } else {
        fields = context.simpleMatchToIndexNames(fieldPattern);
    }
    if (fields.size() == 1) {
        Query filter = fieldNamesFieldType.termQuery(fields.iterator().next(), context);
        return new ConstantScoreQuery(filter);
    }
    BooleanQuery.Builder boolFilterBuilder = new BooleanQuery.Builder();
    for (String field : fields) {
        Query filter = fieldNamesFieldType.termQuery(field, context);
        boolFilterBuilder.add(filter, BooleanClause.Occur.SHOULD);
    }
    return new ConstantScoreQuery(boolFilterBuilder.build());
}
Also used : FieldNamesFieldMapper(org.elasticsearch.index.mapper.FieldNamesFieldMapper) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery)

Example 3 with Query

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

the class FieldMaskingSpanQueryBuilder method doToQuery.

@Override
protected SpanQuery doToQuery(QueryShardContext context) throws IOException {
    String fieldInQuery = fieldName;
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        fieldInQuery = fieldType.name();
    }
    Query innerQuery = queryBuilder.toQuery(context);
    assert innerQuery instanceof SpanQuery;
    return new FieldMaskingSpanQuery((SpanQuery) innerQuery, fieldInQuery);
}
Also used : Query(org.apache.lucene.search.Query) FieldMaskingSpanQuery(org.apache.lucene.search.spans.FieldMaskingSpanQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) FieldMaskingSpanQuery(org.apache.lucene.search.spans.FieldMaskingSpanQuery) FieldMaskingSpanQuery(org.apache.lucene.search.spans.FieldMaskingSpanQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 4 with Query

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

the class SortBuilder method resolveNested.

protected static Nested resolveNested(QueryShardContext context, String nestedPath, QueryBuilder nestedFilter) throws IOException {
    Nested nested = null;
    if (nestedPath != null) {
        BitSetProducer rootDocumentsFilter = context.bitsetFilter(Queries.newNonNestedFilter());
        ObjectMapper nestedObjectMapper = context.getObjectMapper(nestedPath);
        if (nestedObjectMapper == null) {
            throw new QueryShardException(context, "[nested] failed to find nested object under path [" + nestedPath + "]");
        }
        if (!nestedObjectMapper.nested().isNested()) {
            throw new QueryShardException(context, "[nested] nested object under path [" + nestedPath + "] is not of nested type");
        }
        Query innerDocumentsQuery;
        if (nestedFilter != null) {
            context.nestedScope().nextLevel(nestedObjectMapper);
            innerDocumentsQuery = QueryBuilder.rewriteQuery(nestedFilter, context).toFilter(context);
            context.nestedScope().previousLevel();
        } else {
            innerDocumentsQuery = nestedObjectMapper.nestedTypeFilter();
        }
        nested = new Nested(rootDocumentsFilter, innerDocumentsQuery);
    }
    return nested;
}
Also used : Query(org.apache.lucene.search.Query) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) QueryShardException(org.elasticsearch.index.query.QueryShardException) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Example 5 with Query

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

the class CustomFieldQuery method convertMultiPhraseQuery.

private void convertMultiPhraseQuery(int currentPos, int[] termsIdx, MultiPhraseQuery orig, Term[][] terms, int[] pos, IndexReader reader, Collection<Query> flatQueries) throws IOException {
    if (currentPos == 0) {
        // if we have more than 16 terms
        int numTerms = 0;
        for (Term[] currentPosTerm : terms) {
            numTerms += currentPosTerm.length;
        }
        if (numTerms > 16) {
            for (Term[] currentPosTerm : terms) {
                for (Term term : currentPosTerm) {
                    super.flatten(new TermQuery(term), reader, flatQueries, 1F);
                }
            }
            return;
        }
    }
    /*
         * we walk all possible ways and for each path down the MPQ we create a PhraseQuery this is what FieldQuery supports.
         * It seems expensive but most queries will pretty small.
         */
    if (currentPos == terms.length) {
        PhraseQuery.Builder queryBuilder = new PhraseQuery.Builder();
        queryBuilder.setSlop(orig.getSlop());
        for (int i = 0; i < termsIdx.length; i++) {
            queryBuilder.add(terms[i][termsIdx[i]], pos[i]);
        }
        Query query = queryBuilder.build();
        this.flatten(query, reader, flatQueries, 1F);
    } else {
        Term[] t = terms[currentPos];
        for (int i = 0; i < t.length; i++) {
            termsIdx[currentPos] = i;
            convertMultiPhraseQuery(currentPos + 1, termsIdx, orig, terms, pos, reader, flatQueries);
        }
    }
}
Also used : 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) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BoostingQuery(org.apache.lucene.queries.BoostingQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) Term(org.apache.lucene.index.Term)

Aggregations

Query (org.apache.lucene.search.Query)1425 BooleanQuery (org.apache.lucene.search.BooleanQuery)682 TermQuery (org.apache.lucene.search.TermQuery)651 Term (org.apache.lucene.index.Term)405 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)372 IndexSearcher (org.apache.lucene.search.IndexSearcher)297 BoostQuery (org.apache.lucene.search.BoostQuery)290 Test (org.junit.Test)265 PhraseQuery (org.apache.lucene.search.PhraseQuery)250 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)238 TopDocs (org.apache.lucene.search.TopDocs)236 Document (org.apache.lucene.document.Document)221 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)205 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)199 PrefixQuery (org.apache.lucene.search.PrefixQuery)192 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)181 IndexReader (org.apache.lucene.index.IndexReader)170 RegexpQuery (org.apache.lucene.search.RegexpQuery)163 ArrayList (java.util.ArrayList)150 WildcardQuery (org.apache.lucene.search.WildcardQuery)142