Skip to content

Commit

Permalink
Fix remaining leaked SearchResponse issues in :server:integTests (#10…
Browse files Browse the repository at this point in the history
…2896)

This should be the last round for this module, found these using a prototype
that has `SearchResponse` ref-counted already.
  • Loading branch information
original-brownbear authored Dec 4, 2023
1 parent 5b0aec5 commit 143f420
Show file tree
Hide file tree
Showing 39 changed files with 950 additions and 986 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ public void testHasParentFilter() throws Exception {
constantScoreQuery(hasParentQuery("parent", termQuery("p_field", parentToChildrenEntry.getKey()), false))
).setSize(numChildDocsPerParent),
response -> {
assertNoFailures(response);
Set<String> childIds = parentToChildrenEntry.getValue();
assertThat(response.getHits().getTotalHits().value, equalTo((long) childIds.size()));
for (int i = 0; i < response.getHits().getTotalHits().value; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.List;
import java.util.Map;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailuresAndResponse;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
import static org.hamcrest.Matchers.equalTo;

public class LookupRuntimeFieldIT extends ESIntegTestCase {
Expand Down Expand Up @@ -132,90 +134,92 @@ public void populateIndex() throws Exception {
}

public void testBasic() {
SearchResponse searchResponse = prepareSearch("books").addFetchField("author")
.addFetchField("title")
.addSort("published_date", SortOrder.DESC)
.setSize(3)
.get();
ElasticsearchAssertions.assertNoFailures(searchResponse);
ElasticsearchAssertions.assertHitCount(searchResponse, 5);
assertNoFailuresAndResponse(
prepareSearch("books").addFetchField("author").addFetchField("title").addSort("published_date", SortOrder.DESC).setSize(3),
searchResponse -> {
ElasticsearchAssertions.assertHitCount(searchResponse, 5);

SearchHit hit0 = searchResponse.getHits().getHits()[0];
assertThat(hit0.field("title").getValues(), equalTo(List.of("the fifth book")));
assertThat(
hit0.field("author").getValues(),
equalTo(List.of(Map.of("first_name", List.of("Mike"), "last_name", List.of("Boston"))))
);
SearchHit hit0 = searchResponse.getHits().getHits()[0];
assertThat(hit0.field("title").getValues(), equalTo(List.of("the fifth book")));
assertThat(
hit0.field("author").getValues(),
equalTo(List.of(Map.of("first_name", List.of("Mike"), "last_name", List.of("Boston"))))
);

SearchHit hit1 = searchResponse.getHits().getHits()[1];
assertThat(hit1.field("title").getValues(), equalTo(List.of("the forth book")));
assertThat(
hit1.field("author").getValues(),
equalTo(
List.of(
Map.of("first_name", List.of("Mike"), "last_name", List.of("Boston")),
Map.of("first_name", List.of("Jack"), "last_name", List.of("Austin"))
)
)
);
SearchHit hit1 = searchResponse.getHits().getHits()[1];
assertThat(hit1.field("title").getValues(), equalTo(List.of("the forth book")));
assertThat(
hit1.field("author").getValues(),
equalTo(
List.of(
Map.of("first_name", List.of("Mike"), "last_name", List.of("Boston")),
Map.of("first_name", List.of("Jack"), "last_name", List.of("Austin"))
)
)
);

SearchHit hit2 = searchResponse.getHits().getHits()[2];
assertThat(hit2.field("title").getValues(), equalTo(List.of("the third book")));
assertThat(
hit2.field("author").getValues(),
equalTo(List.of(Map.of("first_name", List.of("Mike"), "last_name", List.of("Boston"))))
SearchHit hit2 = searchResponse.getHits().getHits()[2];
assertThat(hit2.field("title").getValues(), equalTo(List.of("the third book")));
assertThat(
hit2.field("author").getValues(),
equalTo(List.of(Map.of("first_name", List.of("Mike"), "last_name", List.of("Boston"))))
);
}
);
}

public void testLookupMultipleIndices() throws IOException {
SearchResponse searchResponse = prepareSearch("books").setRuntimeMappings(parseMapping("""
{
"publisher": {
"type": "lookup",
"target_index": "publishers",
"input_field": "publisher_id",
"target_field": "_id",
"fetch_fields": ["name", "city"]
assertResponse(
prepareSearch("books").setRuntimeMappings(parseMapping("""
{
"publisher": {
"type": "lookup",
"target_index": "publishers",
"input_field": "publisher_id",
"target_field": "_id",
"fetch_fields": ["name", "city"]
}
}
}
"""))
.setFetchSource(false)
.addFetchField("title")
.addFetchField("author")
.addFetchField("publisher")
.addSort("published_date", SortOrder.DESC)
.setSize(2)
.get();
SearchHit hit0 = searchResponse.getHits().getHits()[0];
assertThat(hit0.field("title").getValues(), equalTo(List.of("the fifth book")));
assertThat(
hit0.field("author").getValues(),
equalTo(List.of(Map.of("first_name", List.of("Mike"), "last_name", List.of("Boston"))))
);
assertThat(
hit0.field("publisher").getValues(),
equalTo(List.of(Map.of("name", List.of("The second publisher"), "city", List.of("Toronto"))))
);
"""))
.setFetchSource(false)
.addFetchField("title")
.addFetchField("author")
.addFetchField("publisher")
.addSort("published_date", SortOrder.DESC)
.setSize(2),
searchResponse -> {
SearchHit hit0 = searchResponse.getHits().getHits()[0];
assertThat(hit0.field("title").getValues(), equalTo(List.of("the fifth book")));
assertThat(
hit0.field("author").getValues(),
equalTo(List.of(Map.of("first_name", List.of("Mike"), "last_name", List.of("Boston"))))
);
assertThat(
hit0.field("publisher").getValues(),
equalTo(List.of(Map.of("name", List.of("The second publisher"), "city", List.of("Toronto"))))
);

SearchHit hit1 = searchResponse.getHits().getHits()[1];
assertThat(hit1.field("title").getValues(), equalTo(List.of("the forth book")));
assertThat(
hit1.field("author").getValues(),
equalTo(
List.of(
Map.of("first_name", List.of("Mike"), "last_name", List.of("Boston")),
Map.of("first_name", List.of("Jack"), "last_name", List.of("Austin"))
)
)
);
assertThat(
hit1.field("publisher").getValues(),
equalTo(List.of(Map.of("name", List.of("The first publisher"), "city", List.of("Montreal", "Vancouver"))))
SearchHit hit1 = searchResponse.getHits().getHits()[1];
assertThat(hit1.field("title").getValues(), equalTo(List.of("the forth book")));
assertThat(
hit1.field("author").getValues(),
equalTo(
List.of(
Map.of("first_name", List.of("Mike"), "last_name", List.of("Boston")),
Map.of("first_name", List.of("Jack"), "last_name", List.of("Austin"))
)
)
);
assertThat(
hit1.field("publisher").getValues(),
equalTo(List.of(Map.of("name", List.of("The first publisher"), "city", List.of("Montreal", "Vancouver"))))
);
}
);
}

public void testFetchField() throws Exception {
SearchResponse searchResponse = prepareSearch("books").setRuntimeMappings(parseMapping("""
assertNoFailuresAndResponse(prepareSearch("books").setRuntimeMappings(parseMapping("""
{
"author": {
"type": "lookup",
Expand All @@ -225,12 +229,15 @@ public void testFetchField() throws Exception {
"fetch_fields": ["first_name", {"field": "joined", "format": "MM/yyyy"}]
}
}
""")).addFetchField("author").addFetchField("title").addSort("published_date", SortOrder.ASC).setSize(1).get();
ElasticsearchAssertions.assertNoFailures(searchResponse);
SearchHit hit0 = searchResponse.getHits().getHits()[0];
// "author", "john", "first_name", "John", "last_name", "New York", "joined", "2020-03-01"
assertThat(hit0.field("title").getValues(), equalTo(List.of("the first book")));
assertThat(hit0.field("author").getValues(), equalTo(List.of(Map.of("first_name", List.of("John"), "joined", List.of("03/2020")))));
""")).addFetchField("author").addFetchField("title").addSort("published_date", SortOrder.ASC).setSize(1), searchResponse -> {
SearchHit hit0 = searchResponse.getHits().getHits()[0];
// "author", "john", "first_name", "John", "last_name", "New York", "joined", "2020-03-01"
assertThat(hit0.field("title").getValues(), equalTo(List.of("the first book")));
assertThat(
hit0.field("author").getValues(),
equalTo(List.of(Map.of("first_name", List.of("John"), "joined", List.of("03/2020"))))
);
});
}

private Map<String, Object> parseMapping(String mapping) throws IOException {
Expand Down
Loading

0 comments on commit 143f420

Please sign in to comment.