7373import static org .elasticsearch .index .query .QueryBuilders .boostingQuery ;
7474import static org .elasticsearch .index .query .QueryBuilders .combinedFieldsQuery ;
7575import static org .elasticsearch .index .query .QueryBuilders .constantScoreQuery ;
76+ import static org .elasticsearch .index .query .QueryBuilders .disMaxQuery ;
7677import static org .elasticsearch .index .query .QueryBuilders .existsQuery ;
78+ import static org .elasticsearch .index .query .QueryBuilders .functionScoreQuery ;
7779import static org .elasticsearch .index .query .QueryBuilders .fuzzyQuery ;
7880import static org .elasticsearch .index .query .QueryBuilders .matchPhrasePrefixQuery ;
7981import static org .elasticsearch .index .query .QueryBuilders .matchPhraseQuery ;
@@ -3887,11 +3889,10 @@ public void testConstantKeywordNestedMustQuery() throws Exception {
38873889 assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value</em>" ));
38883890 }
38893891
3890- public void testConstantKeywordNestedFilterQuery () throws Exception {
3892+ public void testConstantKeywordNestedFilterBoolQuery () throws Exception {
38913893 String index = "test" ;
38923894 String constantKeywordFieldName = "test_constant_keyword_field" ;
38933895 String constantValue = "constant_value" ;
3894-
38953896 XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
38963897 prepareCreate (index ).setMapping (mappings ).get ();
38973898
@@ -3908,69 +3909,149 @@ public void testConstantKeywordNestedFilterQuery() throws Exception {
39083909
39093910 public void testDoubleConstantKeywordNestedQueriesBothHighlighted () throws Exception {
39103911 String index = "test" ;
3912+ String firstConstantKeywordFieldName = "test_constant_keyword_field_1" ;
3913+ String firstConstantValue = "constant_value_1" ;
3914+ String secondConstantKeywordFieldName = "test_constant_keyword_field_2" ;
3915+ String secondConstantValue = "constant_value_2" ;
39113916
3912- prepareCreate (index ).setMapping ("""
3913- {
3914- "properties": {
3915- "foo": {
3916- "type": "text"
3917- },
3918- "test_constant_keyword_field_1": {
3919- "type": "constant_keyword",
3920- "value": "constant_value_1"
3921- },
3922- "test_constant_keyword_field_2": {
3923- "type": "constant_keyword",
3924- "value": "constant_value_2"
3925- }
3926- }
3927- }
3928- """ ).get ();
3917+ XContentBuilder mappings = prepareDoubleConstantKeywordMappings (
3918+ firstConstantKeywordFieldName ,
3919+ firstConstantValue ,
3920+ secondConstantKeywordFieldName ,
3921+ secondConstantValue
3922+ );
3923+ prepareCreate (index ).setMapping (mappings ).get ();
39293924
39303925 XContentBuilder document = jsonBuilder ().startObject ().field ("foo" , "bar" ).endObject ();
39313926 saveDocumentIntoIndex (index , "1" , document );
39323927
39333928 SearchResponse search = prepareConstantKeywordSearch (
3934- boolQuery ().filter (matchQuery ("test_constant_keyword_field_1" , "constant_value_1" ))
3935- .should (matchQuery ("test_constant_keyword_field_2" , "constant_value_2" ))
3929+ boolQuery ().filter (matchQuery (firstConstantKeywordFieldName , firstConstantValue ))
3930+ .should (matchQuery (secondConstantKeywordFieldName , secondConstantValue ))
39363931 );
39373932
39383933 assertNoFailures (search );
3939- assertHighlight (search , 0 , "test_constant_keyword_field_1" , 0 , 1 , equalTo ("<em>constant_value_1</em>" ));
3940- assertHighlight (search , 0 , "test_constant_keyword_field_2" , 0 , 1 , equalTo ("<em>constant_value_2</em>" ));
3934+ assertHighlight (search , 0 , firstConstantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value_1</em>" ));
3935+ assertHighlight (search , 0 , secondConstantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value_2</em>" ));
39413936 }
39423937
39433938 public void testDoubleConstantKeywordNestedQueriesJustOneHighlighted () throws Exception {
39443939 String index = "test" ;
3940+ String firstConstantKeywordFieldName = "test_constant_keyword_field_1" ;
3941+ String firstConstantValue = "constant_value_1" ;
3942+ String secondConstantKeywordFieldName = "test_constant_keyword_field_2" ;
3943+ String secondConstantValue = "constant_value_2" ;
39453944
3946- prepareCreate (index ).setMapping ("""
3947- {
3948- "properties": {
3949- "foo": {
3950- "type": "text"
3951- },
3952- "test_constant_keyword_field_1": {
3953- "type": "constant_keyword",
3954- "value": "constant_value_1"
3955- },
3956- "test_constant_keyword_field_2": {
3957- "type": "constant_keyword",
3958- "value": "constant_value_2"
3959- }
3960- }
3961- }
3962- """ ).get ();
3945+ XContentBuilder mappings = prepareDoubleConstantKeywordMappings (
3946+ firstConstantKeywordFieldName ,
3947+ firstConstantValue ,
3948+ secondConstantKeywordFieldName ,
3949+ secondConstantValue
3950+ );
3951+ prepareCreate (index ).setMapping (mappings ).get ();
39633952
39643953 XContentBuilder document = jsonBuilder ().startObject ().field ("foo" , "bar" ).endObject ();
39653954 saveDocumentIntoIndex (index , "1" , document );
39663955
39673956 SearchResponse search = prepareConstantKeywordSearch (
3968- boolQuery ().filter (matchQuery ("test_constant_keyword_field_1" , "constant_value_1" ))
3957+ boolQuery ().filter (matchQuery (firstConstantKeywordFieldName , firstConstantValue ))
39693958 );
39703959
39713960 assertNoFailures (search );
3972- assertHighlight (search , 0 , "test_constant_keyword_field_1" , 0 , 1 , equalTo ("<em>constant_value_1</em>" ));
3973- assertNotHighlighted (search , 0 , "test_constant_keyword_field_2" );
3961+ assertHighlight (search , 0 , firstConstantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value_1</em>" ));
3962+ assertNotHighlighted (search , 0 , secondConstantKeywordFieldName );
3963+ }
3964+
3965+ public void testConstantKeywordNestedBoostingQuery () throws Exception {
3966+ String index = "test" ;
3967+ String firstConstantKeywordFieldName = "test_constant_keyword_field_1" ;
3968+ String firstConstantValue = "constant_value_1" ;
3969+ String secondConstantKeywordFieldName = "test_constant_keyword_field_2" ;
3970+ String secondConstantValue = "constant_value_2" ;
3971+
3972+ XContentBuilder mappings = prepareDoubleConstantKeywordMappings (
3973+ firstConstantKeywordFieldName ,
3974+ firstConstantValue ,
3975+ secondConstantKeywordFieldName ,
3976+ secondConstantValue
3977+ );
3978+ prepareCreate (index ).setMapping (mappings ).get ();
3979+
3980+ XContentBuilder document = jsonBuilder ().startObject ().field ("foo" , "bar" ).endObject ();
3981+ saveDocumentIntoIndex (index , "1" , document );
3982+
3983+ SearchResponse search = prepareConstantKeywordSearch (
3984+ boostingQuery (
3985+ matchQuery (firstConstantKeywordFieldName , firstConstantValue ),
3986+ matchQuery (secondConstantKeywordFieldName , secondConstantValue )
3987+ )
3988+ );
3989+
3990+ assertNoFailures (search );
3991+ assertHighlight (search , 0 , firstConstantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value_1</em>" ));
3992+ assertHighlight (search , 0 , secondConstantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value_2</em>" ));
3993+ }
3994+
3995+ public void testConstantKeywordNestedConstantScoreQuery () throws Exception {
3996+ String index = "test" ;
3997+ String constantKeywordFieldName = "test_constant_keyword_field_1" ;
3998+ String constantValue = "constant_value_1" ;
3999+
4000+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
4001+ prepareCreate (index ).setMapping (mappings ).get ();
4002+
4003+ XContentBuilder document = jsonBuilder ().startObject ().field ("foo" , "bar" ).endObject ();
4004+ saveDocumentIntoIndex (index , "1" , document );
4005+
4006+ SearchResponse search = prepareConstantKeywordSearch (constantScoreQuery (matchQuery (constantKeywordFieldName , constantValue )));
4007+
4008+ assertNoFailures (search );
4009+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value_1</em>" ));
4010+ }
4011+
4012+ public void testConstantKeywordNestedDisMaxQuery () throws Exception {
4013+ String index = "test" ;
4014+ String firstConstantKeywordFieldName = "test_constant_keyword_field_1" ;
4015+ String firstConstantValue = "constant_value_1" ;
4016+ String secondConstantKeywordFieldName = "test_constant_keyword_field_2" ;
4017+ String secondConstantValue = "constant_value_2" ;
4018+
4019+ XContentBuilder mappings = prepareDoubleConstantKeywordMappings (
4020+ firstConstantKeywordFieldName ,
4021+ firstConstantValue ,
4022+ secondConstantKeywordFieldName ,
4023+ secondConstantValue
4024+ );
4025+ prepareCreate (index ).setMapping (mappings ).get ();
4026+
4027+ XContentBuilder document = jsonBuilder ().startObject ().field ("foo" , "bar" ).endObject ();
4028+ saveDocumentIntoIndex (index , "1" , document );
4029+
4030+ SearchResponse search = prepareConstantKeywordSearch (
4031+ disMaxQuery ().add (matchQuery (firstConstantKeywordFieldName , firstConstantValue ))
4032+ .add (matchQuery (secondConstantKeywordFieldName , secondConstantValue ))
4033+ );
4034+
4035+ assertNoFailures (search );
4036+ assertHighlight (search , 0 , firstConstantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value_1</em>" ));
4037+ assertHighlight (search , 0 , secondConstantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value_2</em>" ));
4038+ }
4039+
4040+ public void testConstantKeywordNestedFunctionScoreQuery () throws Exception {
4041+ String index = "test" ;
4042+ String constantKeywordFieldName = "test_constant_keyword_field_1" ;
4043+ String constantValue = "constant_value_1" ;
4044+
4045+ XContentBuilder mappings = prepareConstantKeywordMappings (constantKeywordFieldName , constantValue );
4046+ prepareCreate (index ).setMapping (mappings ).get ();
4047+
4048+ XContentBuilder document = jsonBuilder ().startObject ().field ("foo" , "bar" ).endObject ();
4049+ saveDocumentIntoIndex (index , "1" , document );
4050+
4051+ SearchResponse search = prepareConstantKeywordSearch (functionScoreQuery (matchQuery (constantKeywordFieldName , constantValue )));
4052+
4053+ assertNoFailures (search );
4054+ assertHighlight (search , 0 , constantKeywordFieldName , 0 , 1 , equalTo ("<em>constant_value_1</em>" ));
39744055 }
39754056
39764057 private XContentBuilder prepareConstantKeywordMappings (String constantKeywordFieldName , String constantValue ) throws IOException {
@@ -3991,6 +4072,33 @@ private XContentBuilder prepareConstantKeywordMappings(String constantKeywordFie
39914072 return mappings ;
39924073 }
39934074
4075+ private XContentBuilder prepareDoubleConstantKeywordMappings (
4076+ String firstConstantKeywordFieldName ,
4077+ String firstConstantValue ,
4078+ String secondConstantKeywordFieldName ,
4079+ String secondConstantValue
4080+ ) throws IOException {
4081+ XContentBuilder mappings = jsonBuilder ();
4082+ mappings .startObject ();
4083+ mappings .startObject ("_doc" )
4084+ .startObject ("properties" )
4085+ .startObject (firstConstantKeywordFieldName )
4086+ .field ("type" , "constant_keyword" )
4087+ .field ("value" , firstConstantValue )
4088+ .endObject ()
4089+ .startObject (secondConstantKeywordFieldName )
4090+ .field ("type" , "constant_keyword" )
4091+ .field ("value" , secondConstantValue )
4092+ .endObject ()
4093+ .startObject ("foo" )
4094+ .field ("type" , "text" )
4095+ .endObject ()
4096+ .endObject ()
4097+ .endObject ();
4098+ mappings .endObject ();
4099+ return mappings ;
4100+ }
4101+
39944102 private SearchResponse prepareConstantKeywordSearch (QueryBuilder query ) {
39954103 return client ().prepareSearch ()
39964104 .setSource (new SearchSourceBuilder ().query (query ).highlighter (new HighlightBuilder ().field ("*" )))
0 commit comments