Java Code Examples for org.elasticsearch.index.mapper.MappedFieldType#rangeQuery()

The following examples show how to use org.elasticsearch.index.mapper.MappedFieldType#rangeQuery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExistsQueryParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static Query newFilter(QueryParseContext parseContext, String fieldPattern, String queryName) {
    final FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldMapper.FieldNamesFieldType)parseContext.mapperService().fullName(FieldNamesFieldMapper.NAME);
    if (fieldNamesFieldType == null) {
        // can only happen when no types exist, so no docs exist either
        return Queries.newMatchNoDocsQuery();
    }

    ObjectMapper objectMapper = parseContext.getObjectMapper(fieldPattern);
    if (objectMapper != null) {
        // automatic make the object mapper pattern
        fieldPattern = fieldPattern + ".*";
    }

    Collection<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern);
    if (fields.isEmpty()) {
        // no fields exists, so we should not match anything
        return Queries.newMatchNoDocsQuery();
    }

    BooleanQuery.Builder boolFilterBuilder = new BooleanQuery.Builder();
    for (String field : fields) {
        MappedFieldType fieldType = parseContext.fieldMapper(field);
        Query filter = null;
        if (fieldNamesFieldType.isEnabled()) {
            final String f;
            if (fieldType != null) {
                f = fieldType.names().indexName();
            } else {
                f = field;
            }
            filter = fieldNamesFieldType.termQuery(f, parseContext);
        }
        // if _field_names are not indexed, we need to go the slow way
        if (filter == null && fieldType != null) {
            filter = fieldType.rangeQuery(null, null, true, true);
        }
        if (filter == null) {
            filter = new TermRangeQuery(field, null, null, true, true);
        }
        boolFilterBuilder.add(filter, BooleanClause.Occur.SHOULD);
    }

    BooleanQuery boolFilter = boolFilterBuilder.build();
    if (queryName != null) {
        parseContext.addNamedQuery(queryName, boolFilter);
    }
    return new ConstantScoreQuery(boolFilter);
}
 
Example 2
Source File: OptimizeQueryForSearchAfter.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public Query apply(FieldDoc lastCollected) {
    BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    for (int i = 0; i < orderBy.orderBySymbols().size(); i++) {
        Symbol order = orderBy.orderBySymbols().get(i);
        Object value = lastCollected.fields[i];
        if (order instanceof Reference) {
            final ColumnIdent columnIdent = ((Reference) order).column();
            if (columnIdent.isSystemColumn()) {
                // We can't optimize the initial query because the BooleanQuery
                // must not contain system columns.
                return null;
            }
            boolean nullsFirst = orderBy.nullsFirst()[i];
            value = value == null || value.equals(missingValues[i]) ? null : value;
            if (nullsFirst && value == null) {
                // no filter needed
                continue;
            }
            String columnName = columnIdent.fqn();
            MappedFieldType fieldType = requireNonNull(
                fieldTypeLookup.get(columnName), "Column must exist: " + columnName);

            Query orderQuery;
            // nulls already gone, so they should be excluded
            if (nullsFirst) {
                BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
                booleanQuery.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
                if (orderBy.reverseFlags()[i]) {
                    booleanQuery.add(fieldType.rangeQuery(null, value, false, true, null, null, queryShardContext), BooleanClause.Occur.MUST_NOT);
                } else {
                    booleanQuery.add(fieldType.rangeQuery(value, null, true, false, null, null, queryShardContext), BooleanClause.Occur.MUST_NOT);
                }
                orderQuery = booleanQuery.build();
            } else {
                if (orderBy.reverseFlags()[i]) {
                    orderQuery = fieldType.rangeQuery(value, null, false, false, null, null, queryShardContext);
                } else {
                    orderQuery = fieldType.rangeQuery(null, value, false, false, null, null, queryShardContext);
                }
            }
            queryBuilder.add(orderQuery, BooleanClause.Occur.MUST);
        }
    }
    BooleanQuery query = queryBuilder.build();
    if (query.clauses().size() > 0) {
        return query;
    } else {
        return null;
    }
}