Skip to content

Commit 338e82b

Browse files
committed
Score for match all is 0.0 now
1 parent 9544204 commit 338e82b

File tree

2 files changed

+224
-1
lines changed

2 files changed

+224
-1
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.plugin;
9+
10+
import org.elasticsearch.ElasticsearchException;
11+
import org.elasticsearch.action.index.IndexRequest;
12+
import org.elasticsearch.action.support.WriteRequest;
13+
import org.elasticsearch.common.settings.Settings;
14+
import org.elasticsearch.xpack.esql.EsqlTestUtils;
15+
import org.elasticsearch.xpack.esql.VerificationException;
16+
import org.elasticsearch.xpack.esql.action.AbstractEsqlIntegTestCase;
17+
import org.junit.Before;
18+
19+
import java.util.List;
20+
21+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
22+
import static org.hamcrest.CoreMatchers.containsString;
23+
import static org.hamcrest.Matchers.equalTo;
24+
25+
//@TestLogging(value = "org.elasticsearch.xpack.esql:TRACE,org.elasticsearch.compute:TRACE", reason = "debug")
26+
public class ScoringIT extends AbstractEsqlIntegTestCase {
27+
28+
@Before
29+
public void setupIndex() {
30+
createAndPopulateIndex();
31+
}
32+
33+
public void testWhereMatchWithScoring() {
34+
var query = """
35+
FROM test
36+
METADATA _score
37+
| WHERE match(content, "fox")
38+
| KEEP id, _score
39+
| SORT id ASC
40+
""";
41+
42+
try (var resp = run(query)) {
43+
assertColumnNames(resp.columns(), List.of("id", "_score"));
44+
assertColumnTypes(resp.columns(), List.of("integer", "double"));
45+
assertValues(resp.values(), List.of(List.of(1, 1.156558871269226), List.of(6, 0.9114001989364624)));
46+
}
47+
}
48+
49+
public void testWhereMatchWithScoringDifferentSort() {
50+
51+
var query = """
52+
FROM test
53+
METADATA _score
54+
| WHERE match(content, "fox")
55+
| KEEP id, _score
56+
| SORT id DESC
57+
""";
58+
59+
try (var resp = run(query)) {
60+
assertColumnNames(resp.columns(), List.of("id", "_score"));
61+
assertColumnTypes(resp.columns(), List.of("integer", "double"));
62+
assertValues(resp.values(), List.of(List.of(6, 0.9114001989364624), List.of(1, 1.156558871269226)));
63+
}
64+
}
65+
66+
public void testWhereMatchWithScoringSortScore() {
67+
var query = """
68+
FROM test
69+
METADATA _score
70+
| WHERE match(content, "fox")
71+
| KEEP id, _score
72+
| SORT _score DESC
73+
""";
74+
75+
try (var resp = run(query)) {
76+
assertColumnNames(resp.columns(), List.of("id", "_score"));
77+
assertColumnTypes(resp.columns(), List.of("integer", "double"));
78+
assertValues(resp.values(), List.of(List.of(1, 1.156558871269226), List.of(6, 0.9114001989364624)));
79+
}
80+
}
81+
82+
public void testWhereMatchWithScoringNoSort() {
83+
var query = """
84+
FROM test
85+
METADATA _score
86+
| WHERE match(content, "fox")
87+
| KEEP id, _score
88+
""";
89+
90+
try (var resp = run(query)) {
91+
assertColumnNames(resp.columns(), List.of("id", "_score"));
92+
assertColumnTypes(resp.columns(), List.of("integer", "double"));
93+
assertValuesInAnyOrder(resp.values(), List.of(List.of(1, 1.156558871269226), List.of(6, 0.9114001989364624)));
94+
}
95+
}
96+
97+
public void testNonExistingColumn() {
98+
var query = """
99+
FROM test
100+
| WHERE match(something, "fox")
101+
""";
102+
103+
var error = expectThrows(VerificationException.class, () -> run(query));
104+
assertThat(error.getMessage(), containsString("Unknown column [something]"));
105+
}
106+
107+
public void testWhereMatchEvalColumn() {
108+
var query = """
109+
FROM test
110+
| EVAL upper_content = to_upper(content)
111+
| WHERE match(upper_content, "FOX")
112+
| KEEP id
113+
""";
114+
115+
var error = expectThrows(VerificationException.class, () -> run(query));
116+
assertThat(
117+
error.getMessage(),
118+
containsString("[MATCH] function cannot operate on [upper_content], which is not a field from an index mapping")
119+
);
120+
}
121+
122+
public void testWhereMatchOverWrittenColumn() {
123+
var query = """
124+
FROM test
125+
| DROP content
126+
| EVAL content = CONCAT("document with ID ", to_str(id))
127+
| WHERE match(content, "document")
128+
""";
129+
130+
var error = expectThrows(VerificationException.class, () -> run(query));
131+
assertThat(
132+
error.getMessage(),
133+
containsString("[MATCH] function cannot operate on [content], which is not a field from an index mapping")
134+
);
135+
}
136+
137+
public void testWhereMatchAfterStats() {
138+
var query = """
139+
FROM test
140+
| STATS count(*)
141+
| WHERE match(content, "fox")
142+
""";
143+
144+
var error = expectThrows(VerificationException.class, () -> run(query));
145+
assertThat(error.getMessage(), containsString("Unknown column [content]"));
146+
}
147+
148+
public void testWhereMatchNotPushedDown() {
149+
var query = """
150+
FROM test
151+
| WHERE match(content, "fox") OR length(content) < 20
152+
| KEEP id
153+
| SORT id
154+
""";
155+
156+
try (var resp = run(query)) {
157+
assertColumnNames(resp.columns(), List.of("id"));
158+
assertColumnTypes(resp.columns(), List.of("integer"));
159+
assertValues(resp.values(), List.of(List.of(1), List.of(2), List.of(6)));
160+
}
161+
}
162+
163+
public void testWhereMatchWithRow() {
164+
var query = """
165+
ROW content = "a brown fox"
166+
| WHERE match(content, "fox")
167+
""";
168+
169+
var error = expectThrows(ElasticsearchException.class, () -> run(query));
170+
assertThat(
171+
error.getMessage(),
172+
containsString("line 2:15: [MATCH] function cannot operate on [content], which is not a field from an index mapping")
173+
);
174+
}
175+
176+
public void testMatchWithinEval() {
177+
var query = """
178+
FROM test
179+
| EVAL matches_query = match(content, "fox")
180+
""";
181+
182+
var error = expectThrows(VerificationException.class, () -> run(query));
183+
assertThat(error.getMessage(), containsString("[MATCH] function is only supported in WHERE commands"));
184+
}
185+
186+
public void testMatchAllScoring() {
187+
var query = """
188+
FROM test
189+
METADATA _score
190+
| KEEP id, _score
191+
| SORT id ASC
192+
""";
193+
194+
try (var resp = run(query)) {
195+
assertColumnNames(resp.columns(), List.of("id", "_score"));
196+
assertColumnTypes(resp.columns(), List.of("integer", "double"));
197+
List<List<Object>> values = EsqlTestUtils.getValuesList(resp.values());
198+
assertThat(values.size(), equalTo(6));
199+
for (List<Object> value : values) {
200+
assertThat((Double) value.get(1), equalTo(0.0));
201+
}
202+
}
203+
}
204+
205+
private void createAndPopulateIndex() {
206+
var indexName = "test";
207+
var client = client().admin().indices();
208+
var CreateRequest = client.prepareCreate(indexName)
209+
.setSettings(Settings.builder().put("index.number_of_shards", 1))
210+
.setMapping("id", "type=integer", "content", "type=text");
211+
assertAcked(CreateRequest);
212+
client().prepareBulk()
213+
.add(new IndexRequest(indexName).id("1").source("id", 1, "content", "This is a brown fox"))
214+
.add(new IndexRequest(indexName).id("2").source("id", 2, "content", "This is a brown dog"))
215+
.add(new IndexRequest(indexName).id("3").source("id", 3, "content", "This dog is really brown"))
216+
.add(new IndexRequest(indexName).id("4").source("id", 4, "content", "The dog is brown but this document is very very long"))
217+
.add(new IndexRequest(indexName).id("5").source("id", 5, "content", "There is also a white cat"))
218+
.add(new IndexRequest(indexName).id("6").source("id", 6, "content", "The quick brown fox jumps over the lazy dog"))
219+
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
220+
.get();
221+
ensureYellow(indexName);
222+
}
223+
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/EsPhysicalOperationProviders.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private static class DefaultShardContextForUnmappedField extends DefaultShardCon
173173
}
174174

175175
public Function<org.elasticsearch.compute.lucene.ShardContext, Query> querySupplier(QueryBuilder builder) {
176-
QueryBuilder qb = builder == null ? QueryBuilders.matchAllQuery() : builder;
176+
QueryBuilder qb = builder == null ? QueryBuilders.matchAllQuery().boost(0.0f) : builder;
177177
return ctx -> shardContexts.get(ctx.index()).toQuery(qb);
178178
}
179179

0 commit comments

Comments
 (0)