Search API: Set different boost for indices when searching across indices, closes #23.

This commit is contained in:
kimchy 2010-02-17 19:49:46 +02:00
parent ffbc2a9d8d
commit 8a5a44c1c3
3 changed files with 37 additions and 19 deletions

View File

@ -60,7 +60,7 @@ public class SearchRequest implements ActionRequest {
private String[] types = Strings.EMPTY_ARRAY; private String[] types = Strings.EMPTY_ARRAY;
private TObjectFloatHashMap<String> queryBoost = EMPTY; private TObjectFloatHashMap<String> indexBoost = EMPTY;
private TimeValue timeout; 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 * 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. * his own index, and friends matter more than friends of friends.
*/ */
public TObjectFloatHashMap<String> queryBoost() { public TObjectFloatHashMap<String> indexBoost() {
return queryBoost; return indexBoost;
} }
public SearchRequest queryBoost(String index, float queryBoost) { public SearchRequest indexBoost(String index, float indexBoost) {
if (this.queryBoost == EMPTY) { if (this.indexBoost == EMPTY) {
this.queryBoost = new TObjectFloatHashMap<String>(); this.indexBoost = new TObjectFloatHashMap<String>();
} }
this.queryBoost.put(index, queryBoost); this.indexBoost.put(index, indexBoost);
return this; return this;
} }
@ -237,11 +237,11 @@ public class SearchRequest implements ActionRequest {
int size = in.readInt(); int size = in.readInt();
if (size == 0) { if (size == 0) {
queryBoost = EMPTY; indexBoost = EMPTY;
} else { } else {
queryBoost = new TObjectFloatHashMap<String>(size); indexBoost = new TObjectFloatHashMap<String>(size);
for (int i = 0; i < size; i++) { 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); timeout.writeTo(out);
} }
out.writeUTF(source); out.writeUTF(source);
if (queryBoost == null) { if (indexBoost == null) {
out.writeInt(0); out.writeInt(0);
} else { } else {
out.writeInt(queryBoost.size()); out.writeInt(indexBoost.size());
for (TObjectFloatIterator<String> it = queryBoost.iterator(); it.hasNext();) { for (TObjectFloatIterator<String> it = indexBoost.iterator(); it.hasNext();) {
it.advance(); it.advance();
out.writeUTF(it.key()); out.writeUTF(it.key());
out.writeFloat(it.value()); out.writeFloat(it.value());

View File

@ -47,9 +47,9 @@ public abstract class TransportSearchHelper {
InternalSearchRequest internalRequest = new InternalSearchRequest(shardRouting, request.source()); InternalSearchRequest internalRequest = new InternalSearchRequest(shardRouting, request.source());
internalRequest.from(request.from()).size(request.size()); internalRequest.from(request.from()).size(request.size());
internalRequest.scroll(request.scroll()); internalRequest.scroll(request.scroll());
if (request.queryBoost() != null) { if (request.indexBoost() != null) {
if (request.queryBoost().containsKey(shardRouting.index())) { if (request.indexBoost().containsKey(shardRouting.index())) {
internalRequest.queryBoost(request.queryBoost().get(shardRouting.index())); internalRequest.queryBoost(request.indexBoost().get(shardRouting.index()));
} }
} }
internalRequest.timeout(request.timeout()); internalRequest.timeout(request.timeout());

View File

@ -50,10 +50,13 @@ import static org.elasticsearch.rest.RestResponse.Status.*;
*/ */
public class RestSearchAction extends BaseRestHandler { public class RestSearchAction extends BaseRestHandler {
public final static Pattern fieldsPattern; private final static Pattern fieldsPattern;
private final static Pattern indicesBoostPattern;
static { static {
fieldsPattern = Pattern.compile(","); fieldsPattern = Pattern.compile(",");
indicesBoostPattern = Pattern.compile(",");
} }
@Inject public RestSearchAction(Settings settings, Client client, RestController controller) { @Inject public RestSearchAction(Settings settings, Client client, RestController controller) {
@ -139,8 +142,23 @@ public class RestSearchAction extends BaseRestHandler {
searchRequest.size(Integer.parseInt(size)); searchRequest.size(Integer.parseInt(size));
} }
// TODO query boost per index String sIndicesBoost = request.param("indicesBoost");
// searchRequest.queryBoost(); 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"); String scroll = request.param("scroll");
if (scroll != null) { if (scroll != null) {