Search in sources :

Example 16 with Query

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

the class SpanContainingQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query innerBig = big.toQuery(context);
    assert innerBig instanceof SpanQuery;
    Query innerLittle = little.toQuery(context);
    assert innerLittle instanceof SpanQuery;
    return new SpanContainingQuery((SpanQuery) innerBig, (SpanQuery) innerLittle);
}
Also used : Query(org.apache.lucene.search.Query) SpanContainingQuery(org.apache.lucene.search.spans.SpanContainingQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanContainingQuery(org.apache.lucene.search.spans.SpanContainingQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 17 with Query

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

the class FuzzyQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query query = null;
    String rewrite = this.rewrite;
    if (rewrite == null && context.isFilter()) {
        rewrite = QueryParsers.CONSTANT_SCORE.getPreferredName();
    }
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        query = fieldType.fuzzyQuery(value, fuzziness, prefixLength, maxExpansions, transpositions);
    }
    if (query == null) {
        int maxEdits = fuzziness.asDistance(BytesRefs.toString(value));
        query = new FuzzyQuery(new Term(fieldName, BytesRefs.toBytesRef(value)), maxEdits, prefixLength, maxExpansions, transpositions);
    }
    if (query instanceof MultiTermQuery) {
        MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod(rewrite, null);
        QueryParsers.setRewriteMethod((MultiTermQuery) query, rewriteMethod);
    }
    return query;
}
Also used : Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 18 with Query

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

the class GeoBoundingBoxQueryBuilder method doToQuery.

@Override
public Query doToQuery(QueryShardContext context) {
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "failed to find geo_point field [" + fieldName + "]");
        }
    }
    if (!(fieldType instanceof GeoPointFieldType)) {
        throw new QueryShardException(context, "field [" + fieldName + "] is not a geo_point field");
    }
    QueryValidationException exception = checkLatLon(context.indexVersionCreated().before(Version.V_2_0_0));
    if (exception != null) {
        throw new QueryShardException(context, "couldn't validate latitude/ longitude values", exception);
    }
    GeoPoint luceneTopLeft = new GeoPoint(topLeft);
    GeoPoint luceneBottomRight = new GeoPoint(bottomRight);
    final Version indexVersionCreated = context.indexVersionCreated();
    if (indexVersionCreated.onOrAfter(Version.V_2_2_0) || GeoValidationMethod.isCoerce(validationMethod)) {
        // Special case: if the difference between the left and right is 360 and the right is greater than the left, we are asking for
        // the complete longitude range so need to set longitude to the complete longitude range
        double right = luceneBottomRight.getLon();
        double left = luceneTopLeft.getLon();
        boolean completeLonRange = ((right - left) % 360 == 0 && right > left);
        GeoUtils.normalizePoint(luceneTopLeft, true, !completeLonRange);
        GeoUtils.normalizePoint(luceneBottomRight, true, !completeLonRange);
        if (completeLonRange) {
            luceneTopLeft.resetLon(-180);
            luceneBottomRight.resetLon(180);
        }
    }
    Query query = LatLonPoint.newBoxQuery(fieldType.name(), luceneBottomRight.getLat(), luceneTopLeft.getLat(), luceneTopLeft.getLon(), luceneBottomRight.getLon());
    if (fieldType.hasDocValues()) {
        Query dvQuery = LatLonDocValuesField.newBoxQuery(fieldType.name(), luceneBottomRight.getLat(), luceneTopLeft.getLat(), luceneTopLeft.getLon(), luceneBottomRight.getLon());
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) Version(org.elasticsearch.Version) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) GeoPointFieldType(org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery)

Example 19 with Query

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

the class GeoDistanceQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext shardContext) throws IOException {
    MappedFieldType fieldType = shardContext.fieldMapper(fieldName);
    if (fieldType == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(shardContext, "failed to find geo_point field [" + fieldName + "]");
        }
    }
    if (!(fieldType instanceof GeoPointFieldType)) {
        throw new QueryShardException(shardContext, "field [" + fieldName + "] is not a geo_point field");
    }
    final Version indexVersionCreated = shardContext.indexVersionCreated();
    QueryValidationException exception = checkLatLon(shardContext.indexVersionCreated().before(Version.V_2_0_0));
    if (exception != null) {
        throw new QueryShardException(shardContext, "couldn't validate latitude/ longitude values", exception);
    }
    if (indexVersionCreated.onOrAfter(Version.V_2_2_0) || GeoValidationMethod.isCoerce(validationMethod)) {
        GeoUtils.normalizePoint(center, true, true);
    }
    Query query = LatLonPoint.newDistanceQuery(fieldType.name(), center.lat(), center.lon(), this.distance);
    if (fieldType.hasDocValues()) {
        Query dvQuery = LatLonDocValuesField.newDistanceQuery(fieldType.name(), center.lat(), center.lon(), this.distance);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) Version(org.elasticsearch.Version) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) GeoPointFieldType(org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery)

Example 20 with Query

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

the class SpanNearQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    SpanQuery[] spanQueries = new SpanQuery[clauses.size()];
    for (int i = 0; i < clauses.size(); i++) {
        Query query = clauses.get(i).toQuery(context);
        assert query instanceof SpanQuery;
        spanQueries[i] = (SpanQuery) query;
    }
    return new SpanNearQuery(spanQueries, slop, inOrder);
}
Also used : Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Aggregations

Query (org.apache.lucene.search.Query)1371 BooleanQuery (org.apache.lucene.search.BooleanQuery)663 TermQuery (org.apache.lucene.search.TermQuery)633 Term (org.apache.lucene.index.Term)395 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)359 BoostQuery (org.apache.lucene.search.BoostQuery)284 IndexSearcher (org.apache.lucene.search.IndexSearcher)283 Test (org.junit.Test)258 PhraseQuery (org.apache.lucene.search.PhraseQuery)247 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)234 TopDocs (org.apache.lucene.search.TopDocs)224 Document (org.apache.lucene.document.Document)216 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)205 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)194 PrefixQuery (org.apache.lucene.search.PrefixQuery)188 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)174 IndexReader (org.apache.lucene.index.IndexReader)167 RegexpQuery (org.apache.lucene.search.RegexpQuery)159 WildcardQuery (org.apache.lucene.search.WildcardQuery)141 ArrayList (java.util.ArrayList)139