diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchRequest.java index 53a0f587105..ac30479d1ca 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchRequest.java @@ -60,7 +60,7 @@ public class SearchRequest implements ActionRequest { private String[] types = Strings.EMPTY_ARRAY; - private TObjectFloatHashMap queryBoost = EMPTY; + private TObjectFloatHashMap indexBoost = EMPTY; private TimeValue timeout; @@ -191,15 +191,15 @@ public class SearchRequest implements ActionRequest { * Allows to set a dynamic query boost on an index level query. Very handy when, for example, each user has * his own index, and friends matter more than friends of friends. */ - public TObjectFloatHashMap queryBoost() { - return queryBoost; + public TObjectFloatHashMap indexBoost() { + return indexBoost; } - public SearchRequest queryBoost(String index, float queryBoost) { - if (this.queryBoost == EMPTY) { - this.queryBoost = new TObjectFloatHashMap(); + public SearchRequest indexBoost(String index, float indexBoost) { + if (this.indexBoost == EMPTY) { + this.indexBoost = new TObjectFloatHashMap(); } - this.queryBoost.put(index, queryBoost); + this.indexBoost.put(index, indexBoost); return this; } @@ -237,11 +237,11 @@ public class SearchRequest implements ActionRequest { int size = in.readInt(); if (size == 0) { - queryBoost = EMPTY; + indexBoost = EMPTY; } else { - queryBoost = new TObjectFloatHashMap(size); + indexBoost = new TObjectFloatHashMap(size); for (int i = 0; i < size; i++) { - queryBoost.put(in.readUTF(), in.readFloat()); + indexBoost.put(in.readUTF(), in.readFloat()); } } @@ -285,11 +285,11 @@ public class SearchRequest implements ActionRequest { timeout.writeTo(out); } out.writeUTF(source); - if (queryBoost == null) { + if (indexBoost == null) { out.writeInt(0); } else { - out.writeInt(queryBoost.size()); - for (TObjectFloatIterator it = queryBoost.iterator(); it.hasNext();) { + out.writeInt(indexBoost.size()); + for (TObjectFloatIterator it = indexBoost.iterator(); it.hasNext();) { it.advance(); out.writeUTF(it.key()); out.writeFloat(it.value()); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/type/TransportSearchHelper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/type/TransportSearchHelper.java index a2be9d6a311..3eb35dc608b 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/type/TransportSearchHelper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/type/TransportSearchHelper.java @@ -47,9 +47,9 @@ public abstract class TransportSearchHelper { InternalSearchRequest internalRequest = new InternalSearchRequest(shardRouting, request.source()); internalRequest.from(request.from()).size(request.size()); internalRequest.scroll(request.scroll()); - if (request.queryBoost() != null) { - if (request.queryBoost().containsKey(shardRouting.index())) { - internalRequest.queryBoost(request.queryBoost().get(shardRouting.index())); + if (request.indexBoost() != null) { + if (request.indexBoost().containsKey(shardRouting.index())) { + internalRequest.queryBoost(request.indexBoost().get(shardRouting.index())); } } internalRequest.timeout(request.timeout()); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java index 97aa1ecb42c..a9210efe0a3 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java @@ -50,10 +50,13 @@ import static org.elasticsearch.rest.RestResponse.Status.*; */ public class RestSearchAction extends BaseRestHandler { - public final static Pattern fieldsPattern; + private final static Pattern fieldsPattern; + + private final static Pattern indicesBoostPattern; static { fieldsPattern = Pattern.compile(","); + indicesBoostPattern = Pattern.compile(","); } @Inject public RestSearchAction(Settings settings, Client client, RestController controller) { @@ -139,8 +142,23 @@ public class RestSearchAction extends BaseRestHandler { searchRequest.size(Integer.parseInt(size)); } - // TODO query boost per index -// searchRequest.queryBoost(); + String sIndicesBoost = request.param("indicesBoost"); + if (sIndicesBoost != null) { + String[] indicesBoost = indicesBoostPattern.split(sIndicesBoost); + for (String indexBoost : indicesBoost) { + int divisor = indexBoost.indexOf(','); + if (divisor == -1) { + throw new ElasticSearchIllegalArgumentException("Illegal index boost [" + indexBoost + "], no ','"); + } + String indexName = indexBoost.substring(0, divisor); + String sBoost = indexBoost.substring(divisor + 1); + try { + searchRequest.indexBoost(indexName, Float.parseFloat(sBoost)); + } catch (NumberFormatException e) { + throw new ElasticSearchIllegalArgumentException("Illegal index boost [" + indexBoost + "], boost not a float number"); + } + } + } String scroll = request.param("scroll"); if (scroll != null) {