1919
2020package org .elasticsearch .client ;
2121
22- import org .apache .http .entity .ContentType ;
23- import org .apache .http .entity .StringEntity ;
2422import org .elasticsearch .action .search .SearchRequest ;
2523import org .elasticsearch .action .support .IndicesOptions ;
2624import org .elasticsearch .index .query .MatchAllQueryBuilder ;
3735
3836import java .io .IOException ;
3937import java .util .ArrayList ;
40- import java .util .Collections ;
4138import java .util .List ;
4239import java .util .Map ;
4340import java .util .stream .Collectors ;
@@ -49,19 +46,17 @@ public class RankEvalIT extends ESRestHighLevelClientTestCase {
4946
5047 @ Before
5148 public void indexDocuments () throws IOException {
52- StringEntity doc = new StringEntity ("{\" text\" :\" berlin\" }" , ContentType .APPLICATION_JSON );
53- client ().performRequest ("PUT" , "/index/doc/1" , Collections .emptyMap (), doc );
54- doc = new StringEntity ("{\" text\" :\" amsterdam\" }" , ContentType .APPLICATION_JSON );
55- client ().performRequest ("PUT" , "/index/doc/2" , Collections .emptyMap (), doc );
56- client ().performRequest ("PUT" , "/index/doc/3" , Collections .emptyMap (), doc );
57- client ().performRequest ("PUT" , "/index/doc/4" , Collections .emptyMap (), doc );
58- client ().performRequest ("PUT" , "/index/doc/5" , Collections .emptyMap (), doc );
59- client ().performRequest ("PUT" , "/index/doc/6" , Collections .emptyMap (), doc );
60- client ().performRequest ("POST" , "/index/_refresh" );
61-
62- // add another index to test basic multi index support
63- client ().performRequest ("PUT" , "/index2/doc/7" , Collections .emptyMap (), doc );
64- client ().performRequest ("POST" , "/index2/_refresh" );
49+ Request berlin = new Request ("PUT" , "/index/doc/berlin" );
50+ berlin .setJsonEntity ("{\" text\" :\" berlin\" }" );
51+ client ().performRequest (berlin );
52+ for (int i = 0 ; i < 6 ; i ++) {
53+ // add another index to test basic multi index support
54+ String index = i == 0 ? "index2" : "index" ;
55+ Request amsterdam = new Request ("PUT" , "/" + index + "/doc/amsterdam" + i );
56+ amsterdam .setJsonEntity ("{\" text\" :\" amsterdam\" }" );
57+ client ().performRequest (amsterdam );
58+ }
59+ client ().performRequest (new Request ("POST" , "/_refresh" ));
6560 }
6661
6762 /**
@@ -71,10 +66,10 @@ public void indexDocuments() throws IOException {
7166 public void testRankEvalRequest () throws IOException {
7267 SearchSourceBuilder testQuery = new SearchSourceBuilder ();
7368 testQuery .query (new MatchAllQueryBuilder ());
74- List <RatedDocument > amsterdamRatedDocs = createRelevant ("index" , "2 " , "3 " , "4 " , "5 " );
75- amsterdamRatedDocs .addAll (createRelevant ("index2" , "7 " ));
69+ List <RatedDocument > amsterdamRatedDocs = createRelevant ("index" , "amsterdam1 " , "amsterdam2 " , "amsterdam3 " , "amsterdam4 " );
70+ amsterdamRatedDocs .addAll (createRelevant ("index2" , "amsterdam0 " ));
7671 RatedRequest amsterdamRequest = new RatedRequest ("amsterdam_query" , amsterdamRatedDocs , testQuery );
77- RatedRequest berlinRequest = new RatedRequest ("berlin_query" , createRelevant ("index" , "1 " ), testQuery );
72+ RatedRequest berlinRequest = new RatedRequest ("berlin_query" , createRelevant ("index" , "berlin " ), testQuery );
7873 List <RatedRequest > specifications = new ArrayList <>();
7974 specifications .add (amsterdamRequest );
8075 specifications .add (berlinRequest );
@@ -95,7 +90,7 @@ public void testRankEvalRequest() throws IOException {
9590 assertEquals (7 , hitsAndRatings .size ());
9691 for (RatedSearchHit hit : hitsAndRatings ) {
9792 String id = hit .getSearchHit ().getId ();
98- if (id .equals ("1 " ) || id .equals ("6 " )) {
93+ if (id .equals ("berlin " ) || id .equals ("amsterdam5 " )) {
9994 assertFalse (hit .getRating ().isPresent ());
10095 } else {
10196 assertEquals (1 , hit .getRating ().get ().intValue ());
@@ -107,15 +102,15 @@ public void testRankEvalRequest() throws IOException {
107102 assertEquals (7 , hitsAndRatings .size ());
108103 for (RatedSearchHit hit : hitsAndRatings ) {
109104 String id = hit .getSearchHit ().getId ();
110- if (id .equals ("1 " )) {
105+ if (id .equals ("berlin " )) {
111106 assertEquals (1 , hit .getRating ().get ().intValue ());
112107 } else {
113108 assertFalse (hit .getRating ().isPresent ());
114109 }
115110 }
116111
117112 // now try this when test2 is closed
118- client ().performRequest ("POST" , "index2/_close" , Collections . emptyMap ( ));
113+ client ().performRequest (new Request ( "POST" , "index2/_close" ));
119114 rankEvalRequest .indicesOptions (IndicesOptions .fromParameters (null , "true" , null , SearchRequest .DEFAULT_INDICES_OPTIONS ));
120115 response = execute (rankEvalRequest , highLevelClient ()::rankEval , highLevelClient ()::rankEvalAsync ,
121116 highLevelClient ()::rankEval , highLevelClient ()::rankEvalAsync );
0 commit comments