Search in sources :

Example 26 with MappedFieldType

use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.

the class TermsSetQueryBuilder method createTermQueries.

/**
 * Visible only for testing purposes.
 */
List<Query> createTermQueries(QueryShardContext context) {
    final MappedFieldType fieldType = context.fieldMapper(fieldName);
    final List<Query> queries = new ArrayList<>(values.size());
    for (Object value : values) {
        if (fieldType != null) {
            queries.add(fieldType.termQuery(value, context));
        } else {
            queries.add(new TermQuery(new Term(fieldName, BytesRefs.toBytesRef(value))));
        }
    }
    return queries;
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) CoveringQuery(org.apache.lucene.sandbox.search.CoveringQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term)

Example 27 with MappedFieldType

use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.

the class WildcardQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        throw new IllegalStateException("Rewrite first");
    }
    MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(rewrite, null, LoggingDeprecationHandler.INSTANCE);
    return fieldType.wildcardQuery(value, method, caseInsensitive, context);
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MappedFieldType(org.opensearch.index.mapper.MappedFieldType)

Example 28 with MappedFieldType

use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.

the class DecayFunctionBuilder method parseVariable.

private AbstractDistanceScoreFunction parseVariable(String fieldName, XContentParser parser, QueryShardContext context, MultiValueMode mode) throws IOException {
    // the field must exist, else we cannot read the value for the doc later
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        throw new ParsingException(parser.getTokenLocation(), "unknown field [{}]", fieldName);
    }
    // dates and time and geo need special handling
    parser.nextToken();
    // TODO these ain't gonna work with runtime fields
    if (fieldType instanceof DateFieldMapper.DateFieldType) {
        return parseDateVariable(parser, context, fieldType, mode);
    } else if (fieldType instanceof GeoPointFieldType) {
        return parseGeoVariable(parser, context, fieldType, mode);
    } else if (fieldType instanceof NumberFieldMapper.NumberFieldType) {
        return parseNumberVariable(parser, context, fieldType, mode);
    } else {
        throw new ParsingException(parser.getTokenLocation(), "field [{}] is of type [{}], but only numeric types are supported.", fieldName, fieldType);
    }
}
Also used : ParsingException(org.opensearch.common.ParsingException) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) GeoPointFieldType(org.opensearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType)

Example 29 with MappedFieldType

use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.

the class RegexpQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws QueryShardException, IOException {
    final int maxAllowedRegexLength = context.getIndexSettings().getMaxRegexLength();
    if (value.length() > maxAllowedRegexLength) {
        throw new IllegalArgumentException("The length of regex [" + value.length() + "] used in the Regexp Query request has exceeded " + "the allowed maximum of [" + maxAllowedRegexLength + "]. " + "This maximum can be set by changing the [" + IndexSettings.MAX_REGEX_LENGTH_SETTING.getKey() + "] index level setting.");
    }
    MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(rewrite, null, LoggingDeprecationHandler.INSTANCE);
    int matchFlagsValue = caseInsensitive ? RegExp.ASCII_CASE_INSENSITIVE : 0;
    Query query = null;
    // For BWC we mask irrelevant bits (RegExp changed ALL from 0xffff to 0xff)
    int sanitisedSyntaxFlag = syntaxFlagsValue & RegExp.ALL;
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        query = fieldType.regexpQuery(value, sanitisedSyntaxFlag, matchFlagsValue, maxDeterminizedStates, method, context);
    }
    if (query == null) {
        if (method == null) {
            method = MultiTermQuery.CONSTANT_SCORE_REWRITE;
        }
        query = new RegexpQuery(new Term(fieldName, BytesRefs.toBytesRef(value)), sanitisedSyntaxFlag, matchFlagsValue, RegexpQuery.DEFAULT_PROVIDER, maxDeterminizedStates, method);
    }
    return query;
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term) RegexpQuery(org.apache.lucene.search.RegexpQuery)

Example 30 with MappedFieldType

use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.

the class IntervalQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    MappedFieldType fieldType = context.fieldMapper(field);
    if (fieldType == null) {
        // Be lenient with unmapped fields so that cross-index search will work nicely
        return new MatchNoDocsQuery();
    }
    Set<String> maskedFields = new HashSet<>();
    sourceProvider.extractFields(maskedFields);
    for (String maskedField : maskedFields) {
        MappedFieldType ft = context.fieldMapper(maskedField);
        if (ft == null) {
            // Be lenient with unmapped fields so that cross-index search will work nicely
            return new MatchNoDocsQuery();
        }
    }
    return new IntervalQuery(field, sourceProvider.getSource(context, fieldType));
}
Also used : MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) HashSet(java.util.HashSet)

Aggregations

MappedFieldType (org.opensearch.index.mapper.MappedFieldType)321 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)150 IndexReader (org.apache.lucene.index.IndexReader)146 Directory (org.apache.lucene.store.Directory)136 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)130 RandomIndexWriter (org.apache.lucene.tests.index.RandomIndexWriter)127 IndexSearcher (org.apache.lucene.search.IndexSearcher)124 Document (org.apache.lucene.document.Document)96 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)83 ArrayList (java.util.ArrayList)76 BytesRef (org.apache.lucene.util.BytesRef)64 Query (org.apache.lucene.search.Query)57 Script (org.opensearch.script.Script)51 IntPoint (org.apache.lucene.document.IntPoint)49 IOException (java.io.IOException)48 List (java.util.List)48 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)48 LongPoint (org.apache.lucene.document.LongPoint)47 NumberFieldMapper (org.opensearch.index.mapper.NumberFieldMapper)46 KeywordFieldMapper (org.opensearch.index.mapper.KeywordFieldMapper)42