Skip to content

Commit 5180b1a

Browse files
committed
Add FilterFieldType
This class allows developers (in core or plugins) to wrap an existing field type, delegating all behavior by default, overriding specific methods as needed. Signed-off-by: Michael Froh <[email protected]>
1 parent 05b1cf5 commit 5180b1a

File tree

2 files changed

+286
-0
lines changed

2 files changed

+286
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
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.mapper;
10+
11+
import org.apache.lucene.analysis.TokenStream;
12+
import org.apache.lucene.index.IndexReader;
13+
import org.apache.lucene.queries.intervals.IntervalsSource;
14+
import org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper;
15+
import org.apache.lucene.queries.spans.SpanQuery;
16+
import org.apache.lucene.search.MultiTermQuery;
17+
import org.apache.lucene.search.Query;
18+
import org.opensearch.common.geo.ShapeRelation;
19+
import org.opensearch.common.time.DateMathParser;
20+
import org.opensearch.common.unit.Fuzziness;
21+
import org.opensearch.index.analysis.NamedAnalyzer;
22+
import org.opensearch.index.query.IntervalMode;
23+
import org.opensearch.index.query.QueryRewriteContext;
24+
import org.opensearch.index.query.QueryShardContext;
25+
import org.opensearch.search.DocValueFormat;
26+
import org.opensearch.search.lookup.SearchLookup;
27+
28+
import java.io.IOException;
29+
import java.time.ZoneId;
30+
import java.util.List;
31+
import java.util.Map;
32+
import java.util.function.Function;
33+
34+
public abstract class FilterFieldType<T extends MappedFieldType> extends MappedFieldType {
35+
protected final T delegate;
36+
37+
public FilterFieldType(T delegate) {
38+
super(
39+
delegate.name(),
40+
delegate.isSearchable(),
41+
delegate.isStored(),
42+
delegate.hasDocValues(),
43+
delegate.getTextSearchInfo(),
44+
delegate.meta()
45+
);
46+
this.delegate = delegate;
47+
}
48+
49+
@Override
50+
public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) {
51+
return delegate.valueFetcher(context, searchLookup, format);
52+
}
53+
54+
@Override
55+
public Query termQuery(Object value, QueryShardContext context) {
56+
return delegate.termQuery(value, context);
57+
}
58+
59+
@Override
60+
public String familyTypeName() {
61+
return delegate.familyTypeName();
62+
}
63+
64+
@Override
65+
public String name() {
66+
return delegate.name();
67+
}
68+
69+
@Override
70+
public float boost() {
71+
return delegate.boost();
72+
}
73+
74+
@Override
75+
public void setBoost(float boost) {
76+
delegate.setBoost(boost);
77+
}
78+
79+
@Override
80+
public boolean hasDocValues() {
81+
return delegate.hasDocValues();
82+
}
83+
84+
@Override
85+
public NamedAnalyzer indexAnalyzer() {
86+
return delegate.indexAnalyzer();
87+
}
88+
89+
@Override
90+
public void setIndexAnalyzer(NamedAnalyzer analyzer) {
91+
delegate.setIndexAnalyzer(analyzer);
92+
}
93+
94+
@Override
95+
public Object valueForDisplay(Object value) {
96+
return delegate.valueForDisplay(value);
97+
}
98+
99+
@Override
100+
public boolean isSearchable() {
101+
return delegate.isSearchable();
102+
}
103+
104+
@Override
105+
public boolean isStored() {
106+
return delegate.isStored();
107+
}
108+
109+
@Override
110+
public Function<byte[], Number> pointReaderIfPossible() {
111+
return delegate.pointReaderIfPossible();
112+
}
113+
114+
@Override
115+
public boolean isAggregatable() {
116+
return delegate.isAggregatable();
117+
}
118+
119+
@Override
120+
public Query termQueryCaseInsensitive(Object value, QueryShardContext context) {
121+
return delegate.termQueryCaseInsensitive(value, context);
122+
}
123+
124+
@Override
125+
public Query termsQuery(List<?> values, QueryShardContext context) {
126+
return delegate.termsQuery(values, context);
127+
}
128+
129+
@Override
130+
public Query rangeQuery(
131+
Object lowerTerm,
132+
Object upperTerm,
133+
boolean includeLower,
134+
boolean includeUpper,
135+
ShapeRelation relation,
136+
ZoneId timeZone,
137+
DateMathParser parser,
138+
QueryShardContext context
139+
) {
140+
return delegate.rangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, relation, timeZone, parser, context);
141+
}
142+
143+
@Override
144+
public Query fuzzyQuery(
145+
Object value,
146+
Fuzziness fuzziness,
147+
int prefixLength,
148+
int maxExpansions,
149+
boolean transpositions,
150+
MultiTermQuery.RewriteMethod method,
151+
QueryShardContext context
152+
) {
153+
return delegate.fuzzyQuery(value, fuzziness, prefixLength, maxExpansions, transpositions, method, context);
154+
}
155+
156+
@Override
157+
public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, boolean caseInsensitve, QueryShardContext context) {
158+
return delegate.prefixQuery(value, method, caseInsensitve, context);
159+
}
160+
161+
@Override
162+
public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, boolean caseInsensitve, QueryShardContext context) {
163+
return delegate.wildcardQuery(value, method, caseInsensitve, context);
164+
}
165+
166+
@Override
167+
public Query normalizedWildcardQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) {
168+
return delegate.normalizedWildcardQuery(value, method, context);
169+
}
170+
171+
@Override
172+
public Query regexpQuery(
173+
String value,
174+
int syntaxFlags,
175+
int matchFlags,
176+
int maxDeterminizedStates,
177+
MultiTermQuery.RewriteMethod method,
178+
QueryShardContext context
179+
) {
180+
return delegate.regexpQuery(value, syntaxFlags, matchFlags, maxDeterminizedStates, method, context);
181+
}
182+
183+
@Override
184+
public Query existsQuery(QueryShardContext context) {
185+
return delegate.existsQuery(context);
186+
}
187+
188+
@Override
189+
public Query phraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements) throws IOException {
190+
return delegate.phraseQuery(stream, slop, enablePositionIncrements);
191+
}
192+
193+
@Override
194+
public Query phraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, QueryShardContext context) throws IOException {
195+
return delegate.phraseQuery(stream, slop, enablePositionIncrements, context);
196+
}
197+
198+
@Override
199+
public Query multiPhraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements) throws IOException {
200+
return delegate.multiPhraseQuery(stream, slop, enablePositionIncrements);
201+
}
202+
203+
@Override
204+
public Query multiPhraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, QueryShardContext context)
205+
throws IOException {
206+
return delegate.multiPhraseQuery(stream, slop, enablePositionIncrements, context);
207+
}
208+
209+
@Override
210+
public Query phrasePrefixQuery(TokenStream stream, int slop, int maxExpansions) throws IOException {
211+
return delegate.phrasePrefixQuery(stream, slop, maxExpansions);
212+
}
213+
214+
@Override
215+
public Query phrasePrefixQuery(TokenStream stream, int slop, int maxExpansions, QueryShardContext context) throws IOException {
216+
return delegate.phrasePrefixQuery(stream, slop, maxExpansions, context);
217+
}
218+
219+
@Override
220+
public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRewriteMethod method, QueryShardContext context) {
221+
return delegate.spanPrefixQuery(value, method, context);
222+
}
223+
224+
@Override
225+
public Query distanceFeatureQuery(Object origin, String pivot, float boost, QueryShardContext context) {
226+
return delegate.distanceFeatureQuery(origin, pivot, boost, context);
227+
}
228+
229+
@Override
230+
public IntervalsSource intervals(String query, int max_gaps, IntervalMode mode, NamedAnalyzer analyzer, boolean prefix)
231+
throws IOException {
232+
return delegate.intervals(query, max_gaps, mode, analyzer, prefix);
233+
}
234+
235+
@Override
236+
public Relation isFieldWithinQuery(
237+
IndexReader reader,
238+
Object from,
239+
Object to,
240+
boolean includeLower,
241+
boolean includeUpper,
242+
ZoneId timeZone,
243+
DateMathParser dateMathParser,
244+
QueryRewriteContext context
245+
) throws IOException {
246+
return delegate.isFieldWithinQuery(reader, from, to, includeLower, includeUpper, timeZone, dateMathParser, context);
247+
}
248+
249+
@Override
250+
public boolean eagerGlobalOrdinals() {
251+
return delegate.eagerGlobalOrdinals();
252+
}
253+
254+
@Override
255+
public void setEagerGlobalOrdinals(boolean eagerGlobalOrdinals) {
256+
delegate.setEagerGlobalOrdinals(eagerGlobalOrdinals);
257+
}
258+
259+
@Override
260+
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
261+
return delegate.docValueFormat(format, timeZone);
262+
}
263+
264+
@Override
265+
public Map<String, String> meta() {
266+
return delegate.meta();
267+
}
268+
269+
@Override
270+
public TextSearchInfo getTextSearchInfo() {
271+
return delegate.getTextSearchInfo();
272+
}
273+
274+
@Override
275+
public MappedFieldType unwrap() {
276+
return delegate.unwrap();
277+
}
278+
}

server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java

+8
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,12 @@ public Map<String, String> meta() {
521521
public TextSearchInfo getTextSearchInfo() {
522522
return textSearchInfo;
523523
}
524+
525+
/**
526+
* @return a concrete (unfiltered) field type, which should be the current instance
527+
* if this is not a field type wrapper. See {@link FilterFieldType}.
528+
*/
529+
public MappedFieldType unwrap() {
530+
return this;
531+
}
524532
}

0 commit comments

Comments
 (0)