use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.
the class FieldSortBuilder method isBottomSortShardDisjoint.
/**
* Returns whether some values of the given {@link QueryShardContext#getIndexReader()} are within the
* primary sort value provided in the <code>bottomSortValues</code>.
*/
public boolean isBottomSortShardDisjoint(QueryShardContext context, SearchSortValuesAndFormats bottomSortValues) throws IOException {
if (bottomSortValues == null || bottomSortValues.getRawSortValues().length == 0) {
return false;
}
if (canRewriteToMatchNone() == false) {
return false;
}
MappedFieldType fieldType = context.fieldMapper(fieldName);
if (fieldType == null) {
// unmapped
return false;
}
if (fieldType.isSearchable() == false) {
return false;
}
DocValueFormat docValueFormat = bottomSortValues.getSortValueFormats()[0];
final DateMathParser dateMathParser;
if (docValueFormat instanceof DocValueFormat.DateTime) {
dateMathParser = ((DocValueFormat.DateTime) docValueFormat).getDateMathParser();
} else {
dateMathParser = null;
}
Object bottomSortValue = bottomSortValues.getFormattedSortValues()[0];
Object minValue = order() == SortOrder.DESC ? bottomSortValue : null;
Object maxValue = order() == SortOrder.DESC ? null : bottomSortValue;
try {
MappedFieldType.Relation relation = fieldType.isFieldWithinQuery(context.getIndexReader(), minValue, maxValue, true, true, null, dateMathParser, context);
return relation == MappedFieldType.Relation.DISJOINT;
} catch (OpenSearchParseException exc) {
// can happen if the sort field is mapped differently in another search index
return false;
}
}
use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.
the class FieldSortBuilder method getMinMaxOrNull.
/**
* Return the {@link MinAndMax} indexed value from the provided {@link FieldSortBuilder} or <code>null</code> if unknown.
* The value can be extracted on non-nested indexed mapped fields of type keyword, numeric or date, other fields
* and configurations return <code>null</code>.
*/
public static MinAndMax<?> getMinMaxOrNull(QueryShardContext context, FieldSortBuilder sortBuilder) throws IOException {
SortAndFormats sort = SortBuilder.buildSort(Collections.singletonList(sortBuilder), context).get();
SortField sortField = sort.sort.getSort()[0];
if (sortField.getField() == null) {
return null;
}
IndexReader reader = context.getIndexReader();
MappedFieldType fieldType = context.fieldMapper(sortField.getField());
if (reader == null || (fieldType == null || fieldType.isSearchable() == false)) {
return null;
}
switch(IndexSortConfig.getSortFieldType(sortField)) {
case LONG:
case INT:
case DOUBLE:
case FLOAT:
return extractNumericMinAndMax(reader, sortField, fieldType, sortBuilder);
case STRING:
case STRING_VAL:
if (fieldType instanceof KeywordFieldMapper.KeywordFieldType) {
Terms terms = MultiTerms.getTerms(reader, fieldType.name());
if (terms == null) {
return null;
}
return terms.getMin() != null ? new MinAndMax<>(terms.getMin(), terms.getMax()) : null;
}
break;
}
return null;
}
use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.
the class SuggestionBuilder method populateCommonFields.
/**
* Transfers the text, prefix, regex, analyzer, field, size and shard size settings from the
* original {@link SuggestionBuilder} to the target {@link SuggestionContext}
*/
protected void populateCommonFields(MapperService mapperService, SuggestionSearchContext.SuggestionContext suggestionContext) {
Objects.requireNonNull(field, "field must not be null");
MappedFieldType fieldType = mapperService.fieldType(field);
if (fieldType == null) {
throw new IllegalArgumentException("no mapping found for field [" + field + "]");
} else if (analyzer == null) {
// no analyzer name passed in, so try the field's analyzer, or the default analyzer
if (fieldType.getTextSearchInfo().getSearchAnalyzer() == null) {
suggestionContext.setAnalyzer(mapperService.searchAnalyzer());
} else {
suggestionContext.setAnalyzer(fieldType.getTextSearchInfo().getSearchAnalyzer());
}
} else {
Analyzer luceneAnalyzer = mapperService.getNamedAnalyzer(analyzer);
if (luceneAnalyzer == null) {
throw new IllegalArgumentException("analyzer [" + analyzer + "] doesn't exists");
}
suggestionContext.setAnalyzer(luceneAnalyzer);
}
suggestionContext.setField(fieldType.name());
if (size != null) {
suggestionContext.setSize(size);
}
if (shardSize != null) {
suggestionContext.setShardSize(shardSize);
} else {
// if no shard size is set in builder, use size (or at least 5)
suggestionContext.setShardSize(Math.max(suggestionContext.getSize(), 5));
}
if (text != null) {
suggestionContext.setText(BytesRefs.toBytesRef(text));
}
if (prefix != null) {
suggestionContext.setPrefix(BytesRefs.toBytesRef(prefix));
}
if (regex != null) {
suggestionContext.setRegex(BytesRefs.toBytesRef(regex));
}
if (text != null && prefix == null) {
suggestionContext.setPrefix(BytesRefs.toBytesRef(text));
} else if (text == null && prefix != null) {
suggestionContext.setText(BytesRefs.toBytesRef(prefix));
} else if (text == null && regex != null) {
suggestionContext.setText(BytesRefs.toBytesRef(regex));
}
}
use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.
the class UpdateMappingIntegrationIT method assertConcreteMappingsOnAll.
/**
* Waits until mappings for the provided fields exist on all nodes. Note, this waits for the current
* started shards and checks for concrete mappings.
*/
private void assertConcreteMappingsOnAll(final String index, final String... fieldNames) {
Set<String> nodes = internalCluster().nodesInclude(index);
assertThat(nodes, Matchers.not(Matchers.emptyIterable()));
for (String node : nodes) {
IndicesService indicesService = internalCluster().getInstance(IndicesService.class, node);
IndexService indexService = indicesService.indexService(resolveIndex(index));
assertThat("index service doesn't exists on " + node, indexService, notNullValue());
MapperService mapperService = indexService.mapperService();
for (String fieldName : fieldNames) {
MappedFieldType fieldType = mapperService.fieldType(fieldName);
assertNotNull("field " + fieldName + " doesn't exists on " + node, fieldType);
}
}
assertMappingOnClusterManager(index, fieldNames);
}
use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.
the class GeoShapeIntegrationIT method testOrientationPersistence.
/**
* Test that orientation parameter correctly persists across cluster restart
*/
public void testOrientationPersistence() throws Exception {
String idxName = "orientation";
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("location").field("type", "geo_shape").field("orientation", "left").endObject().endObject().endObject());
// create index
assertAcked(prepareCreate(idxName).setMapping(mapping));
mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("location").field("type", "geo_shape").field("orientation", "right").endObject().endObject().endObject());
assertAcked(prepareCreate(idxName + "2").setMapping(mapping));
ensureGreen(idxName, idxName + "2");
internalCluster().fullRestart();
ensureGreen(idxName, idxName + "2");
// left orientation test
IndicesService indicesService = internalCluster().getInstance(IndicesService.class, findNodeName(idxName));
IndexService indexService = indicesService.indexService(resolveIndex(idxName));
MappedFieldType fieldType = indexService.mapperService().fieldType("location");
assertThat(fieldType, instanceOf(GeoShapeFieldMapper.GeoShapeFieldType.class));
GeoShapeFieldMapper.GeoShapeFieldType gsfm = (GeoShapeFieldMapper.GeoShapeFieldType) fieldType;
ShapeBuilder.Orientation orientation = gsfm.orientation();
assertThat(orientation, equalTo(ShapeBuilder.Orientation.CLOCKWISE));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.LEFT));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.CW));
// right orientation test
indicesService = internalCluster().getInstance(IndicesService.class, findNodeName(idxName + "2"));
indexService = indicesService.indexService(resolveIndex((idxName + "2")));
fieldType = indexService.mapperService().fieldType("location");
assertThat(fieldType, instanceOf(GeoShapeFieldMapper.GeoShapeFieldType.class));
gsfm = (GeoShapeFieldMapper.GeoShapeFieldType) fieldType;
orientation = gsfm.orientation();
assertThat(orientation, equalTo(ShapeBuilder.Orientation.COUNTER_CLOCKWISE));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.RIGHT));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.CCW));
}
Aggregations