|
9 | 9 |
|
10 | 10 | import com.carrotsearch.randomizedtesting.generators.RandomPicks; |
11 | 11 |
|
| 12 | +import net.bytebuddy.utility.RandomString; |
| 13 | + |
12 | 14 | import org.apache.lucene.analysis.Analyzer; |
13 | 15 | import org.apache.lucene.analysis.TokenFilter; |
14 | 16 | import org.apache.lucene.analysis.TokenStream; |
@@ -3507,94 +3509,91 @@ public void testConstantKeywordFieldHighlighting() throws IOException { |
3507 | 3509 | // check that constant_keyword highlighting works |
3508 | 3510 | String index = "test"; |
3509 | 3511 | String constantKeywordFieldName = "level"; |
| 3512 | + String constantValue = new RandomString(5).nextString(); |
3510 | 3513 |
|
3511 | | - XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName); |
| 3514 | + XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue); |
3512 | 3515 | assertAcked(prepareCreate(index).setMapping(mappings)); |
3513 | 3516 |
|
3514 | 3517 | XContentBuilder document = jsonBuilder().startObject() |
3515 | 3518 | .field("message", "some text") |
3516 | | - .field("level", "DEBUG") |
| 3519 | + .field("level", constantValue) |
3517 | 3520 | .endObject(); |
3518 | 3521 | saveDocumentIntoIndex(index, "1", document); |
3519 | 3522 |
|
3520 | | - SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEBUG")); |
| 3523 | + SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery(constantValue)); |
3521 | 3524 |
|
3522 | 3525 | 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))); |
3527 | 3527 | } |
3528 | 3528 |
|
3529 | 3529 | public void testImplicitConstantKeywordFieldHighlighting() throws IOException { |
3530 | 3530 | //Constant field value is defined by the mapping |
3531 | 3531 | String index = "test"; |
3532 | 3532 | String constantKeywordFieldName = "level"; |
| 3533 | + String constantValue = new RandomString(5).nextString(); |
3533 | 3534 |
|
3534 | | - XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName); |
| 3535 | + XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue); |
3535 | 3536 | assertAcked(prepareCreate(index).setMapping(mappings)); |
3536 | 3537 |
|
3537 | 3538 | XContentBuilder document = jsonBuilder().startObject() |
3538 | 3539 | .field("message", "some text") |
3539 | 3540 | .endObject(); |
3540 | 3541 | saveDocumentIntoIndex(index, "1", document); |
3541 | 3542 |
|
3542 | | - SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEBUG")); |
| 3543 | + SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery(constantValue)); |
3543 | 3544 |
|
3544 | 3545 | 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))); |
3549 | 3547 | } |
3550 | 3548 |
|
3551 | 3549 | public void testConstantKeywordFieldPartialNoHighlighting() throws IOException { |
3552 | 3550 | String index = "test"; |
3553 | 3551 | String constantKeywordFieldName = "level"; |
| 3552 | + String constantValue = new RandomString(5).nextString(); |
| 3553 | + String partialConstantValue = constantValue.substring(0, 3); |
3554 | 3554 |
|
3555 | | - XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName); |
| 3555 | + XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue); |
3556 | 3556 | assertAcked(prepareCreate(index).setMapping(mappings)); |
3557 | 3557 |
|
3558 | 3558 | XContentBuilder document = jsonBuilder().startObject() |
3559 | 3559 | .field("message", "some text") |
3560 | 3560 | .endObject(); |
3561 | 3561 | saveDocumentIntoIndex(index, "1", document); |
3562 | 3562 |
|
3563 | | - SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEB")); |
| 3563 | + SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery(partialConstantValue)); |
3564 | 3564 |
|
3565 | 3565 | assertNoFailures(search); |
3566 | | - assertEquals(0, search.getHits().getHits().length); |
| 3566 | + assertHitCount(search, 0); |
3567 | 3567 | } |
3568 | 3568 |
|
3569 | 3569 | 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)); |
3572 | 3574 |
|
3573 | | - assertAcked(prepareCreate("test").setMapping(mappings)); |
| 3575 | + XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue); |
| 3576 | + assertAcked(prepareCreate(index).setMapping(mappings)); |
3574 | 3577 |
|
3575 | 3578 | XContentBuilder document = jsonBuilder().startObject() |
3576 | 3579 | .field("message", "some text") |
3577 | 3580 | .endObject(); |
| 3581 | + saveDocumentIntoIndex(index, "1", document); |
3578 | 3582 |
|
3579 | | - saveDocumentIntoIndex("test", "1", document); |
3580 | | - |
3581 | | - SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEB*")); |
| 3583 | + SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery(partialConstantValueWithWildCard)); |
3582 | 3584 |
|
3583 | 3585 | 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))); |
3588 | 3587 | } |
3589 | 3588 |
|
3590 | | - private XContentBuilder prepareConstantKeywordMappings(String constantKeywordFieldName) throws IOException { |
| 3589 | + private XContentBuilder prepareConstantKeywordMappings(String constantKeywordFieldName, String constantValue) throws IOException { |
3591 | 3590 | XContentBuilder mappings = jsonBuilder(); |
3592 | 3591 | mappings.startObject(); |
3593 | 3592 | mappings.startObject("_doc") |
3594 | 3593 | .startObject("properties") |
3595 | 3594 | .startObject(constantKeywordFieldName) |
3596 | 3595 | .field("type", "constant_keyword") |
3597 | | - .field("value", "DEBUG") |
| 3596 | + .field("value", constantValue) |
3598 | 3597 | .endObject() |
3599 | 3598 | .startObject("message") |
3600 | 3599 | .field("type", "text") |
|
0 commit comments