Skip to content

Commit 2c0145a

Browse files
authored
Drop boost from runtime distance feature query (#63949) (#64005)
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 a685d96 commit 2c0145a

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,11 @@ public static long parseToLong(
441441
}
442442

443443
@Override
444-
public Query distanceFeatureQuery(Object origin, String pivot, float boost, QueryShardContext context) {
444+
public Query distanceFeatureQuery(Object origin, String pivot, QueryShardContext context) {
445445
long originLong = parseToLong(origin, true, null, null, context::nowInMillis);
446446
TimeValue pivotTime = TimeValue.parseTimeValue(pivot, "distance_feature.pivot");
447-
return resolution.distanceFeatureQuery(name(), boost, originLong, pivotTime);
447+
// As we already apply boost in AbstractQueryBuilder::toQuery, we always passing a boost of 1.0 to distanceFeatureQuery
448+
return resolution.distanceFeatureQuery(name(), 1.0f, originLong, pivotTime);
448449
}
449450

450451
@Override

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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew
305305
+ "] which is of type [" + typeName() + "]");
306306
}
307307

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

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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 7 additions & 16 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public void testDistanceFeatureQuery() throws IOException {
222222
);
223223
try (DirectoryReader reader = iw.getReader()) {
224224
IndexSearcher searcher = newSearcher(reader);
225-
Query query = simpleMappedFieldType().distanceFeatureQuery(1595432181354L, "1ms", 1, mockContext());
225+
Query query = simpleMappedFieldType().distanceFeatureQuery(1595432181354L, "1ms", mockContext());
226226
TopDocs docs = searcher.search(query, 4);
227227
assertThat(docs.scoreDocs, arrayWithSize(3));
228228
assertThat(readSource(reader, docs.scoreDocs[0].doc), equalTo("{\"timestamp\": [1595432181354]}"));
@@ -251,7 +251,7 @@ public void testDistanceFeatureQueryInLoop() throws IOException {
251251
}
252252

253253
private Query randomDistanceFeatureQuery(MappedFieldType ft, QueryShardContext ctx) {
254-
return ft.distanceFeatureQuery(randomDate(), randomTimeValue(), randomFloat(), ctx);
254+
return ft.distanceFeatureQuery(randomDate(), randomTimeValue(), ctx);
255255
}
256256

257257
@Override

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

Lines changed: 11 additions & 23 deletions
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
@@ -109,12 +98,13 @@ public void execute() {
10998
leafFactory,
11099
"test",
111100
1595432181351L,
112-
6L,
113-
between(1, 100)
101+
3L
114102
);
115-
TopDocs td = searcher.search(query, 1);
116-
assertThat(td.scoreDocs[0].score, equalTo(query.boost()));
103+
TopDocs td = searcher.search(query, 2);
104+
assertThat(td.scoreDocs[0].score, equalTo(1.0f));
117105
assertThat(td.scoreDocs[0].doc, equalTo(1));
106+
assertThat(td.scoreDocs[1].score, equalTo(.5f));
107+
assertThat(td.scoreDocs[1].doc, equalTo(0));
118108
}
119109
}
120110
}
@@ -128,7 +118,7 @@ public void testMaxScore() throws IOException {
128118
float boost = randomFloat();
129119
assertThat(
130120
query.createWeight(searcher, ScoreMode.COMPLETE, boost).scorer(reader.leaves().get(0)).getMaxScore(randomInt()),
131-
equalTo(query.boost() * boost)
121+
equalTo(boost)
132122
);
133123
}
134124
}
@@ -138,9 +128,7 @@ public void testMaxScore() throws IOException {
138128
protected void assertToString(LongScriptFieldDistanceFeatureQuery query) {
139129
assertThat(
140130
query.toString(query.fieldName()),
141-
equalTo(
142-
"LongScriptFieldDistanceFeatureQuery(origin=" + query.origin() + ",pivot=" + query.pivot() + ",boost=" + query.boost() + ")"
143-
)
131+
equalTo("LongScriptFieldDistanceFeatureQuery(origin=" + query.origin() + ",pivot=" + query.pivot() + ")")
144132
);
145133
}
146134

0 commit comments

Comments
 (0)