Added support for _name parameter in indices filter

Closes #4166
This commit is contained in:
Luca Cavanna 2013-11-14 16:24:15 +01:00
parent 4670a2e514
commit 6945f294e7
2 changed files with 52 additions and 2 deletions

View File

@ -60,6 +60,7 @@ public class IndicesFilterParser implements FilterParser {
boolean filterFound = false;
boolean indicesFound = false;
boolean currentIndexMatchesIndices = false;
String filterName = null;
String currentFieldName = null;
XContentParser.Token token;
@ -116,6 +117,8 @@ public class IndicesFilterParser implements FilterParser {
} else if ("none".equals(type)) {
noMatchFilter = Queries.MATCH_NO_FILTER;
}
} else if ("_name".equals(currentFieldName)) {
filterName = parser.text();
} else {
throw new QueryParsingException(parseContext.index(), "[indices] filter does not support [" + currentFieldName + "]");
}
@ -128,10 +131,18 @@ public class IndicesFilterParser implements FilterParser {
throw new QueryParsingException(parseContext.index(), "[indices] requires 'indices' or 'index' element");
}
Filter chosenFilter;
if (currentIndexMatchesIndices) {
return filter;
chosenFilter = filter;
} else {
chosenFilter = noMatchFilter;
}
return noMatchFilter;
if (filterName != null) {
parseContext.addNamedFilter(filterName, chosenFilter);
}
return chosenFilter;
}
protected boolean matchesIndices(String currentIndex, String... indices) {

View File

@ -164,4 +164,43 @@ public class MatchedQueriesTests extends ElasticsearchIntegrationTest {
}
}
}
@Test
public void testIndicesFilterSupportsName() {
createIndex("test1", "test2");
ensureGreen();
client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
client().prepareIndex("test2", "type1", "2").setSource("title", "title2").get();
client().prepareIndex("test2", "type1", "3").setSource("title", "title3").get();
refresh();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(filteredQuery(matchAllQuery(),
orFilter(
indicesFilter(termFilter("title", "title1").filterName("title1"), "test1")
.noMatchFilter(termFilter("title", "title2").filterName("title2")).filterName("indices_filter"),
termFilter("title", "title3").filterName("title3")).filterName("or"))).get();
assertHitCount(searchResponse, 3l);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.id().equals("1")) {
assertThat(hit.matchedQueries().length, equalTo(3));
assertThat(hit.matchedQueries(), hasItemInArray("indices_filter"));
assertThat(hit.matchedQueries(), hasItemInArray("title1"));
assertThat(hit.matchedQueries(), hasItemInArray("or"));
} else if (hit.id().equals("2")) {
assertThat(hit.matchedQueries().length, equalTo(3));
assertThat(hit.matchedQueries(), hasItemInArray("indices_filter"));
assertThat(hit.matchedQueries(), hasItemInArray("title2"));
assertThat(hit.matchedQueries(), hasItemInArray("or"));
} else if (hit.id().equals("3")) {
assertThat(hit.matchedQueries().length, equalTo(2));
assertThat(hit.matchedQueries(), hasItemInArray("title3"));
assertThat(hit.matchedQueries(), hasItemInArray("or"));
} else {
fail("Unexpected document returned with id " + hit.id());
}
}
}
}