Skip to content

Commit f2bcc77

Browse files
authored
Drop boost from runtime distance feature query (#63949)
This drops the `boost` parameter of the `distance_feature` query builder internally, relying on our query building infrastructure to wrap the query in a `boosting` query. Relates to #63767
1 parent e566730 commit f2bcc77

File tree

8 files changed

+30
-51
lines changed

8 files changed

+30
-51
lines changed

server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,11 @@ public static long parseToLong(
432432
}
433433

434434
@Override
435-
public Query distanceFeatureQuery(Object origin, String pivot, float boost, QueryShardContext context) {
435+
public Query distanceFeatureQuery(Object origin, String pivot, QueryShardContext context) {
436436
long originLong = parseToLong(origin, true, null, null, context::nowInMillis);
437437
TimeValue pivotTime = TimeValue.parseTimeValue(pivot, "distance_feature.pivot");
438-
return resolution.distanceFeatureQuery(name(), boost, originLong, pivotTime);
438+
// As we already apply boost in AbstractQueryBuilder::toQuery, we always passing a boost of 1.0 to distanceFeatureQuery
439+
return resolution.distanceFeatureQuery(name(), 1.0f, originLong, pivotTime);
439440
}
440441

441442
@Override

server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S
194194
}
195195

196196
@Override
197-
public Query distanceFeatureQuery(Object origin, String pivot, float boost, QueryShardContext context) {
197+
public Query distanceFeatureQuery(Object origin, String pivot, QueryShardContext context) {
198198
GeoPoint originGeoPoint;
199199
if (origin instanceof GeoPoint) {
200200
originGeoPoint = (GeoPoint) origin;
@@ -205,7 +205,8 @@ public Query distanceFeatureQuery(Object origin, String pivot, float boost, Quer
205205
"Must be of type [geo_point] or [string] for geo_point fields!");
206206
}
207207
double pivotDouble = DistanceUnit.DEFAULT.parse(pivot, DistanceUnit.DEFAULT);
208-
return LatLonPoint.newDistanceFeatureQuery(name(), boost, originGeoPoint.lat(), originGeoPoint.lon(), pivotDouble);
208+
// As we already apply boost in AbstractQueryBuilder::toQuery, we always passing a boost of 1.0 to distanceFeatureQuery
209+
return LatLonPoint.newDistanceFeatureQuery(name(), 1.0f, originGeoPoint.lat(), originGeoPoint.lon(), pivotDouble);
209210
}
210211
}
211212

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew
294294
+ "] which is of type [" + typeName() + "]");
295295
}
296296

297-
public Query distanceFeatureQuery(Object origin, String pivot, float boost, QueryShardContext context) {
297+
public Query distanceFeatureQuery(Object origin, String pivot, QueryShardContext context) {
298298
throw new IllegalArgumentException("Illegal data type of [" + typeName() + "]!"+
299299
"[" + DistanceFeatureQueryBuilder.NAME + "] query can only be run on a date, date_nanos or geo_point field type!");
300300
}

server/src/main/java/org/elasticsearch/index/query/DistanceFeatureQueryBuilder.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
112112
if (fieldType == null) {
113113
return Queries.newMatchNoDocsQuery("Can't run [" + NAME + "] query on unmapped fields!");
114114
}
115-
// As we already apply boost in AbstractQueryBuilder::toQuery, we always passing a boost of 1.0 to distanceFeatureQuery
116-
return fieldType.distanceFeatureQuery(origin.origin(), pivot, 1.0f, context);
115+
return fieldType.distanceFeatureQuery(origin.origin(), pivot, context);
117116
}
118117

119118
String fieldName() {

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/DateScriptFieldType.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public DateScriptFieldData.Builder fielddataBuilder(String fullyQualifiedIndexNa
8181
}
8282

8383
@Override
84-
public Query distanceFeatureQuery(Object origin, String pivot, float boost, QueryShardContext context) {
84+
public Query distanceFeatureQuery(Object origin, String pivot, QueryShardContext context) {
8585
checkAllowExpensiveQueries(context);
8686
return DateFieldType.handleNow(context, now -> {
8787
long originLong = DateFieldType.parseToLong(
@@ -98,8 +98,7 @@ public Query distanceFeatureQuery(Object origin, String pivot, float boost, Quer
9898
leafFactory(context)::newInstance,
9999
name(),
100100
originLong,
101-
pivotTime.getMillis(),
102-
boost
101+
pivotTime.getMillis()
103102
);
104103
});
105104
}

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/query/LongScriptFieldDistanceFeatureQuery.java

+7-16
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,17 @@
2727
public final class LongScriptFieldDistanceFeatureQuery extends AbstractScriptFieldQuery<AbstractLongFieldScript> {
2828
private final long origin;
2929
private final long pivot;
30-
private final float boost;
3130

3231
public LongScriptFieldDistanceFeatureQuery(
3332
Script script,
3433
Function<LeafReaderContext, AbstractLongFieldScript> leafFactory,
3534
String fieldName,
3635
long origin,
37-
long pivot,
38-
float boost
36+
long pivot
3937
) {
4038
super(script, fieldName, leafFactory);
4139
this.origin = origin;
4240
this.pivot = pivot;
43-
this.boost = boost;
4441
}
4542

4643
@Override
@@ -70,12 +67,11 @@ public Explanation explain(LeafReaderContext context, int doc) {
7067
AbstractLongFieldScript script = scriptContextFunction().apply(context);
7168
script.runForDoc(doc);
7269
long value = valueWithMinAbsoluteDistance(script);
73-
float weight = LongScriptFieldDistanceFeatureQuery.this.boost * boost;
74-
float score = score(weight, distanceFor(value));
70+
float score = score(boost, distanceFor(value));
7571
return Explanation.match(
7672
score,
7773
"Distance score, computed as weight * pivot / (pivot + abs(value - origin)) from:",
78-
Explanation.match(weight, "weight"),
74+
Explanation.match(boost, "weight"),
7975
Explanation.match(pivot, "pivot"),
8076
Explanation.match(origin, "origin"),
8177
Explanation.match(value, "current value")
@@ -105,7 +101,7 @@ public float matchCost() {
105101
}
106102
};
107103
disi = TwoPhaseIterator.asDocIdSetIterator(twoPhase);
108-
this.weight = LongScriptFieldDistanceFeatureQuery.this.boost * boost;
104+
this.weight = boost;
109105
}
110106

111107
@Override
@@ -179,15 +175,14 @@ public String toString(String field) {
179175
}
180176
b.append(getClass().getSimpleName());
181177
b.append("(origin=").append(origin);
182-
b.append(",pivot=").append(pivot);
183-
b.append(",boost=").append(boost).append(")");
178+
b.append(",pivot=").append(pivot).append(")");
184179
return b.toString();
185180

186181
}
187182

188183
@Override
189184
public int hashCode() {
190-
return Objects.hash(super.hashCode(), origin, pivot, boost);
185+
return Objects.hash(super.hashCode(), origin, pivot);
191186
}
192187

193188
@Override
@@ -196,7 +191,7 @@ public boolean equals(Object obj) {
196191
return false;
197192
}
198193
LongScriptFieldDistanceFeatureQuery other = (LongScriptFieldDistanceFeatureQuery) obj;
199-
return origin == other.origin && pivot == other.pivot && boost == other.boost;
194+
return origin == other.origin && pivot == other.pivot;
200195
}
201196

202197
@Override
@@ -214,8 +209,4 @@ long origin() {
214209
long pivot() {
215210
return pivot;
216211
}
217-
218-
float boost() {
219-
return boost;
220-
}
221212
}

x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/DateScriptFieldTypeTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void testDistanceFeatureQuery() throws IOException {
199199
);
200200
try (DirectoryReader reader = iw.getReader()) {
201201
IndexSearcher searcher = newSearcher(reader);
202-
Query query = simpleMappedFieldType().distanceFeatureQuery(1595432181354L, "1ms", 1, mockContext());
202+
Query query = simpleMappedFieldType().distanceFeatureQuery(1595432181354L, "1ms", mockContext());
203203
TopDocs docs = searcher.search(query, 4);
204204
assertThat(docs.scoreDocs, arrayWithSize(3));
205205
assertThat(readSource(reader, docs.scoreDocs[0].doc), equalTo("{\"timestamp\": [1595432181354]}"));
@@ -228,7 +228,7 @@ public void testDistanceFeatureQueryInLoop() throws IOException {
228228
}
229229

230230
private Query randomDistanceFeatureQuery(MappedFieldType ft, QueryShardContext ctx) {
231-
return ft.distanceFeatureQuery(randomDate(), randomTimeValue(), randomFloat(), ctx);
231+
return ft.distanceFeatureQuery(randomDate(), randomTimeValue(), ctx);
232232
}
233233

234234
@Override

x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/query/LongScriptFieldDistanceFeatureQueryTests.java

+11-23
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,12 @@ public class LongScriptFieldDistanceFeatureQueryTests extends AbstractScriptFiel
3535
protected LongScriptFieldDistanceFeatureQuery createTestInstance() {
3636
long origin = randomLong();
3737
long pivot = randomValueOtherThan(origin, ESTestCase::randomLong);
38-
return new LongScriptFieldDistanceFeatureQuery(randomScript(), leafFactory, randomAlphaOfLength(5), origin, pivot, randomFloat());
38+
return new LongScriptFieldDistanceFeatureQuery(randomScript(), leafFactory, randomAlphaOfLength(5), origin, pivot);
3939
}
4040

4141
@Override
4242
protected LongScriptFieldDistanceFeatureQuery copy(LongScriptFieldDistanceFeatureQuery orig) {
43-
return new LongScriptFieldDistanceFeatureQuery(
44-
orig.script(),
45-
leafFactory,
46-
orig.fieldName(),
47-
orig.origin(),
48-
orig.pivot(),
49-
orig.boost()
50-
);
43+
return new LongScriptFieldDistanceFeatureQuery(orig.script(), leafFactory, orig.fieldName(), orig.origin(), orig.pivot());
5144
}
5245

5346
@Override
@@ -56,8 +49,7 @@ protected LongScriptFieldDistanceFeatureQuery mutate(LongScriptFieldDistanceFeat
5649
String fieldName = orig.fieldName();
5750
long origin = orig.origin();
5851
long pivot = orig.pivot();
59-
float boost = orig.boost();
60-
switch (randomInt(4)) {
52+
switch (randomInt(3)) {
6153
case 0:
6254
script = randomValueOtherThan(script, this::randomScript);
6355
break;
@@ -70,13 +62,10 @@ protected LongScriptFieldDistanceFeatureQuery mutate(LongScriptFieldDistanceFeat
7062
case 3:
7163
pivot = randomValueOtherThan(origin, () -> randomValueOtherThan(orig.pivot(), ESTestCase::randomLong));
7264
break;
73-
case 4:
74-
boost = randomValueOtherThan(boost, ESTestCase::randomFloat);
75-
break;
7665
default:
7766
fail();
7867
}
79-
return new LongScriptFieldDistanceFeatureQuery(script, leafFactory, fieldName, origin, pivot, boost);
68+
return new LongScriptFieldDistanceFeatureQuery(script, leafFactory, fieldName, origin, pivot);
8069
}
8170

8271
@Override
@@ -105,12 +94,13 @@ public void execute() {
10594
leafFactory,
10695
"test",
10796
1595432181351L,
108-
6L,
109-
between(1, 100)
97+
3L
11098
);
111-
TopDocs td = searcher.search(query, 1);
112-
assertThat(td.scoreDocs[0].score, equalTo(query.boost()));
99+
TopDocs td = searcher.search(query, 2);
100+
assertThat(td.scoreDocs[0].score, equalTo(1.0f));
113101
assertThat(td.scoreDocs[0].doc, equalTo(1));
102+
assertThat(td.scoreDocs[1].score, equalTo(.5f));
103+
assertThat(td.scoreDocs[1].doc, equalTo(0));
114104
}
115105
}
116106
}
@@ -124,7 +114,7 @@ public void testMaxScore() throws IOException {
124114
float boost = randomFloat();
125115
assertThat(
126116
query.createWeight(searcher, ScoreMode.COMPLETE, boost).scorer(reader.leaves().get(0)).getMaxScore(randomInt()),
127-
equalTo(query.boost() * boost)
117+
equalTo(boost)
128118
);
129119
}
130120
}
@@ -134,9 +124,7 @@ public void testMaxScore() throws IOException {
134124
protected void assertToString(LongScriptFieldDistanceFeatureQuery query) {
135125
assertThat(
136126
query.toString(query.fieldName()),
137-
equalTo(
138-
"LongScriptFieldDistanceFeatureQuery(origin=" + query.origin() + ",pivot=" + query.pivot() + ",boost=" + query.boost() + ")"
139-
)
127+
equalTo("LongScriptFieldDistanceFeatureQuery(origin=" + query.origin() + ",pivot=" + query.pivot() + ")")
140128
);
141129
}
142130

0 commit comments

Comments
 (0)