Skip to content

Commit 9f8f13d

Browse files
committed
Don't throw UOE in PercolateContext#from and #size
Create mapping in PercolatorTests#testPercolateSorting_unsupportedField in create index call instead of lazily via index call.
1 parent bafa4ce commit 9f8f13d

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

src/main/java/org/elasticsearch/action/percolate/PercolateShardResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public PercolateShardResponse(BytesRef[] matches, List<Map<String, HighlightFiel
6464
this.count = count;
6565
this.scores = scores;
6666
this.percolatorTypeId = context.percolatorTypeId;
67-
this.requestedSize = context.size;
67+
this.requestedSize = context.size();
6868
QuerySearchResult result = context.queryResult();
6969
if (result != null) {
7070
if (result.facets() != null) {

src/main/java/org/elasticsearch/percolator/PercolateContext.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,11 @@
8282
public class PercolateContext extends SearchContext {
8383

8484
public boolean limit;
85-
public int size;
85+
private int size;
8686
public boolean doSort;
8787
public byte percolatorTypeId;
8888
private boolean trackScores;
8989

90-
private final PercolateShardRequest request;
9190
private final SearchShardTarget searchShardTarget;
9291
private final IndexService indexService;
9392
private final IndexFieldDataService fieldDataService;
@@ -118,7 +117,6 @@ public class PercolateContext extends SearchContext {
118117
public PercolateContext(PercolateShardRequest request, SearchShardTarget searchShardTarget, IndexShard indexShard,
119118
IndexService indexService, CacheRecycler cacheRecycler, PageCacheRecycler pageCacheRecycler,
120119
BigArrays bigArrays, ScriptService scriptService) {
121-
this.request = request;
122120
this.indexShard = indexShard;
123121
this.indexService = indexService;
124122
this.fieldDataService = indexService.fieldData();
@@ -554,7 +552,7 @@ public Filter aliasFilter() {
554552

555553
@Override
556554
public int from() {
557-
throw new UnsupportedOperationException();
555+
return 0;
558556
}
559557

560558
@Override
@@ -564,12 +562,14 @@ public SearchContext from(int from) {
564562

565563
@Override
566564
public int size() {
567-
throw new UnsupportedOperationException();
565+
return size;
568566
}
569567

570568
@Override
571569
public SearchContext size(int size) {
572-
throw new UnsupportedOperationException();
570+
this.size = size;
571+
this.limit = true;
572+
return this;
573573
}
574574

575575
@Override

src/main/java/org/elasticsearch/percolator/PercolatorService.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ public PercolateShardResponse percolate(PercolateShardRequest request) {
185185
throw new ElasticsearchIllegalArgumentException("Can't highlight if size isn't specified");
186186
}
187187

188-
if (context.size < 0) {
189-
context.size = 0;
188+
if (context.size() < 0) {
189+
context.size(0);
190190
}
191191

192192
// parse the source either into one MemoryIndex, if it is a single document or index multiple docs if nested
@@ -294,10 +294,9 @@ private ParsedDocument parseRequest(IndexService documentIndexService, Percolate
294294
break;
295295
} else if (token.isValue()) {
296296
if ("size".equals(currentFieldName)) {
297-
context.limit = true;
298-
context.size = parser.intValue();
299-
if (context.size < 0) {
300-
throw new ElasticsearchParseException("size is set to [" + context.size + "] and is expected to be higher or equal to 0");
297+
context.size(parser.intValue());
298+
if (context.size() < 0) {
299+
throw new ElasticsearchParseException("size is set to [" + context.size() + "] and is expected to be higher or equal to 0");
301300
}
302301
} else if ("sort".equals(currentFieldName)) {
303302
parseSort(parser, context);
@@ -531,7 +530,7 @@ public PercolateShardResponse doPercolate(PercolateShardRequest request, Percola
531530
}
532531

533532
if (collector.exists()) {
534-
if (!context.limit || count < context.size) {
533+
if (!context.limit || count < context.size()) {
535534
matches.add(entry.getKey().bytes);
536535
if (context.highlight() != null) {
537536
highlightPhase.hitExecute(context, context.hitContext());

src/main/java/org/elasticsearch/percolator/QueryCollector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ final static class Match extends QueryCollector {
184184
Match(ESLogger logger, PercolateContext context, HighlightPhase highlightPhase) {
185185
super(logger, context);
186186
this.limit = context.limit;
187-
this.size = context.size;
187+
this.size = context.size();
188188
this.context = context;
189189
this.highlightPhase = highlightPhase;
190190
}
@@ -243,7 +243,7 @@ final static class MatchAndSort extends QueryCollector {
243243
MatchAndSort(ESLogger logger, PercolateContext context) {
244244
super(logger, context);
245245
// TODO: Use TopFieldCollector.create(...) for ascending and decending scoring?
246-
topDocsCollector = TopScoreDocCollector.create(context.size, false);
246+
topDocsCollector = TopScoreDocCollector.create(context.size(), false);
247247
}
248248

249249
@Override
@@ -303,7 +303,7 @@ final static class MatchAndScore extends QueryCollector {
303303
MatchAndScore(ESLogger logger, PercolateContext context, HighlightPhase highlightPhase) {
304304
super(logger, context);
305305
this.limit = context.limit;
306-
this.size = context.size;
306+
this.size = context.size();
307307
this.context = context;
308308
this.highlightPhase = highlightPhase;
309309
}

src/test/java/org/elasticsearch/percolator/PercolatorTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,15 +1256,17 @@ public void testPercolateSortingWithNoSize() throws Exception {
12561256

12571257
@Test
12581258
public void testPercolateSorting_unsupportedField() throws Exception {
1259-
client().admin().indices().prepareCreate("my-index").execute().actionGet();
1259+
client().admin().indices().prepareCreate("my-index")
1260+
.addMapping("my-type", "level", "type=integer")
1261+
.get();
12601262
ensureGreen();
12611263

12621264
client().prepareIndex("my-index", PercolatorService.TYPE_NAME, "1")
12631265
.setSource(jsonBuilder().startObject().field("query", matchAllQuery()).field("level", 1).endObject())
1264-
.execute().actionGet();
1266+
.get();
12651267
client().prepareIndex("my-index", PercolatorService.TYPE_NAME, "2")
12661268
.setSource(jsonBuilder().startObject().field("query", matchAllQuery()).field("level", 2).endObject())
1267-
.execute().actionGet();
1269+
.get();
12681270
refresh();
12691271

12701272
PercolateResponse response = client().preparePercolate().setIndices("my-index").setDocumentType("my-type")

0 commit comments

Comments
 (0)