Skip to content

Commit 5e0d998

Browse files
committed
Added shard preference setting to query
Added shard preference setting to query.
1 parent 8d05ffc commit 5e0d998

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

module/app/com/github/cleverage/elasticsearch/IndexQuery.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class IndexQuery<T extends Index> {
5050
private int size = -1;
5151
private boolean explain = false;
5252
private boolean noField = false;
53+
private String preference = null;
5354

5455
public IndexQuery(Class<T> clazz) {
5556
Validate.notNull(clazz, "clazz cannot be null");
@@ -102,6 +103,18 @@ public IndexQuery<T> setExplain(boolean explain) {
102103
return this;
103104
}
104105

106+
/**
107+
* Sets a preference of which shard replicas to execute the search request on.
108+
* @param preference
109+
*
110+
* @return
111+
*/
112+
public IndexQuery<T> setPreference(String preference) {
113+
this.preference = preference;
114+
115+
return this;
116+
}
117+
105118
/**
106119
* Adds a facet
107120
*
@@ -259,6 +272,10 @@ public SearchRequestBuilder getSearchRequestBuilder(IndexQueryPath indexQueryPat
259272
request.setExplain(true);
260273
}
261274

275+
if (preference != null) {
276+
request.setPreference(preference);
277+
}
278+
262279
if (IndexClient.config.showRequest) {
263280
if (StringUtils.isNotBlank(query)) {
264281
Logger.debug("ElasticSearch : Query -> " + query);

module/app/com/github/cleverage/elasticsearch/ScalaHelpers.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ object ScalaHelpers {
209209
* @param size the number of element to retrieve
210210
* @param explain flag used to activate explain
211211
* @param noField flag used to activate the "noField"
212+
* @param preference preference of which shard replicas to execute the search request on
212213
* @tparam T Type into which the results will be converted
213214
*/
214215
case class IndexQuery[T <: Indexable](
@@ -218,7 +219,8 @@ object ScalaHelpers {
218219
val from: Option[Int] = None,
219220
val size: Option[Int] = None,
220221
val explain: Option[Boolean] = None,
221-
val noField: Boolean = false
222+
val noField: Boolean = false,
223+
val preference: Option[String] = None
222224
) {
223225
def withBuilder(builder: QueryBuilder): IndexQuery[T] = copy(builder = builder)
224226
def addFacet(facet: FacetBuilder): IndexQuery[T] = copy(facetBuilders = facet :: facetBuilders)
@@ -227,6 +229,7 @@ object ScalaHelpers {
227229
def withSize(size: Int): IndexQuery[T] = copy(size = Some(size))
228230
def withExplain(explain: Boolean): IndexQuery[T] = copy(explain = Some(explain))
229231
def withNoField(noField: Boolean): IndexQuery[T] = copy(noField = noField)
232+
def withPreference(preference: String): IndexQuery[T] = copy(preference = Some(preference))
230233

231234
/**
232235
* Executes the query
@@ -281,6 +284,9 @@ object ScalaHelpers {
281284
if (noField) {
282285
request.setNoFields()
283286
}
287+
preference.foreach {
288+
request.setPreference(_)
289+
}
284290
request
285291
}
286292
}

0 commit comments

Comments
 (0)