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 );
@@ -95,7 +90,7 @@ public void testRankEvalRequest() throws IOException {
95
90
assertEquals (7 , hitsAndRatings .size ());
96
91
for (RatedSearchHit hit : hitsAndRatings ) {
97
92
String id = hit .getSearchHit ().getId ();
98
- if (id .equals ("1 " ) || id .equals ("6 " )) {
93
+ if (id .equals ("berlin " ) || id .equals ("amsterdam5 " )) {
99
94
assertFalse (hit .getRating ().isPresent ());
100
95
} else {
101
96
assertEquals (1 , hit .getRating ().get ().intValue ());
@@ -107,15 +102,15 @@ public void testRankEvalRequest() throws IOException {
107
102
assertEquals (7 , hitsAndRatings .size ());
108
103
for (RatedSearchHit hit : hitsAndRatings ) {
109
104
String id = hit .getSearchHit ().getId ();
110
- if (id .equals ("1 " )) {
105
+ if (id .equals ("berlin " )) {
111
106
assertEquals (1 , hit .getRating ().get ().intValue ());
112
107
} else {
113
108
assertFalse (hit .getRating ().isPresent ());
114
109
}
115
110
}
116
111
117
112
// now try this when test2 is closed
118
- client ().performRequest ("POST" , "index2/_close" , Collections . emptyMap ( ));
113
+ client ().performRequest (new Request ( "POST" , "index2/_close" ));
119
114
rankEvalRequest .indicesOptions (IndicesOptions .fromParameters (null , "true" , null , SearchRequest .DEFAULT_INDICES_OPTIONS ));
120
115
response = execute (rankEvalRequest , highLevelClient ()::rankEval , highLevelClient ()::rankEvalAsync ,
121
116
highLevelClient ()::rankEval , highLevelClient ()::rankEvalAsync );
0 commit comments