|
| 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.query; |
| 10 | + |
| 11 | +import org.apache.lucene.analysis.Analyzer; |
| 12 | +import org.apache.lucene.index.IndexableField; |
| 13 | +import org.apache.lucene.index.LeafReaderContext; |
| 14 | +import org.apache.lucene.index.memory.MemoryIndex; |
| 15 | +import org.apache.lucene.search.ConstantScoreScorer; |
| 16 | +import org.apache.lucene.search.ConstantScoreWeight; |
| 17 | +import org.apache.lucene.search.DocIdSetIterator; |
| 18 | +import org.apache.lucene.search.IndexSearcher; |
| 19 | +import org.apache.lucene.search.Query; |
| 20 | +import org.apache.lucene.search.QueryVisitor; |
| 21 | +import org.apache.lucene.search.ScoreMode; |
| 22 | +import org.apache.lucene.search.Scorer; |
| 23 | +import org.apache.lucene.search.TwoPhaseIterator; |
| 24 | +import org.apache.lucene.search.Weight; |
| 25 | +import org.opensearch.index.mapper.DerivedFieldValueFetcher; |
| 26 | +import org.opensearch.search.lookup.LeafSearchLookup; |
| 27 | +import org.opensearch.search.lookup.SearchLookup; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Objects; |
| 32 | +import java.util.function.Function; |
| 33 | + |
| 34 | +/** |
| 35 | + * DerivedFieldQuery used for querying derived fields. It contains the logic to execute an input lucene query against |
| 36 | + * DerivedField. It also accepts DerivedFieldValueFetcher and SearchLookup as an input. |
| 37 | + */ |
| 38 | +public final class DerivedFieldQuery extends Query { |
| 39 | + private final Query query; |
| 40 | + private final DerivedFieldValueFetcher valueFetcher; |
| 41 | + private final SearchLookup searchLookup; |
| 42 | + private final Function<Object, IndexableField> indexableFieldGenerator; |
| 43 | + private final Analyzer indexAnalyzer; |
| 44 | + |
| 45 | + /** |
| 46 | + * @param query lucene query to be executed against the derived field |
| 47 | + * @param valueFetcher DerivedFieldValueFetcher ValueFetcher to fetch the value of a derived field from _source |
| 48 | + * using LeafSearchLookup |
| 49 | + * @param searchLookup SearchLookup to get the LeafSearchLookup look used by valueFetcher to fetch the _source |
| 50 | + * @param indexableFieldGenerator used to generate lucene IndexableField from a given object fetched by valueFetcher |
| 51 | + * to be used in lucene memory index. |
| 52 | + */ |
| 53 | + public DerivedFieldQuery( |
| 54 | + Query query, |
| 55 | + DerivedFieldValueFetcher valueFetcher, |
| 56 | + SearchLookup searchLookup, |
| 57 | + Function<Object, IndexableField> indexableFieldGenerator, |
| 58 | + Analyzer indexAnalyzer |
| 59 | + ) { |
| 60 | + this.query = query; |
| 61 | + this.valueFetcher = valueFetcher; |
| 62 | + this.searchLookup = searchLookup; |
| 63 | + this.indexableFieldGenerator = indexableFieldGenerator; |
| 64 | + this.indexAnalyzer = indexAnalyzer; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void visit(QueryVisitor visitor) { |
| 69 | + query.visit(visitor); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Query rewrite(IndexSearcher indexSearcher) throws IOException { |
| 74 | + Query rewritten = indexSearcher.rewrite(query); |
| 75 | + if (rewritten == query) { |
| 76 | + return this; |
| 77 | + } |
| 78 | + return new DerivedFieldQuery(rewritten, valueFetcher, searchLookup, indexableFieldGenerator, indexAnalyzer); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { |
| 83 | + |
| 84 | + return new ConstantScoreWeight(this, boost) { |
| 85 | + @Override |
| 86 | + public Scorer scorer(LeafReaderContext context) { |
| 87 | + DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc()); |
| 88 | + valueFetcher.setNextReader(context); |
| 89 | + LeafSearchLookup leafSearchLookup = searchLookup.getLeafSearchLookup(context); |
| 90 | + TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) { |
| 91 | + @Override |
| 92 | + public boolean matches() { |
| 93 | + leafSearchLookup.source().setSegmentAndDocument(context, approximation.docID()); |
| 94 | + List<Object> values = valueFetcher.fetchValues(leafSearchLookup.source()); |
| 95 | + // TODO: in case of errors from script, should it be ignored and treated as missing field |
| 96 | + // by using a configurable setting? |
| 97 | + MemoryIndex memoryIndex = new MemoryIndex(); |
| 98 | + for (Object value : values) { |
| 99 | + memoryIndex.addField(indexableFieldGenerator.apply(value), indexAnalyzer); |
| 100 | + } |
| 101 | + float score = memoryIndex.search(query); |
| 102 | + return score > 0.0f; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public float matchCost() { |
| 107 | + // TODO: how can we compute this? |
| 108 | + return 1000f; |
| 109 | + } |
| 110 | + }; |
| 111 | + return new ConstantScoreScorer(this, score(), scoreMode, twoPhase); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean isCacheable(LeafReaderContext ctx) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public boolean equals(Object o) { |
| 123 | + if (this == o) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + if (sameClassAs(o) == false) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + DerivedFieldQuery other = (DerivedFieldQuery) o; |
| 130 | + return Objects.equals(this.query, other.query) |
| 131 | + && Objects.equals(this.valueFetcher, other.valueFetcher) |
| 132 | + && Objects.equals(this.searchLookup, other.searchLookup) |
| 133 | + && Objects.equals(this.indexableFieldGenerator, other.indexableFieldGenerator) |
| 134 | + && Objects.equals(this.indexAnalyzer, other.indexAnalyzer); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public int hashCode() { |
| 139 | + return Objects.hash(classHash(), query, valueFetcher, searchLookup, indexableFieldGenerator, indexableFieldGenerator); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public String toString(String f) { |
| 144 | + return "DerivedFieldQuery (Query: [ " + query.toString(f) + "])"; |
| 145 | + } |
| 146 | +} |
0 commit comments