@@ -3501,8 +3501,7 @@ public void testDisableHighlightIdField() throws Exception {
35013501 }
35023502 }
35033503
3504- public void testConstantKeywordFieldHighlighting () throws IOException {
3505- // check that constant_keyword highlighting works
3504+ public void testConstantKeywordFieldHighlightingQueryString () throws IOException {
35063505 String index = "test" ;
35073506 String constantKeywordFieldName = "test_constant_keyword_field" ;
35083507 String constantValue = "constant_value" ;
@@ -3519,50 +3518,179 @@ public void testConstantKeywordFieldHighlighting() throws IOException {
35193518 SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery (constantValue ));
35203519
35213520 assertNoFailures (search );
3522- assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( constantValue ) ));
3521+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value </em>" ));
35233522 }
35243523
3525- public void testImplicitConstantKeywordFieldHighlighting () throws IOException {
3526- // Constant field value is defined by the mapping
3524+ public void testConstantKeywordFieldNoHighlightingQueryString () throws IOException {
35273525 String index = "test" ;
35283526 String constantKeywordFieldName = "test_constant_keyword_field" ;
35293527 String constantValue = "constant_value" ;
3528+ String queryString = "foobar" ;
35303529
35313530 XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
35323531 assertAcked (prepareCreate (index ).setMapping (mappings ));
35333532
35343533 XContentBuilder document = jsonBuilder ().startObject ().field ("foo" , "bar" ).endObject ();
35353534 saveDocumentIntoIndex (index , "1" , document );
35363535
3537- SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery (constantValue ));
3536+ SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery (queryString ));
35383537
35393538 assertNoFailures (search );
3540- assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ( "<em>%s</em>" . formatted ( constantValue )) );
3539+ assertHitCount (search , 0 );
35413540 }
35423541
3543- public void testConstantKeywordFieldPartialNoHighlighting () throws IOException {
3542+ public void testConstantKeywordFieldHighlightingTerm () throws IOException {
35443543 String index = "test" ;
35453544 String constantKeywordFieldName = "test_constant_keyword_field" ;
35463545 String constantValue = "constant_value" ;
3547- String partialConstantValue = constantValue .substring (0 , 3 );
35483546
35493547 XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
35503548 assertAcked (prepareCreate (index ).setMapping (mappings ));
35513549
3552- XContentBuilder document = jsonBuilder ().startObject ().field ("foo" , "bar" ).endObject ();
3550+ XContentBuilder document = jsonBuilder ().startObject ()
3551+ .field ("foo" , "bar" )
3552+ .field (constantKeywordFieldName , constantValue )
3553+ .endObject ();
35533554 saveDocumentIntoIndex (index , "1" , document );
35543555
3555- SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery (partialConstantValue ));
3556+ SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .termQuery (constantKeywordFieldName , constantValue ));
3557+
3558+ assertNoFailures (search );
3559+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value</em>" ));
3560+ }
3561+
3562+ public void testConstantKeywordFieldNoHighlightingTerm () throws IOException {
3563+ String index = "test" ;
3564+ String constantKeywordFieldName = "test_constant_keyword_field" ;
3565+ String constantValue = "constant_value" ;
3566+ String termValue = "foobar" ;
3567+
3568+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
3569+ assertAcked (prepareCreate (index ).setMapping (mappings ));
3570+
3571+ XContentBuilder document = jsonBuilder ().startObject ()
3572+ .field ("foo" , "bar" )
3573+ .field (constantKeywordFieldName , constantValue )
3574+ .endObject ();
3575+ saveDocumentIntoIndex (index , "1" , document );
3576+
3577+ SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .termQuery (constantKeywordFieldName , termValue ));
35563578
35573579 assertNoFailures (search );
35583580 assertHitCount (search , 0 );
35593581 }
35603582
3583+ public void testConstantKeywordFieldHighlightingWildcard () throws IOException {
3584+ String index = "test" ;
3585+ String constantKeywordFieldName = "test_constant_keyword_field" ;
3586+ String constantValue = "constant_value" ;
3587+ String partialConstantValueWithWildCard = "constant*" ;
3588+
3589+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
3590+ assertAcked (prepareCreate (index ).setMapping (mappings ));
3591+
3592+ XContentBuilder document = jsonBuilder ().startObject ()
3593+ .field ("foo" , "bar" )
3594+ .field (constantKeywordFieldName , constantValue )
3595+ .endObject ();
3596+ saveDocumentIntoIndex (index , "1" , document );
3597+
3598+ SearchResponse search = prepareConstantKeywordSearch (
3599+ QueryBuilders .wildcardQuery (constantKeywordFieldName , partialConstantValueWithWildCard )
3600+ );
3601+
3602+ assertNoFailures (search );
3603+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value</em>" ));
3604+ }
3605+
3606+ public void testConstantKeywordFieldNoHighlightingWildcard () throws IOException {
3607+ String index = "test" ;
3608+ String constantKeywordFieldName = "test_constant_keyword_field" ;
3609+ String constantValue = "constant_value" ;
3610+ String partialConstantValueWithWildCard = "foobar*" ;
3611+
3612+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
3613+ assertAcked (prepareCreate (index ).setMapping (mappings ));
3614+
3615+ XContentBuilder document = jsonBuilder ().startObject ()
3616+ .field ("foo" , "bar" )
3617+ .field (constantKeywordFieldName , constantValue )
3618+ .endObject ();
3619+ saveDocumentIntoIndex (index , "1" , document );
3620+
3621+ SearchResponse search = prepareConstantKeywordSearch (
3622+ QueryBuilders .wildcardQuery (constantKeywordFieldName , partialConstantValueWithWildCard )
3623+ );
3624+
3625+ assertNoFailures (search );
3626+ assertHitCount (search , 0 );
3627+ }
3628+
3629+ public void testConstantKeywordFieldHighlightingPrefix () throws IOException {
3630+ String index = "test" ;
3631+ String constantKeywordFieldName = "test_constant_keyword_field" ;
3632+ String constantValue = "constant_value" ;
3633+ String prefixConstantValue = "constant" ;
3634+
3635+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
3636+ assertAcked (prepareCreate (index ).setMapping (mappings ));
3637+
3638+ XContentBuilder document = jsonBuilder ().startObject ()
3639+ .field ("foo" , "bar" )
3640+ .field (constantKeywordFieldName , constantValue )
3641+ .endObject ();
3642+ saveDocumentIntoIndex (index , "1" , document );
3643+
3644+ SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .prefixQuery (constantKeywordFieldName , prefixConstantValue ));
3645+
3646+ assertNoFailures (search );
3647+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value</em>" ));
3648+ }
3649+
3650+ public void testConstantKeywordFieldNoHighlightingPrefix () throws IOException {
3651+ String index = "test" ;
3652+ String constantKeywordFieldName = "test_constant_keyword_field" ;
3653+ String constantValue = "constant_value" ;
3654+ String prefixConstantValue = "value" ;
3655+
3656+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
3657+ assertAcked (prepareCreate (index ).setMapping (mappings ));
3658+
3659+ XContentBuilder document = jsonBuilder ().startObject ()
3660+ .field ("foo" , "bar" )
3661+ .field (constantKeywordFieldName , constantValue )
3662+ .endObject ();
3663+ saveDocumentIntoIndex (index , "1" , document );
3664+
3665+ SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .prefixQuery (constantKeywordFieldName , prefixConstantValue ));
3666+
3667+ assertNoFailures (search );
3668+ assertHitCount (search , 0 );
3669+ }
3670+
3671+ public void testImplicitConstantKeywordFieldHighlighting () throws IOException {
3672+ // Constant field value is defined by the mapping
3673+ String index = "test" ;
3674+ String constantKeywordFieldName = "test_constant_keyword_field" ;
3675+ String constantValue = "constant_value" ;
3676+
3677+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
3678+ assertAcked (prepareCreate (index ).setMapping (mappings ));
3679+
3680+ XContentBuilder document = jsonBuilder ().startObject ().field ("foo" , "bar" ).endObject ();
3681+ saveDocumentIntoIndex (index , "1" , document );
3682+
3683+ SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery (constantValue ));
3684+
3685+ assertNoFailures (search );
3686+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value</em>" ));
3687+ }
3688+
35613689 public void testConstantKeywordFieldPartialWithWildcardHighlighting () throws IOException {
35623690 String index = "test" ;
35633691 String constantKeywordFieldName = "test_constant_keyword_field" ;
35643692 String constantValue = "constant_value" ;
3565- String partialConstantValueWithWildCard = "%s*" . formatted ( constantValue . substring ( 0 , 3 )) ;
3693+ String partialConstantValueWithWildCard = "constant*" ;
35663694
35673695 XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
35683696 assertAcked (prepareCreate (index ).setMapping (mappings ));
@@ -3573,7 +3701,7 @@ public void testConstantKeywordFieldPartialWithWildcardHighlighting() throws IOE
35733701 SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery (partialConstantValueWithWildCard ));
35743702
35753703 assertNoFailures (search );
3576- assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( constantValue ) ));
3704+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value </em>" ));
35773705 }
35783706
35793707 public void testConstantKeywordFieldNotHighlightedOnOtherFieldMatch () throws IOException {
@@ -3607,8 +3735,8 @@ public void testConstantKeywordFieldAndOtherFieldsMatchHighlighted() throws IOEx
36073735 SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery (constantValue ));
36083736
36093737 assertNoFailures (search );
3610- assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( constantValue ) ));
3611- assertHighlight (search , 0 , "foo" , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( constantValue ) ));
3738+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>bar </em>" ));
3739+ assertHighlight (search , 0 , "foo" , 0 , 1 , equalTo ("<em>bar </em>" ));
36123740
36133741 }
36143742
@@ -3628,8 +3756,8 @@ public void testConstantKeywordMultipleHitsHighlighted() throws IOException {
36283756 SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery (constantValue ));
36293757
36303758 assertNoFailures (search );
3631- assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( constantValue ) ));
3632- assertHighlight (search , 1 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( constantValue ) ));
3759+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value </em>" ));
3760+ assertHighlight (search , 1 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value </em>" ));
36333761 }
36343762
36353763 public void testConstantKeywordAndOtherFieldsMatchMultipleHitsHighlighted () throws IOException {
@@ -3648,10 +3776,10 @@ public void testConstantKeywordAndOtherFieldsMatchMultipleHitsHighlighted() thro
36483776 SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery (constantValue ));
36493777
36503778 assertNoFailures (search );
3651- assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( constantValue ) ));
3652- assertHighlight (search , 0 , "foo" , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( constantValue ) ));
3653- assertHighlight (search , 1 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( constantValue ) ));
3654- assertHighlight (search , 1 , "foo" , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( constantValue ) ));
3779+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value </em>" ));
3780+ assertHighlight (search , 0 , "foo" , 0 , 1 , equalTo ("<em>constant_value </em>" ));
3781+ assertHighlight (search , 1 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value </em>" ));
3782+ assertHighlight (search , 1 , "foo" , 0 , 1 , equalTo ("<em>constant_value </em>" ));
36553783 }
36563784
36573785 public void testDoubleConstantKeywordJustOneHighlighted () throws IOException {
@@ -3689,7 +3817,7 @@ public void testDoubleConstantKeywordJustOneHighlighted() throws IOException {
36893817
36903818 assertNoFailures (search );
36913819 assertEquals (1 , search .getHits ().getHits ()[0 ].getHighlightFields ().size ());
3692- assertHighlight (search , 0 , firstConstantKeywordFieldName , 0 , 1 , equalTo ("<em>%s </em>" . formatted ( firstConstantValue ) ));
3820+ assertHighlight (search , 0 , firstConstantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value_1 </em>" ));
36933821 }
36943822
36953823 private XContentBuilder prepareConstantKeywordMappings (String constantKeywordFieldName , String constantValue ) throws IOException {
0 commit comments