Search in sources :

Example 1 with Nested

use of org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested in project OpenSearch by opensearch-project.

the class FieldSortBuilder method buildBucketedSort.

@Override
public BucketedSort buildBucketedSort(QueryShardContext context, int bucketSize, BucketedSort.ExtraData extra) throws IOException {
    if (DOC_FIELD_NAME.equals(fieldName)) {
        throw new IllegalArgumentException("sorting by _doc is not supported");
    }
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    Nested nested = nested(context, fieldType);
    if (fieldType == null) {
        fieldType = resolveUnmappedType(context);
    }
    IndexFieldData<?> fieldData = context.getForField(fieldType);
    if (fieldData instanceof IndexNumericFieldData == false && (sortMode == SortMode.SUM || sortMode == SortMode.AVG || sortMode == SortMode.MEDIAN)) {
        throw new QueryShardException(context, "we only support AVG, MEDIAN and SUM on number based fields");
    }
    if (numericType != null) {
        if (fieldData instanceof IndexNumericFieldData == false) {
            throw new QueryShardException(context, "[numeric_type] option cannot be set on a non-numeric field, got " + fieldType.typeName());
        }
        IndexNumericFieldData numericFieldData = (IndexNumericFieldData) fieldData;
        NumericType resolvedType = resolveNumericType(numericType);
        return numericFieldData.newBucketedSort(resolvedType, context.bigArrays(), missing, localSortMode(), nested, order, fieldType.docValueFormat(null, null), bucketSize, extra);
    }
    try {
        return fieldData.newBucketedSort(context.bigArrays(), missing, localSortMode(), nested, order, fieldType.docValueFormat(null, null), bucketSize, extra);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("error building sort for field [" + fieldName + "] of type [" + fieldType.typeName() + "] in index [" + context.index().getName() + "]: " + e.getMessage(), e);
    }
}
Also used : NumericType(org.opensearch.index.fielddata.IndexNumericFieldData.NumericType) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 2 with Nested

use of org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested in project OpenSearch by opensearch-project.

the class GeoDistanceSortBuilder method buildBucketedSort.

@Override
public BucketedSort buildBucketedSort(QueryShardContext context, int bucketSize, BucketedSort.ExtraData extra) throws IOException {
    GeoPoint[] localPoints = localPoints();
    MultiValueMode localSortMode = localSortMode();
    IndexGeoPointFieldData geoIndexFieldData = fieldData(context);
    Nested nested = nested(context);
    return comparatorSource(localPoints, localSortMode, geoIndexFieldData, nested).newBucketedSort(context.bigArrays(), order, DocValueFormat.RAW, bucketSize, extra);
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) IndexGeoPointFieldData(org.opensearch.index.fielddata.IndexGeoPointFieldData) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) MultiValueMode(org.opensearch.search.MultiValueMode)

Example 3 with Nested

use of org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested in project OpenSearch by opensearch-project.

the class ScriptSortBuilder method fieldComparatorSource.

private IndexFieldData.XFieldComparatorSource fieldComparatorSource(QueryShardContext context) throws IOException {
    MultiValueMode valueMode = null;
    if (sortMode != null) {
        valueMode = MultiValueMode.fromString(sortMode.toString());
    }
    if (valueMode == null) {
        valueMode = order == SortOrder.DESC ? MultiValueMode.MAX : MultiValueMode.MIN;
    }
    final Nested nested;
    if (nestedSort != null) {
        // new nested sorts takes priority
        validateMaxChildrenExistOnlyInTopLevelNestedSort(context, nestedSort);
        nested = resolveNested(context, nestedSort);
    } else {
        nested = resolveNested(context, nestedPath, nestedFilter);
    }
    switch(type) {
        case STRING:
            final StringSortScript.Factory factory = context.compile(script, StringSortScript.CONTEXT);
            final StringSortScript.LeafFactory searchScript = factory.newFactory(script.getParams(), context.lookup());
            return new BytesRefFieldComparatorSource(null, null, valueMode, nested) {

                StringSortScript leafScript;

                @Override
                protected SortedBinaryDocValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = searchScript.newInstance(context);
                    final BinaryDocValues values = new AbstractBinaryDocValues() {

                        final BytesRefBuilder spare = new BytesRefBuilder();

                        @Override
                        public boolean advanceExact(int doc) throws IOException {
                            leafScript.setDocument(doc);
                            return true;
                        }

                        @Override
                        public BytesRef binaryValue() {
                            spare.copyChars(leafScript.execute());
                            return spare.get();
                        }
                    };
                    return FieldData.singleton(values);
                }

                @Override
                protected void setScorer(Scorable scorer) {
                    leafScript.setScorer(scorer);
                }

                @Override
                public BucketedSort newBucketedSort(BigArrays bigArrays, SortOrder sortOrder, DocValueFormat format, int bucketSize, BucketedSort.ExtraData extra) {
                    throw new IllegalArgumentException("error building sort for [_script]: " + "script sorting only supported on [numeric] scripts but was [" + type + "]");
                }
            };
        case NUMBER:
            final NumberSortScript.Factory numberSortFactory = context.compile(script, NumberSortScript.CONTEXT);
            final NumberSortScript.LeafFactory numberSortScript = numberSortFactory.newFactory(script.getParams(), context.lookup());
            return new DoubleValuesComparatorSource(null, Double.MAX_VALUE, valueMode, nested) {

                NumberSortScript leafScript;

                @Override
                protected SortedNumericDoubleValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = numberSortScript.newInstance(context);
                    final NumericDoubleValues values = new NumericDoubleValues() {

                        @Override
                        public boolean advanceExact(int doc) throws IOException {
                            leafScript.setDocument(doc);
                            return true;
                        }

                        @Override
                        public double doubleValue() {
                            return leafScript.execute();
                        }
                    };
                    return FieldData.singleton(values);
                }

                @Override
                protected void setScorer(Scorable scorer) {
                    leafScript.setScorer(scorer);
                }
            };
        default:
            throw new QueryShardException(context, "custom script sort type [" + type + "] not supported");
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DoubleValuesComparatorSource(org.opensearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource) Scorable(org.apache.lucene.search.Scorable) DocValueFormat(org.opensearch.search.DocValueFormat) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) BytesRefFieldComparatorSource(org.opensearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource) StringSortScript(org.opensearch.script.StringSortScript) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) AbstractBinaryDocValues(org.opensearch.index.fielddata.AbstractBinaryDocValues) MultiValueMode(org.opensearch.search.MultiValueMode) AbstractBinaryDocValues(org.opensearch.index.fielddata.AbstractBinaryDocValues) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) NumberSortScript(org.opensearch.script.NumberSortScript) BigArrays(org.opensearch.common.util.BigArrays) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 4 with Nested

use of org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested in project OpenSearch by opensearch-project.

the class SortBuilder method resolveNested.

protected static Nested resolveNested(QueryShardContext context, NestedSortBuilder nestedSort) throws IOException {
    final Query childQuery = resolveNestedQuery(context, nestedSort, null);
    if (childQuery == null) {
        return null;
    }
    final ObjectMapper objectMapper = context.nestedScope().getObjectMapper();
    final Query parentQuery;
    if (objectMapper == null) {
        parentQuery = Queries.newNonNestedFilter(context.indexVersionCreated());
    } else {
        parentQuery = objectMapper.nestedTypeFilter();
    }
    return new Nested(context.bitsetFilter(parentQuery), childQuery, nestedSort, context.searcher());
}
Also used : Query(org.apache.lucene.search.Query) ToChildBlockJoinQuery(org.apache.lucene.search.join.ToChildBlockJoinQuery) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) ObjectMapper(org.opensearch.index.mapper.ObjectMapper)

Example 5 with Nested

use of org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested in project OpenSearch by opensearch-project.

the class FieldSortBuilderTests method testBuildNested.

/**
 * Test that the sort builder nested object gets created in the SortField
 */
public void testBuildNested() throws IOException {
    QueryShardContext shardContextMock = createMockShardContext();
    FieldSortBuilder sortBuilder = new FieldSortBuilder("fieldName").setNestedSort(new NestedSortBuilder("path").setFilter(QueryBuilders.termQuery(MAPPED_STRING_FIELDNAME, "value")));
    SortField sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    XFieldComparatorSource comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    Nested nested = comparatorSource.nested();
    assertNotNull(nested);
    assertEquals(new TermQuery(new Term(MAPPED_STRING_FIELDNAME, "value")), nested.getInnerQuery());
    sortBuilder = new FieldSortBuilder("fieldName").setNestedPath("path");
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    nested = comparatorSource.nested();
    assertNotNull(nested);
    assertEquals(new TermQuery(new Term(TypeFieldMapper.NAME, "__path")), nested.getInnerQuery());
    sortBuilder = new FieldSortBuilder("fieldName").setNestedPath("path").setNestedFilter(QueryBuilders.termQuery(MAPPED_STRING_FIELDNAME, "value"));
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    nested = comparatorSource.nested();
    assertNotNull(nested);
    assertEquals(new TermQuery(new Term(MAPPED_STRING_FIELDNAME, "value")), nested.getInnerQuery());
    // if nested path is missing, we omit any filter and return a SortedNumericSortField
    sortBuilder = new FieldSortBuilder("fieldName").setNestedFilter(QueryBuilders.termQuery(MAPPED_STRING_FIELDNAME, "value"));
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortedNumericSortField.class));
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) TermQuery(org.apache.lucene.search.TermQuery) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) QueryShardContext(org.opensearch.index.query.QueryShardContext) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) XFieldComparatorSource(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource) Term(org.apache.lucene.index.Term)

Aggregations

Nested (org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)10 SortField (org.apache.lucene.search.SortField)6 Term (org.apache.lucene.index.Term)4 TermQuery (org.apache.lucene.search.TermQuery)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 XFieldComparatorSource (org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource)3 QueryShardContext (org.opensearch.index.query.QueryShardContext)3 QueryShardException (org.opensearch.index.query.QueryShardException)3 MultiValueMode (org.opensearch.search.MultiValueMode)3 Query (org.apache.lucene.search.Query)2 GeoPoint (org.opensearch.common.geo.GeoPoint)2 IndexGeoPointFieldData (org.opensearch.index.fielddata.IndexGeoPointFieldData)2 IndexNumericFieldData (org.opensearch.index.fielddata.IndexNumericFieldData)2 NumericType (org.opensearch.index.fielddata.IndexNumericFieldData.NumericType)2 BytesRefFieldComparatorSource (org.opensearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource)2 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 DocValueFormat (org.opensearch.search.DocValueFormat)2 ArrayList (java.util.ArrayList)1 Document (org.apache.lucene.document.Document)1 StringField (org.apache.lucene.document.StringField)1