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

View File

@ -155,7 +155,8 @@ public class TransportPercolateAction extends TransportBroadcastOperationAction<
if (shardResults == null) {
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 {
PercolatorService.ReduceResult result = percolatorService.reduce(percolatorTypeId, shardResults);
long tookInMillis = System.currentTimeMillis() - request.startTime;

View File

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

View File

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