Skip to content

Commit

Permalink
inner hits: Make sure size of 0 works on the inner_hits level.
Browse files Browse the repository at this point in the history
Closes #10358
  • Loading branch information
martijnvg committed Apr 2, 2015
1 parent eeec90b commit e390ef5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,6 @@ public NestedInnerHits(SearchContext context, Query query, Map<String, BaseInner

@Override
public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContext) throws IOException {
TopDocsCollector topDocsCollector;
int topN = from() + size();
if (sort() != null) {
try {
topDocsCollector = TopFieldCollector.create(sort(), topN, true, trackScores(), trackScores());
} catch (IOException e) {
throw ExceptionsHelper.convertToElastic(e);
}
} else {
topDocsCollector = TopScoreDocCollector.create(topN);
}

Filter rawParentFilter;
if (parentObjectMapper == null) {
rawParentFilter = NonNestedDocsFilter.INSTANCE;
Expand All @@ -135,8 +123,26 @@ public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContex
BitDocIdSetFilter parentFilter = context.bitsetFilterCache().getBitDocIdSetFilter(rawParentFilter);
Filter childFilter = context.filterCache().cache(childObjectMapper.nestedTypeFilter(), null, context.queryParserService().autoFilterCachePolicy());
Query q = new FilteredQuery(query, new NestedChildrenFilter(parentFilter, childFilter, hitContext));
context.searcher().search(q, topDocsCollector);
return topDocsCollector.topDocs(from(), size());

if (size() == 0) {
TotalHitCountCollector collector = new TotalHitCountCollector();
context.searcher().search(q, collector);
return new TopDocs(collector.getTotalHits(), Lucene.EMPTY_SCORE_DOCS, 0);
} else {
int topN = from() + size();
TopDocsCollector topDocsCollector;
if (sort() != null) {
try {
topDocsCollector = TopFieldCollector.create(sort(), topN, true, trackScores(), trackScores());
} catch (IOException e) {
throw ExceptionsHelper.convertToElastic(e);
}
} else {
topDocsCollector = TopScoreDocCollector.create(topN);
}
context.searcher().search(q, topDocsCollector);
return topDocsCollector.topDocs(from(), size());
}
}

// A filter that only emits the nested children docs of a specific nested parent doc
Expand Down Expand Up @@ -250,14 +256,6 @@ public ParentChildInnerHits(SearchContext context, Query query, Map<String, Base

@Override
public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContext) throws IOException {
TopDocsCollector topDocsCollector;
int topN = from() + size();
if (sort() != null) {
topDocsCollector = TopFieldCollector.create(sort(), topN, true, trackScores(), trackScores());
} else {
topDocsCollector = TopScoreDocCollector.create(topN);
}

final String term;
final String field;
if (documentMapper.parentFieldMapper().active()) {
Expand All @@ -281,11 +279,28 @@ public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContex
}
Filter filter = new TermFilter(new Term(field, term)); // Only include docs that have the current hit as parent
Filter typeFilter = documentMapper.typeFilter(); // Only include docs that have this inner hits type.
context.searcher().search(
new FilteredQuery(query, new AndFilter(Arrays.asList(filter, typeFilter))),
topDocsCollector
);
return topDocsCollector.topDocs(from(), size());

if (size() == 0) {
TotalHitCountCollector collector = new TotalHitCountCollector();
context.searcher().search(
new FilteredQuery(query, new AndFilter(Arrays.asList(filter, typeFilter))),
collector
);
return new TopDocs(collector.getTotalHits(), Lucene.EMPTY_SCORE_DOCS, 0);
} else {
int topN = from() + size();
TopDocsCollector topDocsCollector;
if (sort() != null) {
topDocsCollector = TopFieldCollector.create(sort(), topN, true, trackScores(), trackScores());
} else {
topDocsCollector = TopScoreDocCollector.create(topN);
}
context.searcher().search(
new FilteredQuery(query, new AndFilter(Arrays.asList(filter, typeFilter))),
topDocsCollector
);
return topDocsCollector.topDocs(from(), size());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,23 @@ public void testRandomNested() throws Exception {
}
indexRandom(true, requestBuilders);

int size = randomIntBetween(0, numDocs);
SearchResponse searchResponse;
if (randomBoolean()) {
searchResponse = client().prepareSearch("idx")
.setSize(numDocs)
.addSort("_uid", SortOrder.ASC)
.addInnerHit("a", new InnerHitsBuilder.InnerHit().setPath("field1").addSort("_doc", SortOrder.DESC).setSize(numDocs)) // Sort order is DESC, because we reverse the inner objects during indexing!
.addInnerHit("b", new InnerHitsBuilder.InnerHit().setPath("field2").addSort("_doc", SortOrder.DESC).setSize(numDocs))
.addInnerHit("a", new InnerHitsBuilder.InnerHit().setPath("field1").addSort("_doc", SortOrder.DESC).setSize(size)) // Sort order is DESC, because we reverse the inner objects during indexing!
.addInnerHit("b", new InnerHitsBuilder.InnerHit().setPath("field2").addSort("_doc", SortOrder.DESC).setSize(size))
.get();
} else {
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
if (randomBoolean()) {
boolQuery.should(nestedQuery("field1", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("a").addSort("_doc", SortOrder.DESC).setSize(numDocs)));
boolQuery.should(nestedQuery("field2", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("b").addSort("_doc", SortOrder.DESC).setSize(numDocs)));
boolQuery.should(nestedQuery("field1", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("a").addSort("_doc", SortOrder.DESC).setSize(size)));
boolQuery.should(nestedQuery("field2", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("b").addSort("_doc", SortOrder.DESC).setSize(size)));
} else {
boolQuery.should(constantScoreQuery(nestedFilter("field1", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("a").addSort("_doc", SortOrder.DESC).setSize(numDocs))));
boolQuery.should(constantScoreQuery(nestedFilter("field2", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("b").addSort("_doc", SortOrder.DESC).setSize(numDocs))));
boolQuery.should(constantScoreQuery(nestedFilter("field1", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("a").addSort("_doc", SortOrder.DESC).setSize(size))));
boolQuery.should(constantScoreQuery(nestedFilter("field2", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("b").addSort("_doc", SortOrder.DESC).setSize(size))));
}
searchResponse = client().prepareSearch("idx")
.setQuery(boolQuery)
Expand All @@ -215,13 +216,14 @@ public void testRandomNested() throws Exception {
.get();
}

assertNoFailures(searchResponse);
assertHitCount(searchResponse, numDocs);
assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
SearchHit searchHit = searchResponse.getHits().getAt(i);
SearchHits inner = searchHit.getInnerHits().get("a");
assertThat(inner.totalHits(), equalTo((long) field1InnerObjects[i]));
for (int j = 0; j < field1InnerObjects[i]; j++) {
for (int j = 0; j < field1InnerObjects[i] && j < size; j++) {
SearchHit innerHit = inner.getAt(j);
assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field1"));
assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
Expand All @@ -230,7 +232,7 @@ public void testRandomNested() throws Exception {

inner = searchHit.getInnerHits().get("b");
assertThat(inner.totalHits(), equalTo((long) field2InnerObjects[i]));
for (int j = 0; j < field2InnerObjects[i]; j++) {
for (int j = 0; j < field2InnerObjects[i] && j < size; j++) {
SearchHit innerHit = inner.getAt(j);
assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field2"));
assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
Expand Down Expand Up @@ -373,23 +375,24 @@ public void testRandomParentChild() throws Exception {
}
indexRandom(true, requestBuilders);

int size = randomIntBetween(0, numDocs);
SearchResponse searchResponse;
if (randomBoolean()) {
searchResponse = client().prepareSearch("idx")
.setSize(numDocs)
.setTypes("parent")
.addSort("_uid", SortOrder.ASC)
.addInnerHit("a", new InnerHitsBuilder.InnerHit().setType("child1").addSort("_uid", SortOrder.ASC).setSize(numDocs))
.addInnerHit("b", new InnerHitsBuilder.InnerHit().setType("child2").addSort("_uid", SortOrder.ASC).setSize(numDocs))
.addInnerHit("a", new InnerHitsBuilder.InnerHit().setType("child1").addSort("_uid", SortOrder.ASC).setSize(size))
.addInnerHit("b", new InnerHitsBuilder.InnerHit().setType("child2").addSort("_uid", SortOrder.ASC).setSize(size))
.get();
} else {
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
if (randomBoolean()) {
boolQuery.should(hasChildQuery("child1", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("a").addSort("_uid", SortOrder.ASC).setSize(numDocs)));
boolQuery.should(hasChildQuery("child2", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("b").addSort("_uid", SortOrder.ASC).setSize(numDocs)));
boolQuery.should(hasChildQuery("child1", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("a").addSort("_uid", SortOrder.ASC).setSize(size)));
boolQuery.should(hasChildQuery("child2", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("b").addSort("_uid", SortOrder.ASC).setSize(size)));
} else {
boolQuery.should(constantScoreQuery(hasChildFilter("child1", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("a").addSort("_uid", SortOrder.ASC).setSize(numDocs))));
boolQuery.should(constantScoreQuery(hasChildFilter("child2", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("b").addSort("_uid", SortOrder.ASC).setSize(numDocs))));
boolQuery.should(constantScoreQuery(hasChildFilter("child1", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("a").addSort("_uid", SortOrder.ASC).setSize(size))));
boolQuery.should(constantScoreQuery(hasChildFilter("child2", matchAllQuery()).innerHit(new QueryInnerHitBuilder().setName("b").addSort("_uid", SortOrder.ASC).setSize(size))));
}
searchResponse = client().prepareSearch("idx")
.setSize(numDocs)
Expand All @@ -399,6 +402,7 @@ public void testRandomParentChild() throws Exception {
.get();
}

assertNoFailures(searchResponse);
assertHitCount(searchResponse, numDocs);
assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));

Expand All @@ -411,7 +415,7 @@ public void testRandomParentChild() throws Exception {

SearchHits inner = searchHit.getInnerHits().get("a");
assertThat(inner.totalHits(), equalTo((long) child1InnerObjects[parent]));
for (int child = 0; child < child1InnerObjects[parent]; child++) {
for (int child = 0; child < child1InnerObjects[parent] && child < size; child++) {
SearchHit innerHit = inner.getAt(child);
assertThat(innerHit.getType(), equalTo("child1"));
String childId = String.format(Locale.ENGLISH, "%04d", offset1 + child);
Expand All @@ -422,7 +426,7 @@ public void testRandomParentChild() throws Exception {

inner = searchHit.getInnerHits().get("b");
assertThat(inner.totalHits(), equalTo((long) child2InnerObjects[parent]));
for (int child = 0; child < child2InnerObjects[parent]; child++) {
for (int child = 0; child < child2InnerObjects[parent] && child < size; child++) {
SearchHit innerHit = inner.getAt(child);
assertThat(innerHit.getType(), equalTo("child2"));
String childId = String.format(Locale.ENGLISH, "%04d", offset2 + child);
Expand Down

0 comments on commit e390ef5

Please sign in to comment.