Skip to content

Commit

Permalink
Fix SimpleNestedIT.testExplain flaky test
Browse files Browse the repository at this point in the history
Signed-off-by: Neetika Singhal <[email protected]>
  • Loading branch information
neetikasinghal committed Jan 2, 2024
1 parent 63f4f13 commit 69378f1
Showing 1 changed file with 101 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public void testDeleteNestedDocsWithAlias() throws Exception {
assertDocumentCount("test", 6);
}

public void testExplain() throws Exception {
public void testExplainSingleDocIndexingNoDeletedDoc() throws Exception {
assertAcked(
prepareCreate("test").setMapping(
jsonBuilder().startObject()
Expand Down Expand Up @@ -487,6 +487,105 @@ public void testExplain() throws Exception {
)
.setRefreshPolicy(IMMEDIATE)
.get();

SearchResponse searchResponse = client().prepareSearch("test")
.setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1"), ScoreMode.Total))
.setExplain(true)
.get();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
Explanation explanation = searchResponse.getHits().getHits()[0].getExplanation();
assertThat(explanation.getValue(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
assertThat(explanation.toString(), startsWith("0.36464313 = Score based on 2 child docs"));
assertTrue(explanation.toString().contains("in range from 0 to 1"));
}

public void testExplainMultipleDocIndexingNoDeletedDoc() throws Exception {
assertAcked(
prepareCreate("test").setMapping(
jsonBuilder().startObject()
.startObject("properties")
.startObject("nested0")
.field("type", "nested")
.endObject()
.startObject("nested1")
.field("type", "nested")
.endObject()
.startObject("nested2")
.field("type", "nested")
.endObject()
.endObject()
.endObject()
)
);

ensureGreen();

for (int i = 0; i < 3; i++) {
client().prepareIndex("test")
.setId(String.valueOf(i))
.setSource(
jsonBuilder().startObject()
.field("field" + i, "value" + i)
.startArray("nested" + i)
.startObject()
.field("n_field" + i, "n_value" + i)
.endObject()
.startObject()
.field("n_field" + i, "n_value" + i)
.endObject()
.endArray()
.endObject()
)
.setRefreshPolicy(IMMEDIATE)
.get();
}

SearchResponse searchResponse = client().prepareSearch("test")
.setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1"), ScoreMode.Total))
.setExplain(true)
.get();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
Explanation explanation = searchResponse.getHits().getHits()[0].getExplanation();
assertThat(explanation.getValue(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
assertThat(explanation.toString(), startsWith("0.36464313 = Score based on 2 child docs"));
}

public void testExplainSingleDocIndexingWithDeletedDoc() throws Exception {
assertAcked(
prepareCreate("test").setMapping(
jsonBuilder().startObject()
.startObject("properties")
.startObject("nested1")
.field("type", "nested")
.endObject()
.endObject()
.endObject()
)
);

ensureGreen();

client().prepareIndex("test")
.setId("1")
.setSource(
jsonBuilder().startObject()
.field("field1", "value1")
.startArray("nested1")
.startObject()
.field("n_field1", "n_value1")
.endObject()
.startObject()
.field("n_field1", "n_value1")
.endObject()
.endArray()
.endObject()
)
.setRefreshPolicy(IMMEDIATE)
.get();

// this will lead to deleted docs
indexRandomForConcurrentSearch("test");

SearchResponse searchResponse = client().prepareSearch("test")
Expand All @@ -497,7 +596,7 @@ public void testExplain() throws Exception {
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
Explanation explanation = searchResponse.getHits().getHits()[0].getExplanation();
assertThat(explanation.getValue(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
assertThat(explanation.toString(), startsWith("0.36464313 = Score based on 2 child docs in range from 0 to 1"));
assertThat(explanation.toString(), startsWith("0.36464313 = Score based on 2 child docs"));
}

public void testSimpleNestedSorting() throws Exception {
Expand Down

0 comments on commit 69378f1

Please sign in to comment.