|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.index.fielddata.fieldcomparator; |
| 11 | + |
| 12 | +import org.apache.lucene.index.LeafReaderContext; |
| 13 | +import org.apache.lucene.index.NumericDocValues; |
| 14 | +import org.apache.lucene.search.FieldComparator; |
| 15 | +import org.apache.lucene.search.LeafFieldComparator; |
| 16 | +import org.apache.lucene.search.Pruning; |
| 17 | +import org.apache.lucene.search.Scorable; |
| 18 | +import org.apache.lucene.search.comparators.DoubleComparator; |
| 19 | +import org.elasticsearch.common.util.BigArrays; |
| 20 | +import org.elasticsearch.core.CheckedFunction; |
| 21 | +import org.elasticsearch.core.Nullable; |
| 22 | +import org.elasticsearch.index.fielddata.FieldData; |
| 23 | +import org.elasticsearch.index.fielddata.IndexNumericFieldData; |
| 24 | +import org.elasticsearch.index.fielddata.NumericDoubleValues; |
| 25 | +import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; |
| 26 | +import org.elasticsearch.script.NumberSortScript; |
| 27 | +import org.elasticsearch.search.DocValueFormat; |
| 28 | +import org.elasticsearch.search.MultiValueMode; |
| 29 | +import org.elasticsearch.search.sort.BucketedSort; |
| 30 | +import org.elasticsearch.search.sort.SortOrder; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | + |
| 34 | +/** |
| 35 | + * Script comparator source for double values. |
| 36 | + */ |
| 37 | +public class ScriptDoubleValuesComparatorSource extends DoubleValuesComparatorSource { |
| 38 | + |
| 39 | + private final CheckedFunction<LeafReaderContext, NumberSortScript, IOException> scriptSupplier; |
| 40 | + |
| 41 | + public ScriptDoubleValuesComparatorSource( |
| 42 | + CheckedFunction<LeafReaderContext, NumberSortScript, IOException> scriptSupplier, |
| 43 | + IndexNumericFieldData indexFieldData, |
| 44 | + @Nullable Object missingValue, |
| 45 | + MultiValueMode sortMode, |
| 46 | + Nested nested |
| 47 | + ) { |
| 48 | + super(indexFieldData, missingValue, sortMode, nested); |
| 49 | + this.scriptSupplier = scriptSupplier; |
| 50 | + } |
| 51 | + |
| 52 | + private SortedNumericDoubleValues getValues(NumberSortScript leafScript) throws IOException { |
| 53 | + final NumericDoubleValues values = new NumericDoubleValues() { |
| 54 | + @Override |
| 55 | + public boolean advanceExact(int doc) { |
| 56 | + leafScript.setDocument(doc); |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public double doubleValue() { |
| 62 | + return leafScript.execute(); |
| 63 | + } |
| 64 | + }; |
| 65 | + return FieldData.singleton(values); |
| 66 | + } |
| 67 | + |
| 68 | + private NumericDoubleValues getNumericDocValues(LeafReaderContext context, double missingValue, NumberSortScript leafScript) |
| 69 | + throws IOException { |
| 70 | + return getNumericDocValues(context, missingValue, getValues(leafScript)); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected FieldComparator<?> newComparator(int numHits, Pruning enableSkipping, boolean reversed, double dMissingValue) { |
| 75 | + // NOTE: it's important to pass null as a missing value in the constructor so that |
| 76 | + // the comparator doesn't check docsWithField since we replace missing values in select() |
| 77 | + return new DoubleComparator(numHits, null, null, reversed, Pruning.NONE) { |
| 78 | + @Override |
| 79 | + public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException { |
| 80 | + NumberSortScript leafScript = scriptSupplier.apply(context); |
| 81 | + return new DoubleLeafComparator(context) { |
| 82 | + @Override |
| 83 | + protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException { |
| 84 | + return ScriptDoubleValuesComparatorSource.this.getNumericDocValues(context, dMissingValue, leafScript) |
| 85 | + .getRawDoubleValues(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void setScorer(Scorable scorer) { |
| 90 | + leafScript.setScorer(scorer); |
| 91 | + } |
| 92 | + }; |
| 93 | + } |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public BucketedSort newBucketedSort( |
| 99 | + BigArrays bigArrays, |
| 100 | + SortOrder sortOrder, |
| 101 | + DocValueFormat format, |
| 102 | + int bucketSize, |
| 103 | + BucketedSort.ExtraData extra |
| 104 | + ) { |
| 105 | + return new BucketedSort.ForDoubles(bigArrays, sortOrder, format, bucketSize, extra) { |
| 106 | + private final double dMissingValue = (Double) missingObject(missingValue, sortOrder == SortOrder.DESC); |
| 107 | + |
| 108 | + @Override |
| 109 | + public Leaf forLeaf(LeafReaderContext ctx) throws IOException { |
| 110 | + NumberSortScript leafScript = scriptSupplier.apply(ctx); |
| 111 | + return new Leaf(ctx) { |
| 112 | + private final NumericDoubleValues docValues = getNumericDocValues(ctx, dMissingValue, leafScript); |
| 113 | + private double docValue; |
| 114 | + |
| 115 | + @Override |
| 116 | + protected boolean advanceExact(int doc) throws IOException { |
| 117 | + if (docValues.advanceExact(doc)) { |
| 118 | + docValue = docValues.doubleValue(); |
| 119 | + return true; |
| 120 | + } |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + protected double docValue() { |
| 126 | + return docValue; |
| 127 | + } |
| 128 | + }; |
| 129 | + } |
| 130 | + }; |
| 131 | + } |
| 132 | +} |
0 commit comments