Skip to content

Commit ef84b05

Browse files
committed
aligned assert to codebase
1 parent 5f55a1a commit ef84b05

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
1111

12+
import net.bytebuddy.utility.RandomString;
13+
1214
import org.apache.lucene.analysis.Analyzer;
1315
import org.apache.lucene.analysis.TokenFilter;
1416
import org.apache.lucene.analysis.TokenStream;
@@ -3507,94 +3509,91 @@ public void testConstantKeywordFieldHighlighting() throws IOException {
35073509
// check that constant_keyword highlighting works
35083510
String index = "test";
35093511
String constantKeywordFieldName = "level";
3512+
String constantValue = new RandomString(5).nextString();
35103513

3511-
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName);
3514+
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue);
35123515
assertAcked(prepareCreate(index).setMapping(mappings));
35133516

35143517
XContentBuilder document = jsonBuilder().startObject()
35153518
.field("message", "some text")
3516-
.field("level", "DEBUG")
3519+
.field("level", constantValue)
35173520
.endObject();
35183521
saveDocumentIntoIndex(index, "1", document);
35193522

3520-
SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEBUG"));
3523+
SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery(constantValue));
35213524

35223525
assertNoFailures(search);
3523-
assertThat(
3524-
getHighlightedStringFromSearch(search, constantKeywordFieldName),
3525-
equalTo("<em>DEBUG</em>")
3526-
);
3526+
assertHighlight(search, 0, constantKeywordFieldName, 0, 1, equalTo("<em>%s</em>".formatted(constantValue)));
35273527
}
35283528

35293529
public void testImplicitConstantKeywordFieldHighlighting() throws IOException {
35303530
//Constant field value is defined by the mapping
35313531
String index = "test";
35323532
String constantKeywordFieldName = "level";
3533+
String constantValue = new RandomString(5).nextString();
35333534

3534-
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName);
3535+
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue);
35353536
assertAcked(prepareCreate(index).setMapping(mappings));
35363537

35373538
XContentBuilder document = jsonBuilder().startObject()
35383539
.field("message", "some text")
35393540
.endObject();
35403541
saveDocumentIntoIndex(index, "1", document);
35413542

3542-
SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEBUG"));
3543+
SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery(constantValue));
35433544

35443545
assertNoFailures(search);
3545-
assertThat(
3546-
getHighlightedStringFromSearch(search, constantKeywordFieldName),
3547-
equalTo("<em>DEBUG</em>")
3548-
);
3546+
assertHighlight(search, 0, constantKeywordFieldName, 0, 1, equalTo("<em>%s</em>".formatted(constantValue)));
35493547
}
35503548

35513549
public void testConstantKeywordFieldPartialNoHighlighting() throws IOException {
35523550
String index = "test";
35533551
String constantKeywordFieldName = "level";
3552+
String constantValue = new RandomString(5).nextString();
3553+
String partialConstantValue = constantValue.substring(0, 3);
35543554

3555-
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName);
3555+
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue);
35563556
assertAcked(prepareCreate(index).setMapping(mappings));
35573557

35583558
XContentBuilder document = jsonBuilder().startObject()
35593559
.field("message", "some text")
35603560
.endObject();
35613561
saveDocumentIntoIndex(index, "1", document);
35623562

3563-
SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEB"));
3563+
SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery(partialConstantValue));
35643564

35653565
assertNoFailures(search);
3566-
assertEquals(0, search.getHits().getHits().length);
3566+
assertHitCount(search, 0);
35673567
}
35683568

35693569
public void testConstantKeywordFieldPartialWithWildcardHighlighting() throws IOException {
3570-
// check that constant_keyword highlighting works
3571-
XContentBuilder mappings = prepareConstantKeywordMappings("level");
3570+
String index = "test";
3571+
String constantKeywordFieldName = "level";
3572+
String constantValue = new RandomString(5).nextString();
3573+
String partialConstantValueWithWildCard = "%s*".formatted(constantValue.substring(0, 3));
35723574

3573-
assertAcked(prepareCreate("test").setMapping(mappings));
3575+
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue);
3576+
assertAcked(prepareCreate(index).setMapping(mappings));
35743577

35753578
XContentBuilder document = jsonBuilder().startObject()
35763579
.field("message", "some text")
35773580
.endObject();
3581+
saveDocumentIntoIndex(index, "1", document);
35783582

3579-
saveDocumentIntoIndex("test", "1", document);
3580-
3581-
SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEB*"));
3583+
SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery(partialConstantValueWithWildCard));
35823584

35833585
assertNoFailures(search);
3584-
assertThat(
3585-
getHighlightedStringFromSearch(search, "level"),
3586-
equalTo("<em>DEBUG</em>")
3587-
);
3586+
assertHighlight(search, 0, constantKeywordFieldName, 0, 1, equalTo("<em>%s</em>".formatted(constantValue)));
35883587
}
35893588

3590-
private XContentBuilder prepareConstantKeywordMappings(String constantKeywordFieldName) throws IOException {
3589+
private XContentBuilder prepareConstantKeywordMappings(String constantKeywordFieldName, String constantValue) throws IOException {
35913590
XContentBuilder mappings = jsonBuilder();
35923591
mappings.startObject();
35933592
mappings.startObject("_doc")
35943593
.startObject("properties")
35953594
.startObject(constantKeywordFieldName)
35963595
.field("type", "constant_keyword")
3597-
.field("value", "DEBUG")
3596+
.field("value", constantValue)
35983597
.endObject()
35993598
.startObject("message")
36003599
.field("type", "text")

0 commit comments

Comments
 (0)