Search in sources :

Example 6 with DateFieldType

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

the class QueryStringQueryParser method getFieldQuery.

@Override
public Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException {
    if (field != null && EXISTS_FIELD.equals(field)) {
        return existsQuery(queryText);
    }
    if (quoted) {
        return getFieldQuery(field, queryText, getPhraseSlop());
    }
    // as logical operator by the query parser (e.g. age:>=10).
    if (field != null) {
        if (queryText.length() > 1) {
            if (queryText.charAt(0) == '>') {
                if (queryText.length() > 2) {
                    if (queryText.charAt(1) == '=') {
                        return getRangeQuery(field, queryText.substring(2), null, true, true);
                    }
                }
                return getRangeQuery(field, queryText.substring(1), null, false, true);
            } else if (queryText.charAt(0) == '<') {
                if (queryText.length() > 2) {
                    if (queryText.charAt(1) == '=') {
                        return getRangeQuery(field, null, queryText.substring(2), true, true);
                    }
                }
                return getRangeQuery(field, null, queryText.substring(1), true, false);
            }
            // if we are querying a single date field, we also create a range query that leverages the time zone setting
            if (context.fieldMapper(field) instanceof DateFieldType && this.timeZone != null) {
                return getRangeQuery(field, queryText, queryText, true, true);
            }
        }
    }
    Map<String, Float> fields = extractMultiFields(field, quoted);
    if (fields.isEmpty()) {
        // if there is no match in the mappings.
        return newUnmappedFieldQuery(field);
    }
    Analyzer oldAnalyzer = queryBuilder.analyzer;
    try {
        if (forceAnalyzer != null) {
            queryBuilder.setAnalyzer(forceAnalyzer);
        }
        return queryBuilder.parse(type, fields, queryText, null);
    } catch (IOException e) {
        throw new ParseException(e.getMessage());
    } finally {
        queryBuilder.setAnalyzer(oldAnalyzer);
    }
}
Also used : DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) IOException(java.io.IOException) ParseException(org.apache.lucene.queryparser.classic.ParseException) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 7 with DateFieldType

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

the class QueryPhase method enhanceSortOnNumeric.

private static void enhanceSortOnNumeric(SearchContext searchContext, IndexReader reader) {
    if (canEarlyTerminate(reader, searchContext.sort())) {
        // disable this optimization if index sorting matches the query sort since it's already optimized by index searcher
        return;
    }
    Sort sort = searchContext.sort().sort;
    SortField sortField = sort.getSort()[0];
    if (SortField.Type.LONG.equals(IndexSortConfig.getSortFieldType(sortField)) == false)
        return;
    // check if this is a field of type Long or Date, that is indexed and has doc values
    String fieldName = sortField.getField();
    // happens when _score or _doc is the 1st sort field
    if (fieldName == null)
        return;
    // mapperService can be null in tests
    if (searchContext.mapperService() == null)
        return;
    final MappedFieldType fieldType = searchContext.mapperService().fieldType(fieldName);
    // for unmapped fields, default behaviour depending on "unmapped_type" flag
    if (fieldType == null)
        return;
    if ((fieldType.typeName().equals("long") == false) && (fieldType instanceof DateFieldType == false))
        return;
    if (fieldType.isSearchable() == false)
        return;
    if (fieldType.hasDocValues() == false)
        return;
    sortField.setCanUsePoints();
}
Also used : DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField)

Example 8 with DateFieldType

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

the class FieldSortBuilder method extractNumericMinAndMax.

private static MinAndMax<?> extractNumericMinAndMax(IndexReader reader, SortField sortField, MappedFieldType fieldType, FieldSortBuilder sortBuilder) throws IOException {
    String fieldName = fieldType.name();
    if (PointValues.size(reader, fieldName) == 0) {
        return null;
    }
    if (fieldType instanceof NumberFieldType) {
        NumberFieldType numberFieldType = (NumberFieldType) fieldType;
        Number minPoint = numberFieldType.parsePoint(PointValues.getMinPackedValue(reader, fieldName));
        Number maxPoint = numberFieldType.parsePoint(PointValues.getMaxPackedValue(reader, fieldName));
        switch(IndexSortConfig.getSortFieldType(sortField)) {
            case LONG:
                return new MinAndMax<>(minPoint.longValue(), maxPoint.longValue());
            case INT:
                return new MinAndMax<>(minPoint.intValue(), maxPoint.intValue());
            case DOUBLE:
                return new MinAndMax<>(minPoint.doubleValue(), maxPoint.doubleValue());
            case FLOAT:
                return new MinAndMax<>(minPoint.floatValue(), maxPoint.floatValue());
            default:
                return null;
        }
    } else if (fieldType instanceof DateFieldType) {
        DateFieldType dateFieldType = (DateFieldType) fieldType;
        Function<byte[], Long> dateConverter = createDateConverter(sortBuilder, dateFieldType);
        Long min = dateConverter.apply(PointValues.getMinPackedValue(reader, fieldName));
        Long max = dateConverter.apply(PointValues.getMaxPackedValue(reader, fieldName));
        return new MinAndMax<>(min, max);
    }
    return null;
}
Also used : Function(java.util.function.Function) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType)

Example 9 with DateFieldType

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

the class DateFieldTypeTests method testIsFieldWithinRangeEmptyReader.

public void testIsFieldWithinRangeEmptyReader() throws IOException {
    QueryRewriteContext context = new QueryRewriteContext(xContentRegistry(), writableRegistry(), null, () -> nowInMillis);
    IndexReader reader = new MultiReader();
    DateFieldType ft = new DateFieldType("my_date");
    assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", randomBoolean(), randomBoolean(), null, null, context));
}
Also used : DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) MultiReader(org.apache.lucene.index.MultiReader) QueryRewriteContext(org.opensearch.index.query.QueryRewriteContext) IndexReader(org.apache.lucene.index.IndexReader)

Example 10 with DateFieldType

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

the class DateFieldTypeTests method isFieldWithinRangeTestCase.

public void isFieldWithinRangeTestCase(DateFieldType ft) throws IOException {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    Document doc = new Document();
    LongPoint field = new LongPoint("my_date", ft.parse("2015-10-12"));
    doc.add(field);
    w.addDocument(doc);
    field.setLongValue(ft.parse("2016-04-03"));
    w.addDocument(doc);
    DirectoryReader reader = DirectoryReader.open(w);
    DateMathParser alternateFormat = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.toDateMathParser();
    doTestIsFieldWithinQuery(ft, reader, null, null);
    doTestIsFieldWithinQuery(ft, reader, null, alternateFormat);
    doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, null);
    doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, alternateFormat);
    QueryRewriteContext context = new QueryRewriteContext(xContentRegistry(), writableRegistry(), null, () -> nowInMillis);
    // Fields with no value indexed.
    DateFieldType ft2 = new DateFieldType("my_date2");
    assertEquals(Relation.DISJOINT, ft2.isFieldWithinQuery(reader, "2015-10-09", "2016-01-02", false, false, null, null, context));
    IOUtils.close(reader, w, dir);
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) QueryRewriteContext(org.opensearch.index.query.QueryRewriteContext) LongPoint(org.apache.lucene.document.LongPoint) DateMathParser(org.opensearch.common.time.DateMathParser) Document(org.opensearch.index.mapper.ParseContext.Document) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

DateFieldType (org.opensearch.index.mapper.DateFieldMapper.DateFieldType)14 Query (org.apache.lucene.search.Query)6 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)5 QueryShardContext (org.opensearch.index.query.QueryShardContext)5 IndexSortSortedNumericDocValuesRangeQuery (org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Settings (org.opensearch.common.settings.Settings)3 IndexSettings (org.opensearch.index.IndexSettings)3 DateRangeIncludingNowQuery (org.opensearch.index.query.DateRangeIncludingNowQuery)3 MultiReader (org.apache.lucene.index.MultiReader)2 BinaryDocValuesRangeQuery (org.apache.lucene.queries.BinaryDocValuesRangeQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 DateFormatter (org.opensearch.common.time.DateFormatter)2 RangeFieldType (org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType)2 QueryRewriteContext (org.opensearch.index.query.QueryRewriteContext)2 IOException (java.io.IOException)1 Function (java.util.function.Function)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 LongPoint (org.apache.lucene.document.LongPoint)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1