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);
}
}
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();
}
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;
}
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));
}
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);
}
Aggregations