Skip to content

Commit 822acfa

Browse files
committed
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 elastic#4361. Closes elastic#4371.
1 parent 8c1de50 commit 822acfa

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
203203
qpSettings.queryTypes(parseContext.queryTypes());
204204
Query query = parseContext.indexCache().queryParserCache().get(qpSettings);
205205
if (query != null) {
206+
if (queryName != null) {
207+
parseContext.addNamedQuery(queryName, query);
208+
}
206209
return query;
207210
}
208211

src/test/java/org/elasticsearch/search/matchedqueries/MatchedQueriesTests.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,43 @@ public void testIndicesFilterSupportsName() {
203203
}
204204
}
205205
}
206+
207+
/**
208+
* Test case for issue #4361: https://github.com/elasticsearch/elasticsearch/issues/4361
209+
*/
210+
@Test
211+
public void testMatchedWithShould() throws Exception {
212+
createIndex("test");
213+
ensureGreen();
214+
215+
client().prepareIndex("test", "type1", "1").setSource("content", "Lorem ipsum dolor sit amet").get();
216+
client().prepareIndex("test", "type1", "2").setSource("content", "consectetur adipisicing elit").get();
217+
refresh();
218+
219+
// Execute search 5 times to load it in cache
220+
for (int i = 0; i < 5; i++) {
221+
SearchResponse searchResponse = client().prepareSearch()
222+
.setQuery(
223+
boolQuery()
224+
.minimumNumberShouldMatch(1)
225+
.should(queryString("dolor").queryName("dolor"))
226+
.should(queryString("elit").queryName("elit"))
227+
)
228+
.setPreference("_primary")
229+
.get();
230+
231+
assertHitCount(searchResponse, 2l);
232+
for (SearchHit hit : searchResponse.getHits()) {
233+
if (hit.id().equals("1")) {
234+
assertThat(hit.matchedQueries().length, equalTo(1));
235+
assertThat(hit.matchedQueries(), hasItemInArray("dolor"));
236+
} else if (hit.id().equals("2")) {
237+
assertThat(hit.matchedQueries().length, equalTo(1));
238+
assertThat(hit.matchedQueries(), hasItemInArray("elit"));
239+
} else {
240+
fail("Unexpected document returned with id " + hit.id());
241+
}
242+
}
243+
}
244+
}
206245
}

0 commit comments

Comments
 (0)