@@ -75,9 +75,12 @@ public class RatedRequest implements Writeable, ToXContentObject {
7575 private final String id ;
7676 private final List <String > summaryFields ;
7777 private final List <RatedDocument > ratedDocs ;
78- // Search request to execute for this rated request. This can be null if template and corresponding parameters are supplied.
78+ /**
79+ * Search request to execute for this rated request. This can be null in
80+ * case the query is supplied as a template with corresponding parameters
81+ */
7982 @ Nullable
80- private SearchSourceBuilder testRequest ;
83+ private final SearchSourceBuilder evaluationRequest ;
8184 /**
8285 * Map of parameters to use for filling a query template, can be used
8386 * instead of providing testRequest.
@@ -86,27 +89,49 @@ public class RatedRequest implements Writeable, ToXContentObject {
8689 @ Nullable
8790 private String templateId ;
8891
89- private RatedRequest (String id , List <RatedDocument > ratedDocs , SearchSourceBuilder testRequest ,
92+ /**
93+ * Create a rated request with template ids and parameters.
94+ *
95+ * @param id a unique name for this rated request
96+ * @param ratedDocs a list of document ratings
97+ * @param params template parameters
98+ * @param templateId a templare id
99+ */
100+ public RatedRequest (String id , List <RatedDocument > ratedDocs , Map <String , Object > params ,
101+ String templateId ) {
102+ this (id , ratedDocs , null , params , templateId );
103+ }
104+
105+ /**
106+ * Create a rated request using a {@link SearchSourceBuilder} to define the
107+ * evaluated query.
108+ *
109+ * @param id a unique name for this rated request
110+ * @param ratedDocs a list of document ratings
111+ * @param evaluatedQuery the query that is evaluated
112+ */
113+ public RatedRequest (String id , List <RatedDocument > ratedDocs , SearchSourceBuilder evaluatedQuery ) {
114+ this (id , ratedDocs , evaluatedQuery , new HashMap <>(), null );
115+ }
116+
117+ private RatedRequest (String id , List <RatedDocument > ratedDocs , SearchSourceBuilder evaluatedQuery ,
90118 Map <String , Object > params , String templateId ) {
91- if (params != null && (params .size () > 0 && testRequest != null )) {
119+ if (params != null && (params .size () > 0 && evaluatedQuery != null )) {
92120 throw new IllegalArgumentException (
93- "Ambiguous rated request: Set both, verbatim test request and test request "
94- + "template parameters." );
121+ "Ambiguous rated request: Set both, verbatim test request and test request " + "template parameters." );
95122 }
96- if (templateId != null && testRequest != null ) {
123+ if (templateId != null && evaluatedQuery != null ) {
97124 throw new IllegalArgumentException (
98- "Ambiguous rated request: Set both, verbatim test request and test request "
99- + "template parameters." );
125+ "Ambiguous rated request: Set both, verbatim test request and test request " + "template parameters." );
100126 }
101- if ((params == null || params .size () < 1 ) && testRequest == null ) {
102- throw new IllegalArgumentException (
103- "Need to set at least test request or test request template parameters." );
127+ if ((params == null || params .size () < 1 ) && evaluatedQuery == null ) {
128+ throw new IllegalArgumentException ("Need to set at least test request or test request template parameters." );
104129 }
105130 if ((params != null && params .size () > 0 ) && templateId == null ) {
106- throw new IllegalArgumentException (
107- "If template parameters are supplied need to set id of template to apply "
108- + "them to too." );
131+ throw new IllegalArgumentException ("If template parameters are supplied need to set id of template to apply " + "them to too." );
109132 }
133+ validateEvaluatedQuery (evaluatedQuery );
134+
110135 // check that not two documents with same _index/id are specified
111136 Set <DocumentKey > docKeys = new HashSet <>();
112137 for (RatedDocument doc : ratedDocs ) {
@@ -118,7 +143,7 @@ private RatedRequest(String id, List<RatedDocument> ratedDocs, SearchSourceBuild
118143 }
119144
120145 this .id = id ;
121- this .testRequest = testRequest ;
146+ this .evaluationRequest = evaluatedQuery ;
122147 this .ratedDocs = new ArrayList <>(ratedDocs );
123148 if (params != null ) {
124149 this .params = new HashMap <>(params );
@@ -129,18 +154,30 @@ private RatedRequest(String id, List<RatedDocument> ratedDocs, SearchSourceBuild
129154 this .summaryFields = new ArrayList <>();
130155 }
131156
132- public RatedRequest (String id , List <RatedDocument > ratedDocs , Map <String , Object > params ,
133- String templateId ) {
134- this (id , ratedDocs , null , params , templateId );
135- }
136-
137- public RatedRequest (String id , List <RatedDocument > ratedDocs , SearchSourceBuilder testRequest ) {
138- this (id , ratedDocs , testRequest , new HashMap <>(), null );
157+ static void validateEvaluatedQuery (SearchSourceBuilder evaluationRequest ) {
158+ // ensure that testRequest, if set, does not contain aggregation, suggest or highlighting section
159+ if (evaluationRequest != null ) {
160+ if (evaluationRequest .suggest () != null ) {
161+ throw new IllegalArgumentException ("Query in rated requests should not contain a suggest section." );
162+ }
163+ if (evaluationRequest .aggregations () != null ) {
164+ throw new IllegalArgumentException ("Query in rated requests should not contain aggregations." );
165+ }
166+ if (evaluationRequest .highlighter () != null ) {
167+ throw new IllegalArgumentException ("Query in rated requests should not contain a highlighter section." );
168+ }
169+ if (evaluationRequest .explain () != null && evaluationRequest .explain ()) {
170+ throw new IllegalArgumentException ("Query in rated requests should not use explain." );
171+ }
172+ if (evaluationRequest .profile ()) {
173+ throw new IllegalArgumentException ("Query in rated requests should not use profile." );
174+ }
175+ }
139176 }
140177
141- public RatedRequest (StreamInput in ) throws IOException {
178+ RatedRequest (StreamInput in ) throws IOException {
142179 this .id = in .readString ();
143- testRequest = in .readOptionalWriteable (SearchSourceBuilder ::new );
180+ evaluationRequest = in .readOptionalWriteable (SearchSourceBuilder ::new );
144181
145182 int intentSize = in .readInt ();
146183 ratedDocs = new ArrayList <>(intentSize );
@@ -159,7 +196,7 @@ public RatedRequest(StreamInput in) throws IOException {
159196 @ Override
160197 public void writeTo (StreamOutput out ) throws IOException {
161198 out .writeString (id );
162- out .writeOptionalWriteable (testRequest );
199+ out .writeOptionalWriteable (evaluationRequest );
163200
164201 out .writeInt (ratedDocs .size ());
165202 for (RatedDocument ratedDoc : ratedDocs ) {
@@ -173,8 +210,8 @@ public void writeTo(StreamOutput out) throws IOException {
173210 out .writeOptionalString (this .templateId );
174211 }
175212
176- public SearchSourceBuilder getTestRequest () {
177- return testRequest ;
213+ public SearchSourceBuilder getEvaluationRequest () {
214+ return evaluationRequest ;
178215 }
179216
180217 /** return the user supplied request id */
@@ -240,8 +277,8 @@ public static RatedRequest fromXContent(XContentParser parser) {
240277 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
241278 builder .startObject ();
242279 builder .field (ID_FIELD .getPreferredName (), this .id );
243- if (testRequest != null ) {
244- builder .field (REQUEST_FIELD .getPreferredName (), this .testRequest );
280+ if (evaluationRequest != null ) {
281+ builder .field (REQUEST_FIELD .getPreferredName (), this .evaluationRequest );
245282 }
246283 builder .startArray (RATINGS_FIELD .getPreferredName ());
247284 for (RatedDocument doc : this .ratedDocs ) {
@@ -285,7 +322,7 @@ public final boolean equals(Object obj) {
285322
286323 RatedRequest other = (RatedRequest ) obj ;
287324
288- return Objects .equals (id , other .id ) && Objects .equals (testRequest , other .testRequest )
325+ return Objects .equals (id , other .id ) && Objects .equals (evaluationRequest , other .evaluationRequest )
289326 && Objects .equals (summaryFields , other .summaryFields )
290327 && Objects .equals (ratedDocs , other .ratedDocs )
291328 && Objects .equals (params , other .params )
@@ -294,7 +331,7 @@ public final boolean equals(Object obj) {
294331
295332 @ Override
296333 public final int hashCode () {
297- return Objects .hash (id , testRequest , summaryFields , ratedDocs , params ,
334+ return Objects .hash (id , evaluationRequest , summaryFields , ratedDocs , params ,
298335 templateId );
299336 }
300337}
0 commit comments