Improved alias support in the percolate api

* Changed the response to include the alias as part of each match.
* Added `percolate_format=ids` query string option to just serialize the ids in the rest response.
* Added support for multiple indices in the percolate api.

Closes #3420
This commit is contained in:
Martijn van Groningen 2013-08-01 10:42:14 +02:00
parent 5cd01461f6
commit 56227cc141
13 changed files with 365 additions and 183 deletions

View File

@ -61,8 +61,8 @@ public class PercolateRequest extends BroadcastOperationRequest<PercolateRequest
PercolateRequest() {
}
public PercolateRequest(String index, String documentType) {
super(new String[]{index});
public PercolateRequest(String[] indices, String documentType) {
super(indices);
this.documentType = documentType;
}

View File

@ -40,22 +40,10 @@ public class PercolateRequestBuilder extends BroadcastOperationRequestBuilder<Pe
private PercolateSourceBuilder sourceBuilder;
public PercolateRequestBuilder(Client client, String index, String type) {
super((InternalClient) client, new PercolateRequest(index, type));
}
PercolateRequestBuilder(Client client) {
public PercolateRequestBuilder(Client client) {
super((InternalClient) client, new PercolateRequest());
}
/**
* Sets the index to percolate the document against.
*/
public PercolateRequestBuilder setIndex(String index) {
request.indices(index);
return this;
}
/**
* Sets the type of the document to percolate.
*/

View File

@ -23,6 +23,7 @@ import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
@ -34,12 +35,12 @@ import java.util.List;
/**
*
*/
public class PercolateResponse extends BroadcastOperationResponse implements Iterable<Text> {
public class PercolateResponse extends BroadcastOperationResponse implements Iterable<PercolateResponse.Match> {
private long tookInMillis;
private Text[] matches;
private Match[] matches;
public PercolateResponse(int totalShards, int successfulShards, int failedShards, List<ShardOperationFailedException> shardFailures, Text[] matches, long tookInMillis) {
public PercolateResponse(int totalShards, int successfulShards, int failedShards, List<ShardOperationFailedException> shardFailures, Match[] matches, long tookInMillis) {
super(totalShards, successfulShards, failedShards, shardFailures);
this.tookInMillis = tookInMillis;
this.matches = matches;
@ -53,7 +54,7 @@ public class PercolateResponse extends BroadcastOperationResponse implements Ite
PercolateResponse() {
}
public PercolateResponse(Text[] matches) {
public PercolateResponse(Match[] matches) {
this.matches = matches;
}
@ -71,12 +72,12 @@ public class PercolateResponse extends BroadcastOperationResponse implements Ite
return tookInMillis;
}
public Text[] getMatches() {
public Match[] getMatches() {
return this.matches;
}
@Override
public Iterator<Text> iterator() {
public Iterator<Match> iterator() {
return Arrays.asList(matches).iterator();
}
@ -84,13 +85,63 @@ public class PercolateResponse extends BroadcastOperationResponse implements Ite
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
tookInMillis = in.readVLong();
matches = in.readTextArray();
int size = in.readVInt();
matches = new Match[size];
for (int i = 0; i < size; i++) {
matches[i] = new Match();
matches[i].readFrom(in);
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVLong(tookInMillis);
out.writeTextArray(matches);
out.writeVInt(matches.length);
for (Match match : matches) {
match.writeTo(out);
}
}
public static class Match implements Streamable {
private Text id;
private Text index;
public Match(Text id, Text index) {
this.id = id;
this.index = index;
}
Match() {
}
public Text id() {
return id;
}
public Text index() {
return index;
}
public Text getId() {
return id;
}
public Text getIndex() {
return index;
}
@Override
public void readFrom(StreamInput in) throws IOException {
id = in.readText();
index = in.readText();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeText(id);
out.writeText(index);
}
}
}

View File

@ -36,6 +36,7 @@ import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.StringText;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.engine.DocumentMissingException;
import org.elasticsearch.index.shard.ShardId;
@ -44,6 +45,7 @@ import org.elasticsearch.percolator.PercolatorService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -124,7 +126,7 @@ public class TransportPercolateAction extends TransportBroadcastOperationAction<
int successfulShards = 0;
int failedShards = 0;
List<Text[]> shardResults = null;
List<PercolateShardResponse> shardResults = null;
List<ShardOperationFailedException> shardFailures = null;
for (int i = 0; i < shardsResponses.length(); i++) {
@ -142,7 +144,7 @@ public class TransportPercolateAction extends TransportBroadcastOperationAction<
if (shardResults == null) {
shardResults = newArrayList();
}
shardResults.add(percolateShardResponse.matches());
shardResults.add(percolateShardResponse);
successfulShards++;
}
}
@ -153,17 +155,21 @@ public class TransportPercolateAction extends TransportBroadcastOperationAction<
}
int size = 0;
for (Text[] shardResult : shardResults) {
size += shardResult.length;
for (PercolateShardResponse response : shardResults) {
size += response.matches().length;
}
Text[] finalMatches = new Text[size];
int offset = 0;
for (Text[] shardResult : shardResults) {
System.arraycopy(shardResult, 0, finalMatches, offset, shardResult.length);
offset += shardResult.length;
List<PercolateResponse.Match> finalMatches = new ArrayList<PercolateResponse.Match>(size);
for (PercolateShardResponse response : shardResults) {
Text index = new StringText(response.getIndex());
for (Text id : response.matches()) {
finalMatches.add(new PercolateResponse.Match(id, index));
}
}
assert size == offset;
return new PercolateResponse(shardsResponses.length(), successfulShards, failedShards, shardFailures, finalMatches, tookInMillis);
return new PercolateResponse(
shardsResponses.length(), successfulShards, failedShards, shardFailures, finalMatches.toArray(new PercolateResponse.Match[size]), tookInMillis
);
}
@Override

View File

@ -41,9 +41,6 @@ import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.mlt.MoreLikeThisRequest;
import org.elasticsearch.action.mlt.MoreLikeThisRequestBuilder;
import org.elasticsearch.action.termvector.TermVectorRequest;
import org.elasticsearch.action.termvector.TermVectorRequestBuilder;
import org.elasticsearch.action.termvector.TermVectorResponse;
import org.elasticsearch.action.percolate.PercolateRequest;
import org.elasticsearch.action.percolate.PercolateRequestBuilder;
import org.elasticsearch.action.percolate.PercolateResponse;
@ -51,6 +48,9 @@ import org.elasticsearch.action.search.*;
import org.elasticsearch.action.suggest.SuggestRequest;
import org.elasticsearch.action.suggest.SuggestRequestBuilder;
import org.elasticsearch.action.suggest.SuggestResponse;
import org.elasticsearch.action.termvector.TermVectorRequest;
import org.elasticsearch.action.termvector.TermVectorRequestBuilder;
import org.elasticsearch.action.termvector.TermVectorResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
@ -485,11 +485,8 @@ public interface Client {
/**
* Percolates a request returning the matches documents.
*
* @param index The index to percolate the doc
* @param type The type of the doc
*/
PercolateRequestBuilder preparePercolate(String index, String type);
PercolateRequestBuilder preparePercolate();
/**
* Computes a score explanation for the specified request.

View File

@ -48,10 +48,6 @@ import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.mlt.MoreLikeThisAction;
import org.elasticsearch.action.mlt.MoreLikeThisRequest;
import org.elasticsearch.action.mlt.MoreLikeThisRequestBuilder;
import org.elasticsearch.action.termvector.TermVectorAction;
import org.elasticsearch.action.termvector.TermVectorRequest;
import org.elasticsearch.action.termvector.TermVectorRequestBuilder;
import org.elasticsearch.action.termvector.TermVectorResponse;
import org.elasticsearch.action.percolate.PercolateAction;
import org.elasticsearch.action.percolate.PercolateRequest;
import org.elasticsearch.action.percolate.PercolateRequestBuilder;
@ -61,6 +57,10 @@ import org.elasticsearch.action.suggest.SuggestAction;
import org.elasticsearch.action.suggest.SuggestRequest;
import org.elasticsearch.action.suggest.SuggestRequestBuilder;
import org.elasticsearch.action.suggest.SuggestResponse;
import org.elasticsearch.action.termvector.TermVectorAction;
import org.elasticsearch.action.termvector.TermVectorRequest;
import org.elasticsearch.action.termvector.TermVectorRequestBuilder;
import org.elasticsearch.action.termvector.TermVectorResponse;
import org.elasticsearch.action.update.UpdateAction;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
@ -324,8 +324,8 @@ public abstract class AbstractClient implements InternalClient {
}
@Override
public PercolateRequestBuilder preparePercolate(String index, String type) {
return new PercolateRequestBuilder(this, index, type);
public PercolateRequestBuilder preparePercolate() {
return new PercolateRequestBuilder(this);
}
@Override

View File

@ -24,10 +24,10 @@ import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.percolate.PercolateRequest;
import org.elasticsearch.action.percolate.PercolateResponse;
import org.elasticsearch.action.support.IgnoreIndices;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.index.VersionType;
@ -57,16 +57,19 @@ public class RestPercolateAction extends BaseRestHandler {
@Override
public void handleRequest(RestRequest restRequest, RestChannel restChannel) {
String index = restRequest.param("index");
String[] indices = RestActions.splitIndices(restRequest.param("index"));
String type = restRequest.param("type");
PercolateRequest percolateRequest = new PercolateRequest(index, type);
PercolateRequest percolateRequest = new PercolateRequest(indices, type);
percolateRequest.routing(restRequest.param("routing"));
percolateRequest.preference(restRequest.param("preference"));
percolateRequest.source(restRequest.content(), restRequest.contentUnsafe());
percolateRequest.routing(restRequest.param("routing"));
percolateRequest.preference(restRequest.param("preference"));
if (restRequest.hasParam("ignore_indices")) {
percolateRequest.ignoreIndices(IgnoreIndices.fromString(restRequest.param("ignore_indices")));
}
executePercolate(percolateRequest, restRequest, restChannel);
}
@ -100,8 +103,18 @@ public class RestPercolateAction extends BaseRestHandler {
builder.endObject();
builder.startArray(Fields.MATCHES);
for (Text match : response) {
builder.value(match);
boolean justIds = "ids".equals(restRequest.param("percolate_format"));
if (justIds) {
for (PercolateResponse.Match match : response) {
builder.value(match.id());
}
} else {
for (PercolateResponse.Match match : response) {
builder.startObject();
builder.field(Fields._INDEX, match.getIndex());
builder.field(Fields._ID, match.getId());
builder.endObject();
}
}
builder.endArray();
@ -141,7 +154,7 @@ public class RestPercolateAction extends BaseRestHandler {
getRequest.versionType(VersionType.fromString(restRequest.param("version_type"), getRequest.versionType()));
PercolateRequest percolateRequest = new PercolateRequest(
restRequest.param("percolate_index", index),
RestActions.splitIndices(restRequest.param("percolate_index", index)),
restRequest.param("percolate_type", type)
);
percolateRequest.getRequest(getRequest);
@ -152,6 +165,9 @@ public class RestPercolateAction extends BaseRestHandler {
percolateRequest.routing(restRequest.param("percolate_routing"));
percolateRequest.preference(restRequest.param("percolate_preference"));
if (restRequest.hasParam("ignore_indices")) {
percolateRequest.ignoreIndices(IgnoreIndices.fromString(restRequest.param("ignore_indices")));
}
executePercolate(percolateRequest, restRequest, restChannel);
}
@ -169,5 +185,7 @@ public class RestPercolateAction extends BaseRestHandler {
static final XContentBuilderString REASON = new XContentBuilderString("reason");
static final XContentBuilderString TOOK = new XContentBuilderString("took");
static final XContentBuilderString MATCHES = new XContentBuilderString("matches");
static final XContentBuilderString _INDEX = new XContentBuilderString("_index");
static final XContentBuilderString _ID = new XContentBuilderString("_id");
}
}

View File

@ -113,7 +113,8 @@ public class PercolatorStressBenchmark {
source = source(Integer.toString(i), number);
expectedMatches = 1;
}
PercolateResponse percolate = client.preparePercolate("test", "type1")
PercolateResponse percolate = client.preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(source)
.execute().actionGet();
if (percolate.getMatches().length != expectedMatches) {

View File

@ -88,7 +88,9 @@ public class NoMasterNodeTests extends AbstractNodesTests {
try {
PercolateSourceBuilder percolateSource = new PercolateSourceBuilder();
percolateSource.percolateDocument().setDoc(new HashMap());
node.client().preparePercolate("test", "type1").setSource(percolateSource).execute().actionGet();
node.client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(percolateSource).execute().actionGet();
assert false;
} catch (ClusterBlockException e) {
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));

View File

@ -133,20 +133,23 @@ public class ConcurrentPercolatorTests extends AbstractNodesTests {
}
PercolateResponse percolate;
if (count % 3 == 0) {
percolate = client().preparePercolate("index", "type").setSource(bothFields)
percolate = client().preparePercolate().setIndices("index").setDocumentType("type")
.setSource(bothFields)
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(2));
assertThat(convertFromTextArray(percolate.getMatches()), arrayContainingInAnyOrder("test1", "test2"));
assertThat(convertFromTextArray(percolate.getMatches(), "index"), arrayContainingInAnyOrder("test1", "test2"));
} else if (count % 3 == 1) {
percolate = client().preparePercolate("index", "type").setSource(onlyField2)
percolate = client().preparePercolate().setIndices("index").setDocumentType("type")
.setSource(onlyField2)
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(percolate.getMatches()), arrayContaining("test1"));
assertThat(convertFromTextArray(percolate.getMatches(), "index"), arrayContaining("test1"));
} else {
percolate = client().preparePercolate("index", "type").setSource(onlyField1)
percolate = client().preparePercolate().setIndices("index").setDocumentType("type")
.setSource(onlyField1)
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(percolate.getMatches()), arrayContaining("test2"));
assertThat(convertFromTextArray(percolate.getMatches(), "index"), arrayContaining("test2"));
}
}
@ -272,7 +275,7 @@ public class ConcurrentPercolatorTests extends AbstractNodesTests {
switch (x) {
case 0:
atLeastExpected = type1.get();
response = client().preparePercolate("index", "type")
response = client().preparePercolate().setIndices("index").setDocumentType("type")
.setSource(onlyField1Doc).execute().actionGet();
assertThat(response.getShardFailures(), emptyArray());
assertThat(response.getSuccessfulShards(), equalTo(response.getTotalShards()));
@ -280,7 +283,7 @@ public class ConcurrentPercolatorTests extends AbstractNodesTests {
break;
case 1:
atLeastExpected = type2.get();
response = client().preparePercolate("index", "type")
response = client().preparePercolate().setIndices("index").setDocumentType("type")
.setSource(onlyField2Doc).execute().actionGet();
assertThat(response.getShardFailures(), emptyArray());
assertThat(response.getSuccessfulShards(), equalTo(response.getTotalShards()));
@ -288,7 +291,7 @@ public class ConcurrentPercolatorTests extends AbstractNodesTests {
break;
case 2:
atLeastExpected = type3.get();
response = client().preparePercolate("index", "type")
response = client().preparePercolate().setIndices("index").setDocumentType("type")
.setSource(field1AndField2Doc).execute().actionGet();
assertThat(response.getShardFailures(), emptyArray());
assertThat(response.getSuccessfulShards(), equalTo(response.getTotalShards()));
@ -398,7 +401,7 @@ public class ConcurrentPercolatorTests extends AbstractNodesTests {
break;
}
int atLeastExpected = liveIds.size();
PercolateResponse response = client().preparePercolate("index", "type")
PercolateResponse response = client().preparePercolate().setIndices("index").setDocumentType("type")
.setSource(percolateDoc).execute().actionGet();
assertThat(response.getShardFailures(), emptyArray());
assertThat(response.getSuccessfulShards(), equalTo(response.getTotalShards()));

View File

@ -84,7 +84,9 @@ public class RecoveryPercolatorTests extends AbstractNodesTests {
.setRefresh(true)
.execute().actionGet();
PercolateResponse percolate = client.preparePercolate("test", "type1").setSource(jsonBuilder().startObject().startObject("doc")
PercolateResponse percolate = client.preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc")
.field("field1", "value1")
.endObject().endObject())
.execute().actionGet();
@ -102,7 +104,9 @@ public class RecoveryPercolatorTests extends AbstractNodesTests {
assertThat(clusterHealth.isTimedOut(), equalTo(false));
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
percolate = client.preparePercolate("test", "type1").setSource(jsonBuilder().startObject().startObject("doc")
percolate = client.preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc")
.field("field1", "value1")
.endObject().endObject())
.execute().actionGet();
@ -133,7 +137,9 @@ public class RecoveryPercolatorTests extends AbstractNodesTests {
assertThat(client.prepareCount().setTypes("_percolator").setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(1l));
PercolateResponse percolate = client.preparePercolate("test", "type1").setSource(jsonBuilder().startObject().startObject("doc")
PercolateResponse percolate = client.preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc")
.field("field1", "value1")
.endObject().endObject())
.execute().actionGet();
@ -162,7 +168,9 @@ public class RecoveryPercolatorTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
assertThat(client.prepareCount().setTypes("_percolator").setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(0l));
percolate = client.preparePercolate("test", "type1").setSource(jsonBuilder().startObject().startObject("doc")
percolate = client.preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc")
.field("field1", "value1")
.endObject().endObject())
.execute().actionGet();
@ -179,7 +187,9 @@ public class RecoveryPercolatorTests extends AbstractNodesTests {
assertThat(client.prepareCount().setTypes("_percolator").setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(1l));
percolate = client.preparePercolate("test", "type1").setSource(jsonBuilder().startObject().startObject("doc")
percolate = client.preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc")
.field("field1", "value1")
.endObject().endObject())
.execute().actionGet();
@ -220,11 +230,12 @@ public class RecoveryPercolatorTests extends AbstractNodesTests {
}
logger.info("--> Percolate doc with field1=95");
PercolateResponse response = client.preparePercolate("test", "type1")
PercolateResponse response = client.preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", 95).endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(6));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("95", "96", "97", "98", "99", "100"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("95", "96", "97", "98", "99", "100"));
logger.info("--> Close and open index to trigger percolate queries loading...");
client.admin().indices().prepareClose("test").execute().actionGet();
@ -233,10 +244,11 @@ public class RecoveryPercolatorTests extends AbstractNodesTests {
ensureGreen(client);
logger.info("--> Percolate doc with field1=100");
response = client.preparePercolate("test", "type1")
response = client.preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", 100).endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(response.getMatches()[0].string(), equalTo("100"));
assertThat(response.getMatches()[0].id().string(), equalTo("100"));
}
}

View File

@ -21,15 +21,17 @@ package org.elasticsearch.test.integration.percolator;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.percolate.PercolateResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IgnoreIndices;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.ImmutableSettings.Builder;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.engine.DocumentMissingException;
@ -82,42 +84,47 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
client().admin().indices().prepareRefresh("test").execute().actionGet();
logger.info("--> Percolate doc with field1=b");
PercolateResponse response = client().preparePercolate("test", "type")
PercolateResponse response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "b").endObject()))
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(2));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("1", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("1", "4"));
logger.info("--> Percolate doc with field1=c");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setPercolateDoc(docBuilder().setDoc(yamlBuilder().startObject().field("field1", "c").endObject()))
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(2));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("2", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("2", "4"));
logger.info("--> Percolate doc with field1=b c");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setPercolateDoc(docBuilder().setDoc(smileBuilder().startObject().field("field1", "b c").endObject()))
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(4));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("1", "2", "3", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("1", "2", "3", "4"));
logger.info("--> Percolate doc with field1=d");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "d").endObject()))
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(response.getMatches()), arrayContaining("4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContaining("4"));
logger.info("--> Search dummy doc, percolate queries must not be included");
SearchResponse searchResponse = client().prepareSearch("test").execute().actionGet();
SearchResponse searchResponse = client().prepareSearch("test", "test").execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
assertThat(searchResponse.getHits().getAt(0).type(), equalTo("type"));
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1"));
logger.info("--> Percolate non existing doc");
try {
client().preparePercolate("test", "type")
client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("5"))
.execute().actionGet();
fail("Exception should have been thrown");
@ -152,7 +159,8 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.field("field2", "value")
.endObject().endObject().endObject();
PercolateResponse response = client().preparePercolate("index", "type1").setSource(doc)
PercolateResponse response = client().preparePercolate().setSource(doc)
.setIndices("test").setDocumentType("type1")
.execute().actionGet();
assertThat(response.getMatches(), emptyArray());
@ -161,30 +169,37 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setSource(XContentFactory.jsonBuilder().startObject().field("query", termQuery("field2", "value")).endObject())
.execute().actionGet();
response = client().preparePercolate("test", "type1").setSource(doc).execute().actionGet();
response = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(doc).execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(response.getMatches()), arrayContaining("test1"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContaining("test1"));
response = client().preparePercolate("test", "type1").setSource(docWithType).execute().actionGet();
response = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(docWithType).execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(response.getMatches()), arrayContaining("test1"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContaining("test1"));
// add second query...
client().prepareIndex("test", "_percolator", "test2")
.setSource(XContentFactory.jsonBuilder().startObject().field("query", termQuery("field1", 1)).endObject())
.execute().actionGet();
response = client().preparePercolate("test", "type1")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(doc)
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(2));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("test1", "test2"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("test1", "test2"));
client().prepareDelete("test", "_percolator", "test2").execute().actionGet();
response = client().preparePercolate("test", "type1").setSource(doc).execute().actionGet();
response = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(doc).execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(response.getMatches()), arrayContaining("test1"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContaining("test1"));
// add a range query (cached)
// add a query
@ -196,9 +211,11 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
)
.execute().actionGet();
response = client().preparePercolate("test", "type1").setSource(doc).execute().actionGet();
response = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(doc).execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(response.getMatches()), arrayContaining("test1"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContaining("test1"));
}
@Test
@ -217,20 +234,23 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
}
logger.info("--> Percolate doc with no routing");
PercolateResponse response = client().preparePercolate("test", "type")
PercolateResponse response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value").endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(100));
logger.info("--> Percolate doc with routing=0");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value").endObject().endObject())
.setRouting("0")
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(50));
logger.info("--> Percolate doc with routing=1");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value").endObject().endObject())
.setRouting("1")
.execute().actionGet();
@ -297,38 +317,15 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setRefresh(true)
.execute().actionGet();
PercolateResponse percolate = client().preparePercolate("test", "doc").setSource(jsonBuilder().startObject()
.startObject("doc").field("filingcategory", "s").endObject()
.field("query", termQuery("source", "productizer"))
.endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
}
@Test
public void registerPercolatorAndThenCreateAnIndex() throws Exception {
logger.info("--> register a query");
client().prepareIndex("my-percolate-index", "_percolator", "kuku")
PercolateResponse percolate = client().preparePercolate()
.setIndices("test").setDocumentType("doc")
.setSource(jsonBuilder().startObject()
.field("color", "blue")
.field("query", termQuery("field1", "value1"))
.startObject("doc").field("filingcategory", "s").endObject()
.field("query", termQuery("source", "productizer"))
.endObject())
.setRefresh(true)
.execute().actionGet();
client().admin().indices().prepareCreate("test").setSettings(settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
ensureGreen();
PercolateResponse percolate = client().preparePercolate("my-percolate-index", "type1")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value1").endObject().endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
percolate = client().preparePercolate("my-percolate-index", "type1")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value1").endObject().field("query", matchAllQuery()).endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
}
@Test
@ -352,14 +349,16 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
for (int i = 0; i < 10; i++) {
PercolateResponse percolate = client().preparePercolate("test", "type1")
PercolateResponse percolate = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value1").endObject().endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
}
for (int i = 0; i < 10; i++) {
PercolateResponse percolate = client().preparePercolate("test", "type1")
PercolateResponse percolate = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setPreference("_local")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value1").endObject().endObject())
.execute().actionGet();
@ -399,17 +398,19 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setRefresh(true)
.execute().actionGet();
PercolateResponse percolate = client().preparePercolate("test", "type1")
PercolateResponse percolate = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value1").endObject().endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(percolate.getMatches()), arrayContaining("kuku"));
assertThat(convertFromTextArray(percolate.getMatches(), "test"), arrayContaining("kuku"));
percolate = client().preparePercolate("test", "type1")
percolate = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc").startObject("type1").field("field1", "value2").endObject().endObject().endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(percolate.getMatches()), arrayContaining("bubu"));
assertThat(convertFromTextArray(percolate.getMatches(), "test"), arrayContaining("bubu"));
}
@ -427,11 +428,12 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setRefresh(true)
.execute().actionGet();
PercolateResponse percolate = client().preparePercolate("test", "type1")
PercolateResponse percolate = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value1").endObject().endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(percolate.getMatches()), arrayContaining("kuku"));
assertThat(convertFromTextArray(percolate.getMatches(), "test"), arrayContaining("kuku"));
logger.info("--> register a query 2");
client().prepareIndex("test", "_percolator", "bubu")
@ -442,11 +444,12 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setRefresh(true)
.execute().actionGet();
percolate = client().preparePercolate("test", "type1")
percolate = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc").startObject("type1").field("field1", "value2").endObject().endObject().endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(percolate.getMatches()), arrayContaining("bubu"));
assertThat(convertFromTextArray(percolate.getMatches(), "test"), arrayContaining("bubu"));
logger.info("--> register a query 3");
client().prepareIndex("test", "_percolator", "susu")
@ -457,19 +460,22 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setRefresh(true)
.execute().actionGet();
percolate = client().preparePercolate("test", "type1")
percolate = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc").startObject("type1").field("field1", "value2").endObject().endObject()
.field("query", termQuery("color", "red")).endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(percolate.getMatches()), arrayContaining("susu"));
assertThat(convertFromTextArray(percolate.getMatches(), "test"), arrayContaining("susu"));
logger.info("--> deleting query 1");
client().prepareDelete("test", "_percolator", "kuku").setRefresh(true).execute().actionGet();
percolate = client().preparePercolate("test", "type1").setSource(jsonBuilder().startObject().startObject("doc").startObject("type1")
.field("field1", "value1")
.endObject().endObject().endObject())
percolate = client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject().startObject("doc").startObject("type1")
.field("field1", "value1")
.endObject().endObject().endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), emptyArray());
}
@ -495,14 +501,15 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.execute().actionGet();
logger.info("--> percolate a document");
PercolateResponse percolate = client().preparePercolate("test", "type1").setSource(jsonBuilder().startObject()
.startObject("doc").startObject("type1")
.field("field1", "value1")
.endObject().endObject()
.endObject())
PercolateResponse percolate = client().preparePercolate().setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder().startObject()
.startObject("doc").startObject("type1")
.field("field1", "value1")
.endObject().endObject()
.endObject())
.execute().actionGet();
assertThat(percolate.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(percolate.getMatches()), arrayContaining("kuku"));
assertThat(convertFromTextArray(percolate.getMatches(), "test"), arrayContaining("kuku"));
}
@Test
@ -517,11 +524,12 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
client().admin().indices().prepareRefresh("test").execute().actionGet();
logger.info("--> First percolate request");
PercolateResponse response = client().preparePercolate("test", "type")
PercolateResponse response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field", "val").endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(response.getMatches()), arrayContaining("1"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContaining("1"));
IndicesStatsResponse indicesResponse = client().admin().indices().prepareStats("test").execute().actionGet();
assertThat(indicesResponse.getTotal().getPercolate().getCount(), equalTo(5l)); // We have 5 partitions
@ -535,11 +543,12 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
assertThat(percolateCount, equalTo(5l)); // We have 5 partitions
logger.info("--> Second percolate request");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field", "val").endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(response.getMatches()), arrayContaining("1"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContaining("1"));
indicesResponse = client().admin().indices().prepareStats().setPercolate(true).execute().actionGet();
assertThat(indicesResponse.getTotal().getPercolate().getCount(), equalTo(10l));
@ -563,11 +572,12 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
}
logger.info("--> {}th percolate request", counter);
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field", "val").endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(response.getMatches()), arrayContaining("1"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContaining("1"));
} while (++counter <= 1000);
assertTrue("Something is off, we should have spent at least 1ms on percolating...", moreThanOneMs);
@ -610,40 +620,44 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
client().admin().indices().prepareRefresh("test").execute().actionGet();
logger.info("--> Percolate existing doc with id 1");
PercolateResponse response = client().preparePercolate("test", "type")
PercolateResponse response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("1"))
.execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
assertThat(response.getSuccessfulShards(), equalTo(5));
assertThat(response.getMatches(), arrayWithSize(2));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("1", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("1", "4"));
logger.info("--> Percolate existing doc with id 2");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("2"))
.execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
assertThat(response.getSuccessfulShards(), equalTo(5));
assertThat(response.getMatches(), arrayWithSize(2));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("2", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("2", "4"));
logger.info("--> Percolate existing doc with id 3");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("3"))
.execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
assertThat(response.getSuccessfulShards(), equalTo(5));
assertThat(response.getMatches(), arrayWithSize(4));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("1", "2", "3", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("1", "2", "3", "4"));
logger.info("--> Percolate existing doc with id 4");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("4"))
.execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
assertThat(response.getSuccessfulShards(), equalTo(5));
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(response.getMatches()), arrayContaining("4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContaining("4"));
logger.info("--> Search normals docs, percolate queries must not be included");
SearchResponse searchResponse = client().prepareSearch("test").execute().actionGet();
@ -684,40 +698,44 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
client().admin().indices().prepareRefresh("test").execute().actionGet();
logger.info("--> Percolate existing doc with id 1");
PercolateResponse response = client().preparePercolate("test", "type")
PercolateResponse response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("1").routing("4"))
.execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
assertThat(response.getSuccessfulShards(), equalTo(5));
assertThat(response.getMatches(), arrayWithSize(2));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("1", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("1", "4"));
logger.info("--> Percolate existing doc with id 2");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("2").routing("3"))
.execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
assertThat(response.getSuccessfulShards(), equalTo(5));
assertThat(response.getMatches(), arrayWithSize(2));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("2", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("2", "4"));
logger.info("--> Percolate existing doc with id 3");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("3").routing("2"))
.execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
assertThat(response.getSuccessfulShards(), equalTo(5));
assertThat(response.getMatches(), arrayWithSize(4));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("1", "2", "3", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("1", "2", "3", "4"));
logger.info("--> Percolate existing doc with id 4");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("4").routing("1"))
.execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
assertThat(response.getSuccessfulShards(), equalTo(5));
assertThat(response.getMatches(), arrayWithSize(1));
assertThat(convertFromTextArray(response.getMatches()), arrayContaining("4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContaining("4"));
}
@Test
@ -731,7 +749,7 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
client().prepareIndex("test", "type", "3").setSource("field1", "b c").execute().actionGet();
client().prepareIndex("test", "type", "4").setSource("field1", "d").execute().actionGet();
logger.info("--> register a queries");
logger.info("--> registering queries");
client().prepareIndex("test", "_percolator", "1")
.setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "b")).field("a", "b").endObject())
.execute().actionGet();
@ -750,17 +768,19 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
client().admin().indices().prepareRefresh("test").execute().actionGet();
logger.info("--> Percolate existing doc with id 2 and version 1");
PercolateResponse response = client().preparePercolate("test", "type")
PercolateResponse response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("2").version(1l))
.execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
assertThat(response.getSuccessfulShards(), equalTo(5));
assertThat(response.getMatches(), arrayWithSize(2));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("2", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("2", "4"));
logger.info("--> Percolate existing doc with id 2 and version 2");
try {
client().preparePercolate("test", "type")
client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("2").version(2l))
.execute().actionGet();
fail("Error should have been thrown");
@ -771,22 +791,102 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
client().prepareIndex("test", "type", "2").setSource("field1", "c").execute().actionGet();
logger.info("--> Percolate existing doc with id 2 and version 2");
response = client().preparePercolate("test", "type")
response = client().preparePercolate()
.setIndices("test").setDocumentType("type")
.setGetRequest(Requests.getRequest("test").type("type").id("2").version(2l))
.execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
assertThat(response.getSuccessfulShards(), equalTo(5));
assertThat(response.getMatches(), arrayWithSize(2));
assertThat(convertFromTextArray(response.getMatches()), arrayContainingInAnyOrder("2", "4"));
assertThat(convertFromTextArray(response.getMatches(), "test"), arrayContainingInAnyOrder("2", "4"));
}
public static String[] convertFromTextArray(Text[] texts) {
if (texts.length == 0) {
@Test
public void testPercolateMultipleIndicesAndAliases() throws Exception {
client().admin().indices().prepareCreate("test1")
.setSettings(settingsBuilder().put("index.number_of_shards", 2))
.execute().actionGet();
client().admin().indices().prepareCreate("test2")
.setSettings(settingsBuilder().put("index.number_of_shards", 2))
.execute().actionGet();
ensureGreen();
logger.info("--> registering queries");
for (int i = 1; i <= 10; i++) {
String index = i % 2 == 0 ? "test1" : "test2";
client().prepareIndex(index, "_percolator", Integer.toString(i))
.setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject())
.execute().actionGet();
}
logger.info("--> Percolate doc to index test1");
PercolateResponse response = client().preparePercolate()
.setIndices("test1").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value").endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(5));
logger.info("--> Percolate doc to index test2");
response = client().preparePercolate()
.setIndices("test2").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value").endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(5));
logger.info("--> Percolate doc to index test1 and test2");
response = client().preparePercolate()
.setIndices("test1", "test2").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value").endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(10));
logger.info("--> Percolate doc to index test2 and test3, with ignore missing");
response = client().preparePercolate()
.setIndices("test1", "test3").setDocumentType("type")
.setIgnoreIndices(IgnoreIndices.MISSING)
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value").endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(5));
logger.info("--> Adding aliases");
IndicesAliasesResponse aliasesResponse = client().admin().indices().prepareAliases()
.addAlias("test1", "my-alias1")
.addAlias("test2", "my-alias1")
.addAlias("test2", "my-alias2")
.setTimeout(TimeValue.timeValueHours(10))
.execute().actionGet();
assertTrue(aliasesResponse.isAcknowledged());
logger.info("--> Percolate doc to my-alias1");
response = client().preparePercolate()
.setIndices("my-alias1").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value").endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(10));
for (PercolateResponse.Match match : response) {
assertThat(match.getIndex().string(), anyOf(equalTo("test1"), equalTo("test2")));
}
logger.info("--> Percolate doc to my-alias2");
response = client().preparePercolate()
.setIndices("my-alias2").setDocumentType("type")
.setSource(jsonBuilder().startObject().startObject("doc").field("field1", "value").endObject().endObject())
.execute().actionGet();
assertThat(response.getMatches(), arrayWithSize(5));
for (PercolateResponse.Match match : response) {
assertThat(match.getIndex().string(), equalTo("test2"));
}
}
public static String[] convertFromTextArray(PercolateResponse.Match[] matches, String index) {
if (matches.length == 0) {
return Strings.EMPTY_ARRAY;
}
String[] strings = new String[texts.length];
for (int i = 0; i < texts.length; i++) {
strings[i] = texts[i].string();
String[] strings = new String[matches.length];
for (int i = 0; i < matches.length; i++) {
assert index.equals(matches[i].getIndex().string());
strings[i] = matches[i].id().string();
}
return strings;
}

View File

@ -60,14 +60,16 @@ public class TTLPercolatorTests extends AbstractNodesTests {
.endObject()
).setRefresh(true).setTTL(ttl).execute().actionGet();
PercolateResponse percolateResponse = client.preparePercolate("test", "type1").setSource(jsonBuilder()
PercolateResponse percolateResponse = client.preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder()
.startObject()
.startObject("doc")
.field("field1", "value1")
.endObject()
.endObject()
).execute().actionGet();
assertThat(convertFromTextArray(percolateResponse.getMatches()), arrayContaining("kuku"));
assertThat(convertFromTextArray(percolateResponse.getMatches(), "test"), arrayContaining("kuku"));
long timeSpent = System.currentTimeMillis() - now;
long waitTime = ttl + purgeInterval - timeSpent;
if (waitTime >= 0) {
@ -90,7 +92,9 @@ public class TTLPercolatorTests extends AbstractNodesTests {
} while (currentDeleteCount < 2); // TTL deletes one doc, but it is indexed in the primary shard and replica shard.
assertThat(currentDeleteCount, equalTo(2l));
percolateResponse = client.preparePercolate("test", "type1").setSource(jsonBuilder()
percolateResponse = client.preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(jsonBuilder()
.startObject()
.startObject("doc")
.field("field1", "value1")