@@ -3503,76 +3503,86 @@ public void testDisableHighlightIdField() throws Exception {
35033503 }
35043504 }
35053505
3506- public void testImplicitConstantKeywordFieldHighlighting () throws IOException {
3507- //Constant field value is defined by the mapping
3508- XContentBuilder mappings = prepareConstantKeywordMappings ("level" );
3506+ public void testConstantKeywordFieldHighlighting () throws IOException {
3507+ // check that constant_keyword highlighting works
3508+ String index = "test" ;
3509+ String constantKeywordFieldName = "level" ;
35093510
3510- assertAcked (prepareCreate ("test" ).setMapping (mappings ));
3511+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName );
3512+ assertAcked (prepareCreate (index ).setMapping (mappings ));
35113513
3512- client ().prepareIndex ("test" )
3513- .setId ("1" )
3514- .setSource (
3515- jsonBuilder ().startObject ()
3516- .field ("message" , "some text" )
3517- .endObject ()
3518- )
3519- .get ();
3520- refresh ();
3514+ XContentBuilder document = jsonBuilder ().startObject ()
3515+ .field ("message" , "some text" )
3516+ .field ("level" , "DEBUG" )
3517+ .endObject ();
3518+ saveDocumentIntoIndex (index , "1" , document );
35213519
35223520 SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery ("DEBUG" ));
3521+
35233522 assertNoFailures (search );
35243523 assertThat (
3525- getHighlighedStringFromSearch (search , "level" ),
3524+ getHighlightedStringFromSearch (search , constantKeywordFieldName ),
35263525 equalTo ("<em>DEBUG</em>" )
35273526 );
35283527 }
35293528
3530- public void testConstantKeywordFieldHighlighting () throws IOException {
3531- // check that constant_keyword highlighting works
3532- XContentBuilder mappings = prepareConstantKeywordMappings ("level" );
3529+ public void testImplicitConstantKeywordFieldHighlighting () throws IOException {
3530+ //Constant field value is defined by the mapping
3531+ String index = "test" ;
3532+ String constantKeywordFieldName = "level" ;
35333533
3534- assertAcked (prepareCreate ("test" ).setMapping (mappings ));
3534+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName );
3535+ assertAcked (prepareCreate (index ).setMapping (mappings ));
3536+
3537+ XContentBuilder document = jsonBuilder ().startObject ()
3538+ .field ("message" , "some text" )
3539+ .endObject ();
3540+ saveDocumentIntoIndex (index , "1" , document );
35353541
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 ();
35463542 SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery ("DEBUG" ));
3543+
35473544 assertNoFailures (search );
35483545 assertThat (
3549- getHighlighedStringFromSearch (search , "level" ),
3546+ getHighlightedStringFromSearch (search , constantKeywordFieldName ),
35503547 equalTo ("<em>DEBUG</em>" )
35513548 );
35523549 }
35533550
3551+ public void testConstantKeywordFieldPartialNoHighlighting () throws IOException {
3552+ String index = "test" ;
3553+ String constantKeywordFieldName = "level" ;
3554+
3555+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName );
3556+ assertAcked (prepareCreate (index ).setMapping (mappings ));
3557+
3558+ XContentBuilder document = jsonBuilder ().startObject ()
3559+ .field ("message" , "some text" )
3560+ .endObject ();
3561+ saveDocumentIntoIndex (index , "1" , document );
3562+
3563+ SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery ("DEB" ));
3564+
3565+ assertNoFailures (search );
3566+ assertEquals (0 , search .getHits ().getHits ().length );
3567+ }
3568+
35543569 public void testConstantKeywordFieldPartialWithWildcardHighlighting () throws IOException {
35553570 // check that constant_keyword highlighting works
35563571 XContentBuilder mappings = prepareConstantKeywordMappings ("level" );
35573572
35583573 assertAcked (prepareCreate ("test" ).setMapping (mappings ));
35593574
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 ();
3575+ XContentBuilder document = jsonBuilder ().startObject ()
3576+ .field ("message" , "some text" )
3577+ .endObject ();
3578+
3579+ saveDocumentIntoIndex ("test" , "1" , document );
35703580
35713581 SearchResponse search = prepareConstantKeywordSearch (QueryBuilders .queryStringQuery ("DEB*" ));
35723582
35733583 assertNoFailures (search );
35743584 assertThat (
3575- getHighlighedStringFromSearch (search , "level" ),
3585+ getHighlightedStringFromSearch (search , "level" ),
35763586 equalTo ("<em>DEBUG</em>" )
35773587 );
35783588 }
@@ -3604,7 +3614,17 @@ private SearchResponse prepareConstantKeywordSearch(QueryBuilder query){
36043614 .get ();
36053615 }
36063616
3607- private String getHighlighedStringFromSearch (SearchResponse search , String fieldName ){
3617+ private void saveDocumentIntoIndex (String index , String id , XContentBuilder document ){
3618+ client ().prepareIndex (index )
3619+ .setId (id )
3620+ .setSource (
3621+ document
3622+ )
3623+ .get ();
3624+ refresh ();
3625+ }
3626+
3627+ private String getHighlightedStringFromSearch (SearchResponse search , String fieldName ){
36083628 SearchHits hits = search .getHits ();
36093629 assertEquals ("We expect this test search to find exactly one hit" , 1 , hits .getHits ().length );
36103630
0 commit comments