Query String caching could cause matched_filters not working

When searching with a query containing query_strings inside a bool query, the specified _name is randomly missing from the results due to caching.

Closes #4361.
Closes #4371.
This commit is contained in:
David Pilato 2013-12-06 21:50:06 +01:00
parent 8c1de501e7
commit 822acfa568
2 changed files with 42 additions and 0 deletions

View File

@ -203,6 +203,9 @@ public class QueryStringQueryParser implements QueryParser {
qpSettings.queryTypes(parseContext.queryTypes());
Query query = parseContext.indexCache().queryParserCache().get(qpSettings);
if (query != null) {
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
}

View File

@ -203,4 +203,43 @@ public class MatchedQueriesTests extends ElasticsearchIntegrationTest {
}
}
}
/**
* Test case for issue #4361: https://github.com/elasticsearch/elasticsearch/issues/4361
*/
@Test
public void testMatchedWithShould() throws Exception {
createIndex("test");
ensureGreen();
client().prepareIndex("test", "type1", "1").setSource("content", "Lorem ipsum dolor sit amet").get();
client().prepareIndex("test", "type1", "2").setSource("content", "consectetur adipisicing elit").get();
refresh();
// Execute search 5 times to load it in cache
for (int i = 0; i < 5; i++) {
SearchResponse searchResponse = client().prepareSearch()
.setQuery(
boolQuery()
.minimumNumberShouldMatch(1)
.should(queryString("dolor").queryName("dolor"))
.should(queryString("elit").queryName("elit"))
)
.setPreference("_primary")
.get();
assertHitCount(searchResponse, 2l);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.id().equals("1")) {
assertThat(hit.matchedQueries().length, equalTo(1));
assertThat(hit.matchedQueries(), hasItemInArray("dolor"));
} else if (hit.id().equals("2")) {
assertThat(hit.matchedQueries().length, equalTo(1));
assertThat(hit.matchedQueries(), hasItemInArray("elit"));
} else {
fail("Unexpected document returned with id " + hit.id());
}
}
}
}
}