forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement matches() on SourceConfirmedTextQuery (elastic#100134)
`match_only_text` does not currently support highlighting via the matches option of the default highlighter. This commit implements matches on the backing query for this field, and also fixes a bug where the field type's value fetcher could hold on to the wrong reference for a source lookup, causing threading errors.
- Loading branch information
1 parent
68974a2
commit 5d0c8a3
Showing
5 changed files
with
246 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 100134 | ||
summary: Implement matches() on `SourceConfirmedTextQuery` | ||
area: Highlighting | ||
type: enhancement | ||
issues: [] |
131 changes: 131 additions & 0 deletions
131
...as/src/internalClusterTest/java/org/elasticsearch/index/mapper/MatchOnlyTextMapperIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.index.mapper; | ||
|
||
import org.elasticsearch.action.bulk.BulkRequestBuilder; | ||
import org.elasticsearch.action.bulk.BulkResponse; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.action.support.WriteRequest; | ||
import org.elasticsearch.index.mapper.extras.MapperExtrasPlugin; | ||
import org.elasticsearch.index.query.QueryBuilders; | ||
import org.elasticsearch.plugins.Plugin; | ||
import org.elasticsearch.search.SearchHit; | ||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xcontent.XContentFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; | ||
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
public class MatchOnlyTextMapperIT extends ESIntegTestCase { | ||
|
||
protected Collection<Class<? extends Plugin>> nodePlugins() { | ||
return Arrays.asList(MapperExtrasPlugin.class); | ||
} | ||
|
||
public void testHighlightingWithMatchOnlyTextFieldMatchPhrase() throws IOException { | ||
|
||
// We index and retrieve a large number of documents to ensure that we go over multiple | ||
// segments, to ensure that the highlighter is using the correct segment lookups to | ||
// load the source. | ||
|
||
XContentBuilder mappings = jsonBuilder(); | ||
mappings.startObject().startObject("properties").startObject("message").field("type", "match_only_text").endObject().endObject(); | ||
mappings.endObject(); | ||
assertAcked(prepareCreate("test").setMapping(mappings)); | ||
BulkRequestBuilder bulk = client().prepareBulk("test").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); | ||
for (int i = 0; i < 2000; i++) { | ||
bulk.add( | ||
client().prepareIndex() | ||
.setSource( | ||
XContentFactory.jsonBuilder() | ||
.startObject() | ||
.field( | ||
"message", | ||
"[.ds-.slm-history-5-2023.09.20-" | ||
+ randomInt() | ||
+ "][0] marking and sending shard failed due to [failed recovery]" | ||
) | ||
.endObject() | ||
) | ||
); | ||
} | ||
BulkResponse bulkItemResponses = bulk.get(); | ||
assertNoFailures(bulkItemResponses); | ||
|
||
SearchResponse searchResponse = client().prepareSearch("test") | ||
.setQuery(QueryBuilders.matchPhraseQuery("message", "marking and sending shard")) | ||
.setSize(500) | ||
.highlighter(new HighlightBuilder().field("message")) | ||
.get(); | ||
assertNoFailures(searchResponse); | ||
for (SearchHit searchHit : searchResponse.getHits()) { | ||
assertThat( | ||
searchHit.getHighlightFields().get("message").fragments()[0].string(), | ||
containsString("<em>marking and sending shard</em>") | ||
); | ||
} | ||
} | ||
|
||
public void testHighlightingWithMatchOnlyTextFieldSyntheticSource() throws IOException { | ||
|
||
// We index and retrieve a large number of documents to ensure that we go over multiple | ||
// segments, to ensure that the highlighter is using the correct segment lookups to | ||
// load the source. | ||
|
||
String mappings = """ | ||
{ "_source" : { "mode" : "synthetic" }, | ||
"properties" : { | ||
"message" : { "type" : "match_only_text" } | ||
} | ||
} | ||
"""; | ||
assertAcked(prepareCreate("test").setMapping(mappings)); | ||
BulkRequestBuilder bulk = client().prepareBulk("test").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); | ||
for (int i = 0; i < 2000; i++) { | ||
bulk.add( | ||
client().prepareIndex() | ||
.setSource( | ||
XContentFactory.jsonBuilder() | ||
.startObject() | ||
.field( | ||
"message", | ||
"[.ds-.slm-history-5-2023.09.20-" | ||
+ randomInt() | ||
+ "][0] marking and sending shard failed due to [failed recovery]" | ||
) | ||
.endObject() | ||
) | ||
); | ||
} | ||
BulkResponse bulkItemResponses = bulk.get(); | ||
assertNoFailures(bulkItemResponses); | ||
|
||
SearchResponse searchResponse = client().prepareSearch("test") | ||
.setQuery(QueryBuilders.matchPhraseQuery("message", "marking and sending shard")) | ||
.setSize(500) | ||
.highlighter(new HighlightBuilder().field("message")) | ||
.get(); | ||
assertNoFailures(searchResponse); | ||
for (SearchHit searchHit : searchResponse.getHits()) { | ||
assertThat( | ||
searchHit.getHighlightFields().get("message").fragments()[0].string(), | ||
containsString("<em>marking and sending shard</em>") | ||
); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters