Search in sources :

Example 11 with MappedFieldType

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;
    }
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) DocValueFormat(org.opensearch.search.DocValueFormat) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DateMathParser(org.opensearch.common.time.DateMathParser)

Example 12 with MappedFieldType

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;
}
Also used : IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Terms(org.apache.lucene.index.Terms) MultiTerms(org.apache.lucene.index.MultiTerms) SortField(org.apache.lucene.search.SortField)

Example 13 with MappedFieldType

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));
    }
}
Also used : MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 14 with MappedFieldType

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);
}
Also used : IndexService(org.opensearch.index.IndexService) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndicesService(org.opensearch.indices.IndicesService) Matchers.containsString(org.hamcrest.Matchers.containsString) MapperService(org.opensearch.index.mapper.MapperService)

Example 15 with MappedFieldType

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));
}
Also used : ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) IndexService(org.opensearch.index.IndexService) GeoShapeFieldMapper(org.opensearch.index.mapper.GeoShapeFieldMapper) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndicesService(org.opensearch.indices.IndicesService) Matchers.containsString(org.hamcrest.Matchers.containsString)

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