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

The following examples show how to use org.elasticsearch.index.mapper.MappedFieldType#termQuery() . 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: AnyEqQuery.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Query literalMatchesAnyArrayRef(Function any,
                                               Literal candidate,
                                               Reference array,
                                               LuceneQueryBuilder.Context context) {
    MappedFieldType fieldType = context.getFieldTypeOrNull(array.column().fqn());
    if (fieldType == null) {
        if (ArrayType.unnest(array.valueType()).id() == ObjectType.ID) {
            return genericFunctionFilter(any, context); // {x=10} = any(objects)
        }
        return Queries.newMatchNoDocsQuery("column doesn't exist in this index");
    }
    if (DataTypes.isArray(candidate.valueType())) {
        return arrayLiteralEqAnyArray(any, fieldType, candidate.value(), context);
    }
    return fieldType.termQuery(candidate.value(), context.queryShardContext());
}
 
Example 2
Source File: GeohashCellQuery.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new geohash filter for a given set of geohashes. In general this method
 * returns a boolean filter combining the geohashes OR-wise.
 *
 * @param context     Context of the filter
 * @param fieldType field mapper for geopoints
 * @param geohash     mandatory geohash
 * @param geohashes   optional array of additional geohashes
 * @return a new GeoBoundinboxfilter
 */
public static Query create(QueryParseContext context, BaseGeoPointFieldMapper.GeoPointFieldType fieldType, String geohash,
                           @Nullable List<CharSequence> geohashes) {
    MappedFieldType geoHashMapper = fieldType.geoHashFieldType();
    if (geoHashMapper == null) {
        throw new IllegalArgumentException("geohash filter needs geohash_prefix to be enabled");
    }

    if (geohashes == null || geohashes.size() == 0) {
        return geoHashMapper.termQuery(geohash, context);
    } else {
        geohashes.add(geohash);
        return geoHashMapper.termsQuery(geohashes, context);
    }
}
 
Example 3
Source File: MatchQuery.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected final Query termQuery(MappedFieldType fieldType, Object value, boolean lenient) {
    try {
        return fieldType.termQuery(value, parseContext);
    } catch (RuntimeException e) {
        if (lenient) {
            return null;
        }
        throw e;
    }
}
 
Example 4
Source File: MatchQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
protected final Query termQuery(MappedFieldType fieldType, BytesRef value, boolean lenient) {
    try {
        return fieldType.termQuery(value, context);
    } catch (RuntimeException e) {
        if (lenient) {
            return newLenientFieldQuery(fieldType.name(), e);
        }
        throw e;
    }
}
 
Example 5
Source File: EqQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query apply(Function input, LuceneQueryBuilder.Context context) {
    RefAndLiteral refAndLiteral = RefAndLiteral.of(input);
    if (refAndLiteral == null) {
        return null;
    }
    Reference reference = refAndLiteral.reference();
    Literal literal = refAndLiteral.literal();
    String columnName = reference.column().fqn();
    MappedFieldType fieldType = context.getFieldTypeOrNull(columnName);
    if (reference.valueType().id() == ObjectType.ID) {
        //noinspection unchecked
        return refEqObject(input, reference, (Map<String, Object>) literal.value(), context);
    }
    if (fieldType == null) {
        // field doesn't exist, can't match
        return Queries.newMatchNoDocsQuery("column does not exist in this index");
    }
    if (DataTypes.isArray(reference.valueType()) &&
        DataTypes.isArray(literal.valueType())) {

        List values = LuceneQueryBuilder.asList(literal);
        if (values.isEmpty()) {
            return genericFunctionFilter(input, context);
        }
        Query termsQuery = LuceneQueryBuilder.termsQuery(fieldType, values, context.queryShardContext);

        // wrap boolTermsFilter and genericFunction filter in an additional BooleanFilter to control the ordering of the filters
        // termsFilter is applied first
        // afterwards the more expensive genericFunctionFilter
        BooleanQuery.Builder filterClauses = new BooleanQuery.Builder();
        filterClauses.add(termsQuery, BooleanClause.Occur.MUST);
        filterClauses.add(genericFunctionFilter(input, context), BooleanClause.Occur.MUST);
        return filterClauses.build();
    }
    return fieldType.termQuery(literal.value(), context.queryShardContext);
}
 
Example 6
Source File: LuceneQueryBuilder.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query visitReference(Reference symbol, Context context) {
    // called for queries like: where boolColumn
    if (symbol.valueType() == DataTypes.BOOLEAN) {
        MappedFieldType fieldType = context.getFieldTypeOrNull(symbol.column().fqn());
        if (fieldType == null) {
            return Queries.newMatchNoDocsQuery("column does not exist in this index");
        }
        return fieldType.termQuery(true, context.queryShardContext());
    }
    return super.visitReference(symbol, context);
}
 
Example 7
Source File: LikeQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
public static Query like(DataType dataType, @Nullable MappedFieldType fieldType, Object value, boolean ignoreCase) {
    if (fieldType == null) {
        // column doesn't exist on this index -> no match
        return Queries.newMatchNoDocsQuery("column does not exist in this index");
    }

    if (dataType.equals(DataTypes.STRING)) {
        return createCaseAwareQuery(
            fieldType.name(),
            BytesRefs.toString(value),
            ignoreCase);
    }
    return fieldType.termQuery(value, null);
}
 
Example 8
Source File: MatchQueryBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
protected Query singleQuery(MatchQuery.Type type, String fieldName, BytesRef queryString) {
    String field;
    MappedFieldType fieldType = mapperService.smartNameFieldType(fieldName);
    if (fieldType == null) {
        field = fieldName;
    } else {
        field = fieldType.names().indexName();
    }

    if (fieldType != null && fieldType.useTermQueryWithQueryString() && !forceAnalyzeQueryString()) {
        try {
           return fieldType.termQuery(queryString, null);
        } catch (RuntimeException e) {
            return null;
        }
    }

    Analyzer analyzer = getAnalyzer(fieldType);
    InnerQueryBuilder builder = new InnerQueryBuilder(analyzer, fieldType);

    Query query;
    switch (type) {
        case BOOLEAN:
            if (options.commonTermsCutoff() == null) {
                query = builder.createBooleanQuery(field, BytesRefs.toString(queryString), options.operator());
            } else {
                query = builder.createCommonTermsQuery(
                        field,
                        BytesRefs.toString(queryString),
                        options.operator(),
                        options.operator(),
                        options.commonTermsCutoff(),
                        fieldType
                );
            }
            break;
        case PHRASE:
            query = builder.createPhraseQuery(field, BytesRefs.toString(queryString), options.phraseSlop());
            break;
        case PHRASE_PREFIX:
            query = builder.createPhrasePrefixQuery(
                    field,
                    BytesRefs.toString(queryString),
                    options.phraseSlop(),
                    options.maxExpansions()
            );
            break;
        default:
            throw new IllegalArgumentException("invalid type: " + type.toString());
    }

    if (query == null) {
        return zeroTermsQuery();
    } else {
        return query;
    }
}