Search in sources :

Example 6 with MultiValueMode

use of org.opensearch.search.MultiValueMode 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 7 with MultiValueMode

use of org.opensearch.search.MultiValueMode 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 8 with MultiValueMode

use of org.opensearch.search.MultiValueMode in project OpenSearch by opensearch-project.

the class GeoDistanceSortBuilder method build.

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
    GeoPoint[] localPoints = localPoints();
    boolean reverse = order == SortOrder.DESC;
    MultiValueMode localSortMode = localSortMode();
    IndexGeoPointFieldData geoIndexFieldData = fieldData(context);
    Nested nested = nested(context);
    if (// only works with 5.x geo_point
    geoIndexFieldData.getClass() == LatLonPointIndexFieldData.class && nested == null && // LatLonDocValuesField internally picks the closest point
    localSortMode == MultiValueMode.MIN && unit == DistanceUnit.METERS && reverse == false && localPoints.length == 1) {
        return new SortFieldAndFormat(LatLonDocValuesField.newDistanceSort(fieldName, localPoints[0].lat(), localPoints[0].lon()), DocValueFormat.RAW);
    }
    return new SortFieldAndFormat(new SortField(fieldName, comparatorSource(localPoints, localSortMode, geoIndexFieldData, nested), reverse), DocValueFormat.RAW);
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) IndexGeoPointFieldData(org.opensearch.index.fielddata.IndexGeoPointFieldData) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) SortField(org.apache.lucene.search.SortField) MultiValueMode(org.opensearch.search.MultiValueMode) LatLonPointIndexFieldData(org.opensearch.index.fielddata.plain.AbstractLatLonPointIndexFieldData.LatLonPointIndexFieldData)

Example 9 with MultiValueMode

use of org.opensearch.search.MultiValueMode in project OpenSearch by opensearch-project.

the class DecayFunctionParser method fromXContent.

/**
 * Parses bodies of the kind
 *
 * <pre>
 * <code>
 * {
 *      "fieldname1" : {
 *          "origin" : "someValue",
 *          "scale" : "someValue"
 *      },
 *      "multi_value_mode" : "min"
 * }
 * </code>
 * </pre>
 */
@Override
public DFB fromXContent(XContentParser parser) throws IOException, ParsingException {
    String currentFieldName;
    XContentParser.Token token;
    MultiValueMode multiValueMode = DecayFunctionBuilder.DEFAULT_MULTI_VALUE_MODE;
    String fieldName = null;
    BytesReference functionBytes = null;
    while ((token = parser.nextToken()) == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
        token = parser.nextToken();
        if (token == XContentParser.Token.START_OBJECT) {
            fieldName = currentFieldName;
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.copyCurrentStructure(parser);
            functionBytes = BytesReference.bytes(builder);
        } else if (MULTI_VALUE_MODE.match(currentFieldName, parser.getDeprecationHandler())) {
            multiValueMode = MultiValueMode.fromString(parser.text());
        } else {
            throw new ParsingException(parser.getTokenLocation(), "malformed score function score parameters.");
        }
    }
    if (fieldName == null || functionBytes == null) {
        throw new ParsingException(parser.getTokenLocation(), "malformed score function score parameters.");
    }
    DFB functionBuilder = createFromBytes.apply(fieldName, functionBytes);
    functionBuilder.setMultiValueMode(multiValueMode);
    return functionBuilder;
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser) MultiValueMode(org.opensearch.search.MultiValueMode) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 10 with MultiValueMode

use of org.opensearch.search.MultiValueMode in project OpenSearch by opensearch-project.

the class AbstractNumberNestedSortingTestCase method assertAvgScoreMode.

protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException {
    MultiValueMode sortMode = MultiValueMode.AVG;
    Query childFilter = Queries.not(parentFilter);
    XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
    Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits.value, equalTo(7L));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(4));
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) XFieldComparatorSource(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource) MultiValueMode(org.opensearch.search.MultiValueMode)

Aggregations

MultiValueMode (org.opensearch.search.MultiValueMode)11 SortField (org.apache.lucene.search.SortField)7 Sort (org.apache.lucene.search.Sort)6 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)5 Query (org.apache.lucene.search.Query)5 QueryBitSetProducer (org.apache.lucene.search.join.QueryBitSetProducer)5 ToParentBlockJoinQuery (org.apache.lucene.search.join.ToParentBlockJoinQuery)5 TopDocs (org.apache.lucene.search.TopDocs)4 XFieldComparatorSource (org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource)4 ArrayList (java.util.ArrayList)3 Document (org.apache.lucene.document.Document)3 StringField (org.apache.lucene.document.StringField)3 DirectoryReader (org.apache.lucene.index.DirectoryReader)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 TermQuery (org.apache.lucene.search.TermQuery)3 OpenSearchDirectoryReader (org.opensearch.common.lucene.index.OpenSearchDirectoryReader)3 Nested (org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)3 ShardId (org.opensearch.index.shard.ShardId)3 Term (org.apache.lucene.index.Term)2 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)2