|
25 | 25 | import org.elasticsearch.common.geo.GeoPoint; |
26 | 26 | import org.elasticsearch.common.settings.Settings; |
27 | 27 | import org.elasticsearch.common.settings.Settings.Builder; |
| 28 | +import org.elasticsearch.common.text.Text; |
28 | 29 | import org.elasticsearch.common.time.DateFormatter; |
29 | 30 | import org.elasticsearch.common.util.Maps; |
30 | 31 | import org.elasticsearch.index.analysis.AbstractIndexAnalyzerProvider; |
|
44 | 45 | import org.elasticsearch.plugins.Plugin; |
45 | 46 | import org.elasticsearch.rest.RestStatus; |
46 | 47 | import org.elasticsearch.search.SearchHit; |
| 48 | +import org.elasticsearch.search.SearchHits; |
47 | 49 | import org.elasticsearch.search.builder.SearchSourceBuilder; |
48 | 50 | import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.BoundaryScannerType; |
49 | 51 | import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field; |
|
75 | 77 | import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; |
76 | 78 | import static org.elasticsearch.index.query.QueryBuilders.existsQuery; |
77 | 79 | import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery; |
78 | | -import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; |
79 | 80 | import static org.elasticsearch.index.query.QueryBuilders.matchPhrasePrefixQuery; |
80 | 81 | import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; |
81 | 82 | import static org.elasticsearch.index.query.QueryBuilders.matchQuery; |
@@ -3502,6 +3503,120 @@ public void testDisableHighlightIdField() throws Exception { |
3502 | 3503 | } |
3503 | 3504 | } |
3504 | 3505 |
|
| 3506 | + public void testImplicitConstantKeywordFieldHighlighting() throws IOException { |
| 3507 | + //Constant field value is defined by the mapping |
| 3508 | + XContentBuilder mappings = prepareConstantKeywordMappings("level"); |
| 3509 | + |
| 3510 | + assertAcked(prepareCreate("test").setMapping(mappings)); |
| 3511 | + |
| 3512 | + client().prepareIndex("test") |
| 3513 | + .setId("1") |
| 3514 | + .setSource( |
| 3515 | + jsonBuilder().startObject() |
| 3516 | + .field("message", "some text") |
| 3517 | + .endObject() |
| 3518 | + ) |
| 3519 | + .get(); |
| 3520 | + refresh(); |
| 3521 | + |
| 3522 | + SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEBUG")); |
| 3523 | + assertNoFailures(search); |
| 3524 | + assertThat( |
| 3525 | + getHighlighedStringFromSearch(search, "level"), |
| 3526 | + equalTo("<em>DEBUG</em>") |
| 3527 | + ); |
| 3528 | + } |
| 3529 | + |
| 3530 | + public void testConstantKeywordFieldHighlighting() throws IOException { |
| 3531 | + // check that constant_keyword highlighting works |
| 3532 | + XContentBuilder mappings = prepareConstantKeywordMappings("level"); |
| 3533 | + |
| 3534 | + assertAcked(prepareCreate("test").setMapping(mappings)); |
| 3535 | + |
| 3536 | + client().prepareIndex("test") |
| 3537 | + .setId("1") |
| 3538 | + .setSource( |
| 3539 | + jsonBuilder().startObject() |
| 3540 | + .field("message", "some text") |
| 3541 | + .field("level", "DEBUG") |
| 3542 | + .endObject() |
| 3543 | + ) |
| 3544 | + .get(); |
| 3545 | + refresh(); |
| 3546 | + SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEBUG")); |
| 3547 | + assertNoFailures(search); |
| 3548 | + assertThat( |
| 3549 | + getHighlighedStringFromSearch(search, "level"), |
| 3550 | + equalTo("<em>DEBUG</em>") |
| 3551 | + ); |
| 3552 | + } |
| 3553 | + |
| 3554 | + public void testConstantKeywordFieldPartialWithWildcardHighlighting() throws IOException { |
| 3555 | + // check that constant_keyword highlighting works |
| 3556 | + XContentBuilder mappings = prepareConstantKeywordMappings("level"); |
| 3557 | + |
| 3558 | + assertAcked(prepareCreate("test").setMapping(mappings)); |
| 3559 | + |
| 3560 | + client().prepareIndex("test") |
| 3561 | + .setId("1") |
| 3562 | + .setSource( |
| 3563 | + jsonBuilder() |
| 3564 | + .startObject() |
| 3565 | + .field("message", "some text") |
| 3566 | + .endObject() |
| 3567 | + ) |
| 3568 | + .get(); |
| 3569 | + refresh(); |
| 3570 | + |
| 3571 | + SearchResponse search = prepareConstantKeywordSearch(QueryBuilders.queryStringQuery("DEB*")); |
| 3572 | + |
| 3573 | + assertNoFailures(search); |
| 3574 | + assertThat( |
| 3575 | + getHighlighedStringFromSearch(search, "level"), |
| 3576 | + equalTo("<em>DEBUG</em>") |
| 3577 | + ); |
| 3578 | + } |
| 3579 | + |
| 3580 | + private XContentBuilder prepareConstantKeywordMappings(String constantKeywordFieldName) throws IOException { |
| 3581 | + XContentBuilder mappings = jsonBuilder(); |
| 3582 | + mappings.startObject(); |
| 3583 | + mappings.startObject("_doc") |
| 3584 | + .startObject("properties") |
| 3585 | + .startObject(constantKeywordFieldName) |
| 3586 | + .field("type", "constant_keyword") |
| 3587 | + .field("value", "DEBUG") |
| 3588 | + .endObject() |
| 3589 | + .startObject("message") |
| 3590 | + .field("type", "text") |
| 3591 | + .endObject() |
| 3592 | + .endObject() |
| 3593 | + .endObject(); |
| 3594 | + mappings.endObject(); |
| 3595 | + return mappings; |
| 3596 | + } |
| 3597 | + |
| 3598 | + private SearchResponse prepareConstantKeywordSearch(QueryBuilder query){ |
| 3599 | + return client().prepareSearch() |
| 3600 | + .setSource( |
| 3601 | + new SearchSourceBuilder().query(query) |
| 3602 | + .highlighter(new HighlightBuilder().field("*")) |
| 3603 | + ) |
| 3604 | + .get(); |
| 3605 | + } |
| 3606 | + |
| 3607 | + private String getHighlighedStringFromSearch(SearchResponse search, String fieldName){ |
| 3608 | + SearchHits hits = search.getHits(); |
| 3609 | + assertEquals("We expect this test search to find exactly one hit", 1, hits.getHits().length); |
| 3610 | + |
| 3611 | + Map<String, HighlightField> highlightFields = hits.getAt(0).getHighlightFields(); |
| 3612 | + assertTrue("We expect to find an highlighted field of name [ %s ]".formatted(fieldName), highlightFields.containsKey(fieldName)); |
| 3613 | + |
| 3614 | + Text[] fragments = highlightFields.get(fieldName).getFragments(); |
| 3615 | + assertEquals("We expect to have exactly one fragment", 1, fragments.length); |
| 3616 | + |
| 3617 | + return fragments[0].toString(); |
| 3618 | + } |
| 3619 | + |
3505 | 3620 | public static class MockAnalysisPlugin extends Plugin implements AnalysisPlugin { |
3506 | 3621 |
|
3507 | 3622 | public final class MockSnowBall extends TokenFilter { |
@@ -3551,45 +3666,5 @@ public Analyzer get() { |
3551 | 3666 | } |
3552 | 3667 | } |
3553 | 3668 |
|
3554 | | - public void testConstantKeywordFieldHighlighting() throws IOException { |
3555 | | - // check that constant_keyword highlighting works |
3556 | | - XContentBuilder mappings = jsonBuilder(); |
3557 | | - mappings.startObject(); |
3558 | | - mappings.startObject("_doc") |
3559 | | - .startObject("properties") |
3560 | | - .startObject("level") |
3561 | | - .field("type", "constant_keyword") |
3562 | | - .field("value", "DEBUG") |
3563 | | - .endObject() |
3564 | | - .startObject("message") |
3565 | | - .field("type", "text") |
3566 | | - .endObject() |
3567 | | - .endObject() |
3568 | | - .endObject(); |
3569 | | - mappings.endObject(); |
3570 | | - |
3571 | | - assertAcked(prepareCreate("test").setMapping(mappings)); |
3572 | | - |
3573 | | - client().prepareIndex("test") |
3574 | | - .setId("1") |
3575 | | - .setSource( |
3576 | | - jsonBuilder().startObject() |
3577 | | - .field("message", "some text") |
3578 | | - .endObject() |
3579 | | - ) |
3580 | | - .get(); |
3581 | | - refresh(); |
3582 | | - SearchResponse search = client().prepareSearch() |
3583 | | - .setSource( |
3584 | | - new SearchSourceBuilder().query(QueryBuilders.termQuery("level", "DEBUG")) |
3585 | | - .highlighter(new HighlightBuilder().field("*")) |
3586 | | - ) |
3587 | | - .get(); |
3588 | | - assertNoFailures(search); |
3589 | | - assertThat( |
3590 | | - search.getHits().getAt(0).getHighlightFields().get("level").getFragments()[0].toString(), |
3591 | | - equalTo("<em>DEBUG</em>") |
3592 | | - ); |
3593 | | - } |
3594 | 3669 |
|
3595 | 3670 | } |
0 commit comments