|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.fielddata.plain; |
| 10 | + |
| 11 | +import org.apache.lucene.index.IndexSorter; |
| 12 | +import org.apache.lucene.index.SortedSetDocValues; |
| 13 | +import org.apache.lucene.search.FieldComparator; |
| 14 | +import org.apache.lucene.search.FieldComparatorSource; |
| 15 | +import org.apache.lucene.search.IndexSearcher; |
| 16 | +import org.apache.lucene.search.Pruning; |
| 17 | +import org.apache.lucene.search.SortField; |
| 18 | +import org.apache.lucene.search.SortedSetSelector; |
| 19 | +import org.apache.lucene.search.SortedSetSortField; |
| 20 | +import org.apache.lucene.store.DataInput; |
| 21 | +import org.apache.lucene.util.BytesRef; |
| 22 | +import org.opensearch.common.Nullable; |
| 23 | +import org.opensearch.core.indices.breaker.CircuitBreakerService; |
| 24 | +import org.opensearch.index.fielddata.IndexFieldData; |
| 25 | +import org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested; |
| 26 | +import org.opensearch.index.fielddata.IndexFieldDataCache; |
| 27 | +import org.opensearch.index.fielddata.ScriptDocValues; |
| 28 | +import org.opensearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource; |
| 29 | +import org.opensearch.index.mapper.WildcardFieldMapper; |
| 30 | +import org.opensearch.search.MultiValueMode; |
| 31 | +import org.opensearch.search.aggregations.support.ValuesSourceType; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Comparator; |
| 35 | +import java.util.function.Function; |
| 36 | + |
| 37 | +/** |
| 38 | + * Wrapper for {@link SortedSetOrdinalsIndexFieldData} which disables pruning optimization for |
| 39 | + * sorting. Used in {@link WildcardFieldMapper}. |
| 40 | + * |
| 41 | + * @opensearch.internal |
| 42 | + */ |
| 43 | +public class NonPruningSortedSetOrdinalsIndexFieldData extends SortedSetOrdinalsIndexFieldData { |
| 44 | + |
| 45 | + /** |
| 46 | + * Builder for non-pruning sorted set ordinals |
| 47 | + * |
| 48 | + * @opensearch.internal |
| 49 | + */ |
| 50 | + public static class Builder implements IndexFieldData.Builder { |
| 51 | + private final String name; |
| 52 | + private final Function<SortedSetDocValues, ScriptDocValues<?>> scriptFunction; |
| 53 | + private final ValuesSourceType valuesSourceType; |
| 54 | + |
| 55 | + public Builder(String name, ValuesSourceType valuesSourceType) { |
| 56 | + this(name, AbstractLeafOrdinalsFieldData.DEFAULT_SCRIPT_FUNCTION, valuesSourceType); |
| 57 | + } |
| 58 | + |
| 59 | + public Builder(String name, Function<SortedSetDocValues, ScriptDocValues<?>> scriptFunction, ValuesSourceType valuesSourceType) { |
| 60 | + this.name = name; |
| 61 | + this.scriptFunction = scriptFunction; |
| 62 | + this.valuesSourceType = valuesSourceType; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public NonPruningSortedSetOrdinalsIndexFieldData build(IndexFieldDataCache cache, CircuitBreakerService breakerService) { |
| 67 | + return new NonPruningSortedSetOrdinalsIndexFieldData(cache, name, valuesSourceType, breakerService, scriptFunction); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public NonPruningSortedSetOrdinalsIndexFieldData( |
| 72 | + IndexFieldDataCache cache, |
| 73 | + String fieldName, |
| 74 | + ValuesSourceType valuesSourceType, |
| 75 | + CircuitBreakerService breakerService, |
| 76 | + Function<SortedSetDocValues, ScriptDocValues<?>> scriptFunction |
| 77 | + ) { |
| 78 | + super(cache, fieldName, valuesSourceType, breakerService, scriptFunction); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public SortField sortField(@Nullable Object missingValue, MultiValueMode sortMode, Nested nested, boolean reverse) { |
| 83 | + XFieldComparatorSource source = new BytesRefFieldComparatorSource(this, missingValue, sortMode, nested); |
| 84 | + source.disableSkipping(); |
| 85 | + /* |
| 86 | + Check if we can use a simple {@link SortedSetSortField} compatible with index sorting and |
| 87 | + returns a custom sort field otherwise. |
| 88 | + */ |
| 89 | + if (nested != null |
| 90 | + || (sortMode != MultiValueMode.MAX && sortMode != MultiValueMode.MIN) |
| 91 | + || (source.sortMissingLast(missingValue) == false && source.sortMissingFirst(missingValue) == false)) { |
| 92 | + return new NonPruningSortField(new SortField(getFieldName(), source, reverse)); |
| 93 | + } |
| 94 | + SortField sortField = new NonPruningSortField( |
| 95 | + new SortedSetSortField( |
| 96 | + getFieldName(), |
| 97 | + reverse, |
| 98 | + sortMode == MultiValueMode.MAX ? SortedSetSelector.Type.MAX : SortedSetSelector.Type.MIN |
| 99 | + ) |
| 100 | + ); |
| 101 | + sortField.setMissingValue( |
| 102 | + source.sortMissingLast(missingValue) ^ reverse ? SortedSetSortField.STRING_LAST : SortedSetSortField.STRING_FIRST |
| 103 | + ); |
| 104 | + return sortField; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * {@link SortField} implementation which delegates calls to another {@link SortField}. |
| 109 | + * |
| 110 | + */ |
| 111 | + public abstract class FilteredSortField extends SortField { |
| 112 | + protected final SortField delegate; |
| 113 | + |
| 114 | + protected FilteredSortField(SortField sortField) { |
| 115 | + super(sortField.getField(), sortField.getType()); |
| 116 | + this.delegate = sortField; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Object getMissingValue() { |
| 121 | + return delegate.getMissingValue(); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void setMissingValue(Object missingValue) { |
| 126 | + delegate.setMissingValue(missingValue); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public String getField() { |
| 131 | + return delegate.getField(); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Type getType() { |
| 136 | + return delegate.getType(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public boolean getReverse() { |
| 141 | + return delegate.getReverse(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public FieldComparatorSource getComparatorSource() { |
| 146 | + return delegate.getComparatorSource(); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public String toString() { |
| 151 | + return delegate.toString(); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public boolean equals(Object o) { |
| 156 | + return delegate.equals(o); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public int hashCode() { |
| 161 | + return delegate.hashCode(); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void setBytesComparator(Comparator<BytesRef> b) { |
| 166 | + delegate.setBytesComparator(b); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public Comparator<BytesRef> getBytesComparator() { |
| 171 | + return delegate.getBytesComparator(); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public FieldComparator<?> getComparator(int numHits, Pruning pruning) { |
| 176 | + return delegate.getComparator(numHits, pruning); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public SortField rewrite(IndexSearcher searcher) throws IOException { |
| 181 | + return delegate.rewrite(searcher); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean needsScores() { |
| 186 | + return delegate.needsScores(); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public IndexSorter getIndexSorter() { |
| 191 | + return delegate.getIndexSorter(); |
| 192 | + } |
| 193 | + |
| 194 | + @Deprecated |
| 195 | + @Override |
| 196 | + public void setOptimizeSortWithIndexedData(boolean optimizeSortWithIndexedData) { |
| 197 | + delegate.setOptimizeSortWithIndexedData(optimizeSortWithIndexedData); |
| 198 | + } |
| 199 | + |
| 200 | + @Deprecated |
| 201 | + @Override |
| 202 | + public boolean getOptimizeSortWithIndexedData() { |
| 203 | + return delegate.getOptimizeSortWithIndexedData(); |
| 204 | + } |
| 205 | + |
| 206 | + @Deprecated |
| 207 | + @Override |
| 208 | + public void setOptimizeSortWithPoints(boolean optimizeSortWithPoints) { |
| 209 | + delegate.setOptimizeSortWithPoints(optimizeSortWithPoints); |
| 210 | + } |
| 211 | + |
| 212 | + @Deprecated |
| 213 | + @Override |
| 214 | + public boolean getOptimizeSortWithPoints() { |
| 215 | + return delegate.getOptimizeSortWithPoints(); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * {@link SortField} extension which disables pruning in the comparator. |
| 221 | + * |
| 222 | + * @opensearch.internal |
| 223 | + */ |
| 224 | + public final class NonPruningSortField extends FilteredSortField { |
| 225 | + |
| 226 | + private NonPruningSortField(SortField sortField) { |
| 227 | + super(sortField); |
| 228 | + } |
| 229 | + |
| 230 | + public static Type readType(DataInput in) throws IOException { |
| 231 | + return SortField.readType(in); |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public FieldComparator<?> getComparator(int numHits, Pruning pruning) { |
| 236 | + // explictly disable pruning |
| 237 | + return delegate.getComparator(numHits, Pruning.NONE); |
| 238 | + } |
| 239 | + |
| 240 | + public SortField getDelegate() { |
| 241 | + return delegate; |
| 242 | + } |
| 243 | + } |
| 244 | +} |
0 commit comments