Percolator response now always returns the `matches` key.

Closes #4881
This commit is contained in:
Martijn van Groningen 2014-01-27 16:10:30 +01:00
parent 04c41fe349
commit f38296da61
5 changed files with 71 additions and 23 deletions

View File

@ -0,0 +1,31 @@
---
"Basic percolation tests on an empty cluster":
- do:
indices.create:
index: test_index
- do:
indices.refresh: {}
- do:
percolate:
index: test_index
type: test_type
body:
doc:
foo: bar
- match: {'total': 0}
- match: {'matches': []}
- do:
count_percolate:
index: test_index
type: test_type
body:
doc:
foo: bar
- is_false: matches
- match: {'total': 0}

View File

@ -42,7 +42,7 @@ import java.util.*;
*/ */
public class PercolateResponse extends BroadcastOperationResponse implements Iterable<PercolateResponse.Match>, ToXContent { public class PercolateResponse extends BroadcastOperationResponse implements Iterable<PercolateResponse.Match>, ToXContent {
private static final Match[] EMPTY = new Match[0]; public static final Match[] EMPTY = new Match[0];
private long tookInMillis; private long tookInMillis;
private Match[] matches; private Match[] matches;
@ -60,10 +60,10 @@ public class PercolateResponse extends BroadcastOperationResponse implements Ite
this.aggregations = aggregations; this.aggregations = aggregations;
} }
public PercolateResponse(int totalShards, int successfulShards, int failedShards, List<ShardOperationFailedException> shardFailures, long tookInMillis) { public PercolateResponse(int totalShards, int successfulShards, int failedShards, List<ShardOperationFailedException> shardFailures, long tookInMillis, Match[] matches) {
super(totalShards, successfulShards, failedShards, shardFailures); super(totalShards, successfulShards, failedShards, shardFailures);
this.tookInMillis = tookInMillis; this.tookInMillis = tookInMillis;
this.matches = EMPTY; this.matches = matches;
} }
PercolateResponse() { PercolateResponse() {
@ -87,18 +87,30 @@ public class PercolateResponse extends BroadcastOperationResponse implements Ite
return tookInMillis; return tookInMillis;
} }
/**
* @return The queries that match with the document being percolated. This can return <code>null</code> if th.
*/
public Match[] getMatches() { public Match[] getMatches() {
return this.matches; return this.matches;
} }
/**
* @return The total number of queries that have matched with the document being percolated.
*/
public long getCount() { public long getCount() {
return count; return count;
} }
/**
* @return Any facet that has been executed on the query metadata. This can return <code>null</code>.
*/
public InternalFacets getFacets() { public InternalFacets getFacets() {
return facets; return facets;
} }
/**
* @return Any aggregations that has been executed on the query metadata. This can return <code>null</code>.
*/
public InternalAggregations getAggregations() { public InternalAggregations getAggregations() {
return aggregations; return aggregations;
} }
@ -116,7 +128,7 @@ public class PercolateResponse extends BroadcastOperationResponse implements Ite
RestActions.buildBroadcastShardsHeader(builder, this); RestActions.buildBroadcastShardsHeader(builder, this);
builder.field(Fields.TOTAL, count); builder.field(Fields.TOTAL, count);
if (matches.length != 0) { if (matches != null) {
builder.startArray(Fields.MATCHES); builder.startArray(Fields.MATCHES);
boolean justIds = "ids".equals(params.param("percolate_format")); boolean justIds = "ids".equals(params.param("percolate_format"));
if (justIds) { if (justIds) {
@ -171,11 +183,13 @@ public class PercolateResponse extends BroadcastOperationResponse implements Ite
tookInMillis = in.readVLong(); tookInMillis = in.readVLong();
count = in.readVLong(); count = in.readVLong();
int size = in.readVInt(); int size = in.readVInt();
if (size != -1) {
matches = new Match[size]; matches = new Match[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
matches[i] = new Match(); matches[i] = new Match();
matches[i].readFrom(in); matches[i].readFrom(in);
} }
}
facets = InternalFacets.readOptionalFacets(in); facets = InternalFacets.readOptionalFacets(in);
aggregations = InternalAggregations.readOptionalAggregations(in); aggregations = InternalAggregations.readOptionalAggregations(in);
} }
@ -185,10 +199,14 @@ public class PercolateResponse extends BroadcastOperationResponse implements Ite
super.writeTo(out); super.writeTo(out);
out.writeVLong(tookInMillis); out.writeVLong(tookInMillis);
out.writeVLong(count); out.writeVLong(count);
if (matches == null) {
out.writeVInt(-1);
} else {
out.writeVInt(matches.length); out.writeVInt(matches.length);
for (Match match : matches) { for (Match match : matches) {
match.writeTo(out); match.writeTo(out);
} }
}
out.writeOptionalStreamable(facets); out.writeOptionalStreamable(facets);
out.writeOptionalStreamable(aggregations); out.writeOptionalStreamable(aggregations);
} }

View File

@ -155,7 +155,8 @@ public class TransportPercolateAction extends TransportBroadcastOperationAction<
if (shardResults == null) { if (shardResults == null) {
long tookInMillis = System.currentTimeMillis() - request.startTime; long tookInMillis = System.currentTimeMillis() - request.startTime;
return new PercolateResponse(shardsResponses.length(), successfulShards, failedShards, shardFailures, tookInMillis); PercolateResponse.Match[] matches = request.onlyCount() ? null : PercolateResponse.EMPTY;
return new PercolateResponse(shardsResponses.length(), successfulShards, failedShards, shardFailures, tookInMillis, matches);
} else { } else {
PercolatorService.ReduceResult result = percolatorService.reduce(percolatorTypeId, shardResults); PercolatorService.ReduceResult result = percolatorService.reduce(percolatorTypeId, shardResults);
long tookInMillis = System.currentTimeMillis() - request.startTime; long tookInMillis = System.currentTimeMillis() - request.startTime;

View File

@ -795,8 +795,6 @@ public class PercolatorService extends AbstractComponent {
public final static class ReduceResult { public final static class ReduceResult {
private static PercolateResponse.Match[] EMPTY = new PercolateResponse.Match[0];
private final long count; private final long count;
private final PercolateResponse.Match[] matches; private final PercolateResponse.Match[] matches;
private final InternalFacets reducedFacets; private final InternalFacets reducedFacets;
@ -811,7 +809,7 @@ public class PercolatorService extends AbstractComponent {
public ReduceResult(long count, InternalFacets reducedFacets, InternalAggregations reducedAggregations) { public ReduceResult(long count, InternalFacets reducedFacets, InternalAggregations reducedAggregations) {
this.count = count; this.count = count;
this.matches = EMPTY; this.matches = null;
this.reducedFacets = reducedFacets; this.reducedFacets = reducedFacets;
this.reducedAggregations = reducedAggregations; this.reducedAggregations = reducedAggregations;
} }

View File

@ -931,7 +931,7 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
.setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "b").endObject())) .setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "b").endObject()))
.execute().actionGet(); .execute().actionGet();
assertMatchCount(response, 2l); assertMatchCount(response, 2l);
assertThat(response.getMatches(), emptyArray()); assertThat(response.getMatches(), nullValue());
logger.info("--> Count percolate doc with field1=c"); logger.info("--> Count percolate doc with field1=c");
response = client().preparePercolate() response = client().preparePercolate()
@ -939,7 +939,7 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
.setPercolateDoc(docBuilder().setDoc(yamlBuilder().startObject().field("field1", "c").endObject())) .setPercolateDoc(docBuilder().setDoc(yamlBuilder().startObject().field("field1", "c").endObject()))
.execute().actionGet(); .execute().actionGet();
assertMatchCount(response, 2l); assertMatchCount(response, 2l);
assertThat(response.getMatches(), emptyArray()); assertThat(response.getMatches(), nullValue());
logger.info("--> Count percolate doc with field1=b c"); logger.info("--> Count percolate doc with field1=b c");
response = client().preparePercolate() response = client().preparePercolate()
@ -947,7 +947,7 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
.setPercolateDoc(docBuilder().setDoc(smileBuilder().startObject().field("field1", "b c").endObject())) .setPercolateDoc(docBuilder().setDoc(smileBuilder().startObject().field("field1", "b c").endObject()))
.execute().actionGet(); .execute().actionGet();
assertMatchCount(response, 4l); assertMatchCount(response, 4l);
assertThat(response.getMatches(), emptyArray()); assertThat(response.getMatches(), nullValue());
logger.info("--> Count percolate doc with field1=d"); logger.info("--> Count percolate doc with field1=d");
response = client().preparePercolate() response = client().preparePercolate()
@ -955,7 +955,7 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
.setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "d").endObject())) .setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "d").endObject()))
.execute().actionGet(); .execute().actionGet();
assertMatchCount(response, 1l); assertMatchCount(response, 1l);
assertThat(response.getMatches(), emptyArray()); assertThat(response.getMatches(), nullValue());
logger.info("--> Count percolate non existing doc"); logger.info("--> Count percolate non existing doc");
try { try {
@ -1003,7 +1003,7 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
.setGetRequest(Requests.getRequest("test").type("type").id("1")) .setGetRequest(Requests.getRequest("test").type("type").id("1"))
.execute().actionGet(); .execute().actionGet();
assertMatchCount(response, 2l); assertMatchCount(response, 2l);
assertThat(response.getMatches(), emptyArray()); assertThat(response.getMatches(), nullValue());
logger.info("--> Count percolate existing doc with id 2"); logger.info("--> Count percolate existing doc with id 2");
response = client().preparePercolate() response = client().preparePercolate()
@ -1011,7 +1011,7 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
.setGetRequest(Requests.getRequest("test").type("type").id("2")) .setGetRequest(Requests.getRequest("test").type("type").id("2"))
.execute().actionGet(); .execute().actionGet();
assertMatchCount(response, 2l); assertMatchCount(response, 2l);
assertThat(response.getMatches(), emptyArray()); assertThat(response.getMatches(), nullValue());
logger.info("--> Count percolate existing doc with id 3"); logger.info("--> Count percolate existing doc with id 3");
response = client().preparePercolate() response = client().preparePercolate()
@ -1019,7 +1019,7 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
.setGetRequest(Requests.getRequest("test").type("type").id("3")) .setGetRequest(Requests.getRequest("test").type("type").id("3"))
.execute().actionGet(); .execute().actionGet();
assertMatchCount(response, 4l); assertMatchCount(response, 4l);
assertThat(response.getMatches(), emptyArray()); assertThat(response.getMatches(), nullValue());
logger.info("--> Count percolate existing doc with id 4"); logger.info("--> Count percolate existing doc with id 4");
response = client().preparePercolate() response = client().preparePercolate()
@ -1027,7 +1027,7 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
.setGetRequest(Requests.getRequest("test").type("type").id("4")) .setGetRequest(Requests.getRequest("test").type("type").id("4"))
.execute().actionGet(); .execute().actionGet();
assertMatchCount(response, 1l); assertMatchCount(response, 1l);
assertThat(response.getMatches(), emptyArray()); assertThat(response.getMatches(), nullValue());
} }
@Test @Test