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 TObjectFloatHashMap<String> queryBoost = EMPTY;
private TObjectFloatHashMap<String> 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<String> queryBoost() {
return queryBoost;
public TObjectFloatHashMap<String> indexBoost() {
return indexBoost;
}
public SearchRequest queryBoost(String index, float queryBoost) {
if (this.queryBoost == EMPTY) {
this.queryBoost = new TObjectFloatHashMap<String>();
public SearchRequest indexBoost(String index, float indexBoost) {
if (this.indexBoost == EMPTY) {
this.indexBoost = new TObjectFloatHashMap<String>();
}
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<String>(size);
indexBoost = new TObjectFloatHashMap<String>(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<String> it = queryBoost.iterator(); it.hasNext();) {
out.writeInt(indexBoost.size());
for (TObjectFloatIterator<String> it = indexBoost.iterator(); it.hasNext();) {
it.advance();
out.writeUTF(it.key());
out.writeFloat(it.value());

View File

@ -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());

View File

@ -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) {