Skip to content

Commit 7d7fedf

Browse files
committed
Match _id working: search value wasn't being encoded
1 parent f853208 commit 7d7fedf

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/*
23
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
34
* or more contributor license agreements. Licensed under the "Elastic License
@@ -16,16 +17,12 @@
1617

1718
import java.util.Collection;
1819
import java.util.Collections;
20+
import java.util.List;
1921

2022
/**
2123
* A mapper for the _id field.
2224
*/
2325
public class LogsdbIdFieldMapper extends IdFieldMapper {
24-
// here
25-
public static final String NAME = "_id";
26-
27-
public static final String CONTENT_TYPE = "_id";
28-
2926
public static final LogsdbIdFieldMapper INSTANCE = new LogsdbIdFieldMapper();
3027

3128
public LogsdbIdFieldMapper() {
@@ -39,7 +36,7 @@ public void preParse(DocumentParserContext context) {
3936
}
4037
context.id(context.sourceToParse().id());
4138
BytesRef uidEncoded = Uid.encodeId(context.id());
42-
context.doc().add(new SortedDocValuesField(fieldType().name(), uidEncoded));
39+
context.doc().add(SortedDocValuesField.indexedField(fieldType().name(), uidEncoded));
4340
}
4441

4542
@Override
@@ -60,12 +57,12 @@ public String reindexId(String id) {
6057
protected static class LogsdbIdFieldType extends TermBasedFieldType {
6158

6259
public LogsdbIdFieldType() {
63-
super(NAME, false, false, true, TextSearchInfo.SIMPLE_MATCH_ONLY, Collections.emptyMap());
60+
super(IdFieldMapper.NAME, false, false, true, TextSearchInfo.SIMPLE_MATCH_ONLY, Collections.emptyMap());
6461
}
6562

6663
@Override
6764
public String typeName() {
68-
return CONTENT_TYPE;
65+
return IdFieldMapper.CONTENT_TYPE;
6966
}
7067

7168
@Override
@@ -76,24 +73,33 @@ public boolean isSearchable() {
7673

7774
@Override
7875
public Query termsQuery(Collection<?> values, SearchExecutionContext context) {
79-
var bytesRefs = values.stream().map(this::indexedValueForSearch).toList();
76+
var bytesRefs = values.stream()
77+
.map(LogsdbIdFieldType::encode)
78+
.map(this::indexedValueForSearch).toList();
8079
return SortedDocValuesField.newSlowSetQuery(name(), bytesRefs);
8180
}
8281

8382
@Override
8483
public Query termQuery(Object value, SearchExecutionContext context) {
85-
return SortedDocValuesField.newSlowExactQuery(name(), indexedValueForSearch(value));
84+
return SortedDocValuesField.newSlowExactQuery(name(), indexedValueForSearch(encode(value)));
8685
}
8786

8887
@Override
8988
public BlockLoader blockLoader(BlockLoaderContext blContext) {
90-
return new BlockDocValuesReader.BytesRefsFromOrdsBlockLoader(NAME);
89+
return new BlockDocValuesReader.BytesRefsFromOrdsBlockLoader(IdFieldMapper.NAME);
9190
}
9291

9392
@Override
9493
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
9594
// TODO can this be done somehow?
9695
throw new UnsupportedOperationException("logsdb id cannot be fetched by values since only using doc values");
9796
}
97+
98+
private static BytesRef encode(Object idObject) {
99+
if (idObject instanceof BytesRef) {
100+
idObject = ((BytesRef) idObject).utf8ToString();
101+
}
102+
return Uid.encodeId(idObject.toString());
103+
}
98104
}
99105
}

server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ protected void setNextReader(LeafReaderContext ctx, int[] docsInLeaf) throws IOE
153153
this.leafNestedDocuments = nestedDocuments.getLeafNestedDocuments(ctx);
154154
this.leafStoredFieldLoader = storedFieldLoader.getLoader(ctx, docsInLeaf);
155155
this.leafSourceLoader = sourceLoader.leaf(ctx.reader(), docsInLeaf);
156-
this.leafIdLoader = idLoader.leaf(leafStoredFieldLoader, ctx.reader(), docsInLeaf);
156+
this.leafIdLoader = idLoader.leaf(leafSourceLoader, leafStoredFieldLoader, ctx.reader(), docsInLeaf);
157157
fieldLookupProvider.setNextReader(ctx);
158158
for (FetchSubPhaseProcessor processor : processors) {
159159
processor.setNextReader(ctx);

x-pack/plugin/logsdb/src/internalClusterTest/java/org/elasticsearch/xpack/logsdb/LogsIdIT.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@
3030
import org.elasticsearch.index.query.TermQueryBuilder;
3131
import org.elasticsearch.license.LicenseSettings;
3232
import org.elasticsearch.plugins.Plugin;
33+
import org.elasticsearch.search.SearchHit;
3334
import org.elasticsearch.search.builder.SearchSourceBuilder;
35+
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
3436
import org.elasticsearch.test.ESSingleNodeTestCase;
3537
import org.elasticsearch.test.InternalSettingsPlugin;
3638
import org.elasticsearch.xcontent.XContentType;
3739
import org.elasticsearch.xpack.core.XPackPlugin;
3840

3941
import java.io.IOException;
4042
import java.time.Instant;
43+
import java.util.Arrays;
4144
import java.util.Collection;
45+
import java.util.Comparator;
4246
import java.util.List;
4347
import java.util.UUID;
4448
import java.util.concurrent.ExecutionException;
@@ -106,13 +110,6 @@ protected Settings nodeSettings() {
106110
.build();
107111
}
108112

109-
public void testStandard() throws Exception {
110-
String dataStreamName = "k8s";
111-
createTemplate(dataStreamName);
112-
checkIndexSearchAndRetrieval(dataStreamName, false);
113-
}
114-
115-
116113
public void testGetByGeneratedId() throws Exception {
117114
String dataStreamName = "k8s";
118115
createTemplate(dataStreamName);
@@ -125,7 +122,7 @@ public void testGetByGeneratedId() throws Exception {
125122
indexRequest.source(
126123
DOC.replace("$time", formatInstant(time))
127124
.replace("$uuid", UUID.randomUUID().toString())
128-
.replace("$pod", "pod-" + randomIntBetween(0, 10)),
125+
.replace("$pod", "pod-" + j),
129126
XContentType.JSON
130127
);
131128
bulkRequest.add(indexRequest);
@@ -143,11 +140,13 @@ public void testGetByGeneratedId() throws Exception {
143140
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo((long) numDocs));
144141

145142
for (int i = 0; i < searchResponse.getHits().getHits().length; i++) {
146-
String id = searchResponse.getHits().getHits()[i].getId();
143+
SearchHit hit = searchResponse.getHits().getHits()[i];
144+
String id = hit.getId();
147145
assertThat(id, notNullValue());
148146

149147
// Check that the _id is gettable:
150-
var getResponse = client().get(new GetRequest(indexName).id(id)).actionGet();
148+
var getRequest = new GetRequest(indexName).id(id);
149+
var getResponse = client().get(getRequest).actionGet();
151150
assertThat(getResponse.isExists(), is(true));
152151
assertThat(getResponse.getId(), equalTo(id));
153152
}
@@ -163,11 +162,11 @@ public void testGetByProvidedID() throws Exception {
163162
for (int j = 0; j < numDocs; j++) {
164163
var indexRequest = new IndexRequest(indexName)
165164
.opType(DocWriteRequest.OpType.INDEX)
166-
.id("some-id-" + j);
165+
.id("id-" + j);
167166
indexRequest.source(
168167
DOC.replace("$time", formatInstant(time))
169168
.replace("$uuid", UUID.randomUUID().toString())
170-
.replace("$pod", "pod-" + randomIntBetween(0, 10)),
169+
.replace("$pod", "pod-" + j),
171170
XContentType.JSON
172171
);
173172
bulkRequest.add(indexRequest);
@@ -188,22 +187,24 @@ public void testGetByProvidedID() throws Exception {
188187
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo((long) numDocs));
189188

190189
for (int i = 0; i < searchResponse.getHits().getHits().length; i++) {
191-
String id = searchResponse.getHits().getHits()[i].getId();
190+
SearchHit hit = searchResponse.getHits().getHits()[i];
191+
String id = hit.getId();
192+
String numPart = id.split("-")[1];
192193
assertThat(id, notNullValue());
193-
194-
// test get with the IDs from search response
195-
var getResponse = client().get(new GetRequest(indexName).id(id)).actionGet();
196-
assertThat(getResponse.isExists(), is(true));
197-
assertThat(getResponse.getId(), equalTo(id));
194+
// check got correct doc in search response
195+
var pod = (String) hit.getSourceAsMap().get("message");
196+
assertThat(pod, equalTo("pod-" + numPart));
198197
}
199198
});
200199

201200
// test get with the provided IDs
202201
for (int i = 0; i < numDocs; i++) {
203-
String id = "some-id-" + i;
204-
var getResponse = client().get(new GetRequest(indexName).id(id)).actionGet();
202+
String id = "id-" + i;
203+
var getRequest = new GetRequest(indexName).id(id).fetchSourceContext(FetchSourceContext.FETCH_SOURCE);
204+
var getResponse = client().get(getRequest).actionGet();
205205
assertThat(getResponse.isExists(), is(true));
206206
assertThat(getResponse.getId(), equalTo(id));
207+
assertThat(getResponse.getSourceAsMap().get("message"), equalTo("pod-" + i));
207208
}
208209
}
209210

@@ -216,7 +217,7 @@ public void testMatchByProvidedID() throws Exception {
216217
for (int j = 0; j < numDocs; j++) {
217218
var indexRequest = new IndexRequest(indexName)
218219
.opType(DocWriteRequest.OpType.INDEX)
219-
.id("some-id-" + j);
220+
.id("id-" + j);
220221
indexRequest.source(
221222
DOC.replace("$time", formatInstant(time))
222223
.replace("$uuid", UUID.randomUUID().toString())
@@ -231,12 +232,12 @@ public void testMatchByProvidedID() throws Exception {
231232
client().admin().indices().refresh(new RefreshRequest(indexName)).actionGet();
232233

233234
for (int i = 0; i < numDocs; i++) {
234-
String id = "some-id-" + i;
235+
String id = "id-" + i;
235236
var searchRequest = new SearchRequest(indexName);
236237
searchRequest.source(new SearchSourceBuilder().query(new TermQueryBuilder("_id", id)).size(10));
237238
var searchResponse = client().search(searchRequest).actionGet();
238239
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo((long) 1));
239-
assertThat(searchResponse.getHits().getAt(0).field("message"), equalTo("pod-" + i));
240+
assertThat(searchResponse.getHits().getHits()[0].getSourceAsMap().get("message"), equalTo("pod-" + i));
240241
}
241242
}
242243

0 commit comments

Comments
 (0)