19
19
20
20
package org .elasticsearch .client ;
21
21
22
- import org .apache .http .entity .ContentType ;
23
- import org .apache .http .entity .StringEntity ;
24
22
import org .elasticsearch .action .search .SearchRequest ;
25
23
import org .elasticsearch .action .support .IndicesOptions ;
26
24
import org .elasticsearch .index .query .MatchAllQueryBuilder ;
37
35
38
36
import java .io .IOException ;
39
37
import java .util .ArrayList ;
40
- import java .util .Collections ;
41
38
import java .util .List ;
42
39
import java .util .Map ;
43
40
import java .util .stream .Collectors ;
@@ -49,19 +46,17 @@ public class RankEvalIT extends ESRestHighLevelClientTestCase {
49
46
50
47
@ Before
51
48
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" ));
65
60
}
66
61
67
62
/**
@@ -71,10 +66,10 @@ public void indexDocuments() throws IOException {
71
66
public void testRankEvalRequest () throws IOException {
72
67
SearchSourceBuilder testQuery = new SearchSourceBuilder ();
73
68
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 " ));
76
71
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 );
78
73
List <RatedRequest > specifications = new ArrayList <>();
79
74
specifications .add (amsterdamRequest );
80
75
specifications .add (berlinRequest );
@@ -94,7 +89,7 @@ public void testRankEvalRequest() throws IOException {
94
89
assertEquals (7 , hitsAndRatings .size ());
95
90
for (RatedSearchHit hit : hitsAndRatings ) {
96
91
String id = hit .getSearchHit ().getId ();
97
- if (id .equals ("1 " ) || id .equals ("6 " )) {
92
+ if (id .equals ("berlin " ) || id .equals ("amsterdam5 " )) {
98
93
assertFalse (hit .getRating ().isPresent ());
99
94
} else {
100
95
assertEquals (1 , hit .getRating ().get ().intValue ());
@@ -106,15 +101,15 @@ public void testRankEvalRequest() throws IOException {
106
101
assertEquals (7 , hitsAndRatings .size ());
107
102
for (RatedSearchHit hit : hitsAndRatings ) {
108
103
String id = hit .getSearchHit ().getId ();
109
- if (id .equals ("1 " )) {
104
+ if (id .equals ("berlin " )) {
110
105
assertEquals (1 , hit .getRating ().get ().intValue ());
111
106
} else {
112
107
assertFalse (hit .getRating ().isPresent ());
113
108
}
114
109
}
115
110
116
111
// now try this when test2 is closed
117
- client ().performRequest ("POST" , "index2/_close" , Collections . emptyMap ( ));
112
+ client ().performRequest (new Request ( "POST" , "index2/_close" ));
118
113
rankEvalRequest .indicesOptions (IndicesOptions .fromParameters (null , "true" , null , SearchRequest .DEFAULT_INDICES_OPTIONS ));
119
114
response = execute (rankEvalRequest , highLevelClient ()::rankEval , highLevelClient ()::rankEvalAsync );
120
115
}
0 commit comments