Skip to content

Commit f42a1bb

Browse files
committed
Refactors FunctionScoreQueryBuilder and Parser
Relates to #10217 This PR is against the query-refactoring branch. Closes #12608
1 parent 397a430 commit f42a1bb

File tree

44 files changed

+1949
-703
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1949
-703
lines changed

core/src/main/java/org/elasticsearch/common/geo/GeoPoint.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
package org.elasticsearch.common.geo;
2121

2222

23+
import org.elasticsearch.common.io.stream.StreamInput;
24+
import org.elasticsearch.common.io.stream.StreamOutput;
25+
import org.elasticsearch.common.io.stream.Streamable;
26+
27+
import java.io.IOException;
28+
2329
/**
2430
*
2531
*/
26-
public final class GeoPoint {
32+
public final class GeoPoint implements Streamable {
2733

2834
private double lat;
2935
private double lon;
@@ -136,4 +142,16 @@ public static GeoPoint parseFromLatLon(String latLon) {
136142
point.resetFromString(latLon);
137143
return point;
138144
}
145+
146+
@Override
147+
public void readFrom(StreamInput in) throws IOException {
148+
lat = in.readDouble();
149+
lon = in.readDouble();
150+
}
151+
152+
@Override
153+
public void writeTo(StreamOutput out) throws IOException {
154+
out.writeDouble(lat);
155+
out.writeDouble(lon);
156+
}
139157
}

core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
import org.elasticsearch.common.Strings;
3232
import org.elasticsearch.common.bytes.BytesArray;
3333
import org.elasticsearch.common.bytes.BytesReference;
34+
import org.elasticsearch.common.geo.GeoPoint;
3435
import org.elasticsearch.common.text.StringAndBytesText;
3536
import org.elasticsearch.common.text.Text;
3637
import org.elasticsearch.index.query.QueryBuilder;
38+
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
3739
import org.joda.time.DateTime;
3840
import org.joda.time.DateTimeZone;
3941

@@ -253,6 +255,22 @@ public Integer readOptionalVInt() throws IOException {
253255
return null;
254256
}
255257

258+
@Nullable
259+
public Float readOptionalFloat() throws IOException {
260+
if (readBoolean()) {
261+
return readFloat();
262+
}
263+
return null;
264+
}
265+
266+
@Nullable
267+
public Double readOptionalDouble() throws IOException {
268+
if (readBoolean()) {
269+
return readDouble();
270+
}
271+
return null;
272+
}
273+
256274
private final CharsRefBuilder spare = new CharsRefBuilder();
257275

258276
public String readString() throws IOException {
@@ -431,6 +449,8 @@ public Object readGenericValue() throws IOException {
431449
return readDoubleArray();
432450
case 21:
433451
return readBytesRef();
452+
case 22:
453+
return readOptionalStreamable(new GeoPoint());
434454
default:
435455
throw new IOException("Can't read unknown type [" + type + "]");
436456
}
@@ -577,6 +597,18 @@ public QueryBuilder readQuery() throws IOException {
577597
return readNamedWriteable(QueryBuilder.class);
578598
}
579599

600+
public QueryBuilder readOptionalQuery() throws IOException {
601+
if (readBoolean()) {
602+
return readQuery();
603+
} else {
604+
return null;
605+
}
606+
}
607+
608+
public ScoreFunctionBuilder readScoreFunction() throws IOException {
609+
return readNamedWriteable(ScoreFunctionBuilder.class);
610+
}
611+
580612
public static StreamInput wrap(BytesReference reference) {
581613
if (reference.hasArray() == false) {
582614
reference = reference.toBytesArray();

core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
import org.elasticsearch.Version;
3131
import org.elasticsearch.common.Nullable;
3232
import org.elasticsearch.common.bytes.BytesReference;
33+
import org.elasticsearch.common.geo.GeoPoint;
3334
import org.elasticsearch.common.regex.Regex;
3435
import org.elasticsearch.common.text.Text;
3536
import org.elasticsearch.index.query.QueryBuilder;
37+
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
3638
import org.joda.time.ReadableInstant;
3739

3840
import java.io.EOFException;
@@ -204,6 +206,24 @@ public void writeOptionalVInt(@Nullable Integer integer) throws IOException {
204206
}
205207
}
206208

209+
public void writeOptionalFloat(@Nullable Float aFloat) throws IOException {
210+
if (aFloat == null) {
211+
writeBoolean(false);
212+
} else {
213+
writeBoolean(true);
214+
writeFloat(aFloat);
215+
}
216+
}
217+
218+
public void writeOptionalDouble(@Nullable Double aDouble) throws IOException {
219+
if (aDouble == null) {
220+
writeBoolean(false);
221+
} else {
222+
writeBoolean(true);
223+
writeDouble(aDouble);
224+
}
225+
}
226+
207227
public void writeOptionalText(@Nullable Text text) throws IOException {
208228
if (text == null) {
209229
writeInt(-1);
@@ -412,6 +432,9 @@ public void writeGenericValue(@Nullable Object value) throws IOException {
412432
} else if (value instanceof BytesRef) {
413433
writeByte((byte) 21);
414434
writeBytesRef((BytesRef) value);
435+
} else if (value instanceof GeoPoint) {
436+
writeByte((byte) 22);
437+
writeOptionalStreamable((GeoPoint) value);
415438
} else {
416439
throw new IOException("Can't write type [" + type + "]");
417440
}
@@ -629,4 +652,17 @@ void writeNamedWriteable(NamedWriteable namedWriteable) throws IOException {
629652
public void writeQuery(QueryBuilder queryBuilder) throws IOException {
630653
writeNamedWriteable(queryBuilder);
631654
}
655+
656+
public void writeOptionalQuery(QueryBuilder queryBuilder) throws IOException {
657+
if (queryBuilder != null) {
658+
writeBoolean(true);
659+
writeQuery(queryBuilder);
660+
} else {
661+
writeBoolean(false);
662+
}
663+
}
664+
665+
public void writeScoreFunction(ScoreFunctionBuilder scoreFunctionBuilder) throws IOException {
666+
writeNamedWriteable(scoreFunctionBuilder);
667+
}
632668
}

core/src/main/java/org/elasticsearch/common/lucene/search/function/CombineFunction.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020
package org.elasticsearch.common.lucene.search.function;
2121

2222
import org.apache.lucene.search.Explanation;
23+
import org.elasticsearch.ElasticsearchException;
24+
import org.elasticsearch.common.io.stream.StreamInput;
25+
import org.elasticsearch.common.io.stream.StreamOutput;
26+
import org.elasticsearch.common.io.stream.Writeable;
2327

24-
public enum CombineFunction {
28+
import java.io.IOException;
29+
30+
public enum CombineFunction implements Writeable<CombineFunction> {
2531
MULT {
2632
@Override
2733
public float combine(double queryBoost, double queryScore, double funcScore, double maxBoost) {
@@ -167,6 +173,8 @@ public Explanation explain(float queryBoost, Explanation queryExpl, Explanation
167173

168174
};
169175

176+
private static final CombineFunction PROTOTYPE = MULT;
177+
170178
public abstract float combine(double queryBoost, double queryScore, double funcScore, double maxBoost);
171179

172180
public abstract String getName();
@@ -182,4 +190,24 @@ private static double deviation(double input) { // only with assert!
182190
}
183191

184192
public abstract Explanation explain(float queryBoost, Explanation queryExpl, Explanation funcExpl, float maxBoost);
193+
194+
@Override
195+
public CombineFunction readFrom(StreamInput in) throws IOException {
196+
int ord = in.readVInt();
197+
for (CombineFunction combineFunction : CombineFunction.values()) {
198+
if (combineFunction.ordinal() == ord) {
199+
return combineFunction;
200+
}
201+
}
202+
throw new ElasticsearchException("unknown serialized combine function [" + ord + "]");
203+
}
204+
205+
public static CombineFunction readCombineFunctionFrom(StreamInput in) throws IOException {
206+
return PROTOTYPE.readFrom(in);
207+
}
208+
209+
@Override
210+
public void writeTo(StreamOutput out) throws IOException {
211+
out.writeVInt(this.ordinal());
212+
}
185213
}

core/src/main/java/org/elasticsearch/common/lucene/search/function/FieldValueFactorFunction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,13 @@ public double apply(double n) {
168168
public String toString() {
169169
return super.toString().toLowerCase(Locale.ROOT);
170170
}
171+
172+
public static Modifier fromString(String modifier) {
173+
try {
174+
return valueOf(modifier.toUpperCase(Locale.ROOT));
175+
} catch (Throwable t) {
176+
throw new IllegalArgumentException("Illegal modifier: " + modifier);
177+
}
178+
}
171179
}
172180
}

core/src/main/java/org/elasticsearch/common/lucene/search/function/FiltersFunctionScoreQuery.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,17 @@
2222
import org.apache.lucene.index.IndexReader;
2323
import org.apache.lucene.index.LeafReaderContext;
2424
import org.apache.lucene.index.Term;
25-
import org.apache.lucene.search.Explanation;
26-
import org.apache.lucene.search.IndexSearcher;
27-
import org.apache.lucene.search.Query;
28-
import org.apache.lucene.search.Scorer;
29-
import org.apache.lucene.search.Weight;
25+
import org.apache.lucene.search.*;
3026
import org.apache.lucene.util.Bits;
3127
import org.apache.lucene.util.ToStringUtils;
28+
import org.elasticsearch.ElasticsearchException;
29+
import org.elasticsearch.common.io.stream.StreamInput;
30+
import org.elasticsearch.common.io.stream.StreamOutput;
31+
import org.elasticsearch.common.io.stream.Writeable;
3232
import org.elasticsearch.common.lucene.Lucene;
3333

3434
import java.io.IOException;
35-
import java.util.ArrayList;
36-
import java.util.Arrays;
37-
import java.util.List;
38-
import java.util.Locale;
39-
import java.util.Set;
35+
import java.util.*;
4036

4137
/**
4238
* A query that allows for a pluggable boost function / filter. If it matches
@@ -78,8 +74,34 @@ public int hashCode() {
7874
}
7975
}
8076

81-
public static enum ScoreMode {
82-
First, Avg, Max, Sum, Min, Multiply
77+
public enum ScoreMode implements Writeable<ScoreMode> {
78+
First, Avg, Max, Sum, Min, Multiply;
79+
80+
private static final ScoreMode PROTOTYPE = Multiply;
81+
82+
public String getName() {
83+
return name().toLowerCase();
84+
}
85+
86+
@Override
87+
public ScoreMode readFrom(StreamInput in) throws IOException {
88+
int ord = in.readVInt();
89+
for (ScoreMode scoreMode : ScoreMode.values()) {
90+
if (scoreMode.ordinal() == ord) {
91+
return scoreMode;
92+
}
93+
}
94+
throw new ElasticsearchException("unknown serialized score mode [" + ord + "]");
95+
}
96+
97+
public static ScoreMode readScoreModeFrom(StreamInput in) throws IOException {
98+
return PROTOTYPE.readFrom(in);
99+
}
100+
101+
@Override
102+
public void writeTo(StreamOutput out) throws IOException {
103+
out.writeVInt(this.ordinal());
104+
}
83105
}
84106

85107
Query subQuery;

core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.lucene.queryparser.xml.builders.MatchAllDocsQueryBuilder;
2223
import org.elasticsearch.action.search.SearchRequestBuilder;
2324
import org.elasticsearch.common.Nullable;
2425
import org.elasticsearch.common.bytes.BytesReference;

core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import com.google.common.collect.ImmutableMap;
2323
import com.google.common.collect.Maps;
24-
2524
import org.apache.lucene.analysis.Analyzer;
2625
import org.apache.lucene.queryparser.classic.MapperQueryParser;
2726
import org.apache.lucene.queryparser.classic.QueryParserSettings;
@@ -38,11 +37,8 @@
3837
import org.elasticsearch.index.Index;
3938
import org.elasticsearch.index.analysis.AnalysisService;
4039
import org.elasticsearch.index.fielddata.IndexFieldData;
41-
import org.elasticsearch.index.mapper.ContentPath;
42-
import org.elasticsearch.index.mapper.MappedFieldType;
43-
import org.elasticsearch.index.mapper.Mapper;
44-
import org.elasticsearch.index.mapper.MapperBuilders;
45-
import org.elasticsearch.index.mapper.MapperService;
40+
import org.elasticsearch.index.fielddata.IndexFieldDataService;
41+
import org.elasticsearch.index.mapper.*;
4642
import org.elasticsearch.index.mapper.core.StringFieldMapper;
4743
import org.elasticsearch.index.mapper.object.ObjectMapper;
4844
import org.elasticsearch.index.query.support.NestedScope;
@@ -326,4 +322,8 @@ public Version indexVersionCreated() {
326322
public QueryParseContext parseContext() {
327323
return this.parseContext;
328324
}
325+
326+
public IndexFieldDataService fieldData() {
327+
return indexQueryParser.fieldDataService;
328+
}
329329
}

core/src/main/java/org/elasticsearch/index/query/functionscore/DecayFunction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232

3333
public interface DecayFunction {
3434

35-
public double evaluate(double value, double scale);
35+
double evaluate(double value, double scale);
3636

37-
public Explanation explainFunction(String valueString, double value, double scale);
37+
Explanation explainFunction(String valueString, double value, double scale);
3838

3939
/**
4040
* The final scale parameter is computed from the scale parameter given by
@@ -49,6 +49,6 @@ public interface DecayFunction {
4949
* the value which decay function should take once the distance
5050
* reaches this scale
5151
* */
52-
public double processScale(double scale, double decay);
52+
double processScale(double scale, double decay);
5353

5454
}

0 commit comments

Comments
 (0)