Merge pull request #17204 from mstockerl/master

Alias items are not ignored anymore
This commit is contained in:
Britta Weber 2016-04-20 11:40:14 +02:00
commit 352dc19767
2 changed files with 35 additions and 23 deletions

View File

@ -1081,14 +1081,14 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
} }
// fetching the items with multi-termvectors API // fetching the items with multi-termvectors API
MultiTermVectorsResponse responses = fetchResponse(context.getClient(), likeItems, unlikeItems, SearchContext.current()); MultiTermVectorsResponse likeItemsResponse = fetchResponse(context.getClient(), likeItems);
// getting the Fields for liked items // getting the Fields for liked items
mltQuery.setLikeText(getFieldsFor(responses, likeItems)); mltQuery.setLikeText(getFieldsFor(likeItemsResponse));
// getting the Fields for unliked items // getting the Fields for unliked items
if (unlikeItems.length > 0) { if (unlikeItems.length > 0) {
org.apache.lucene.index.Fields[] unlikeFields = getFieldsFor(responses, unlikeItems); MultiTermVectorsResponse unlikeItemsResponse = fetchResponse(context.getClient(), unlikeItems);
org.apache.lucene.index.Fields[] unlikeFields = getFieldsFor(unlikeItemsResponse);
if (unlikeFields.length > 0) { if (unlikeFields.length > 0) {
mltQuery.setUnlikeText(unlikeFields); mltQuery.setUnlikeText(unlikeFields);
} }
@ -1127,30 +1127,19 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
} }
} }
private MultiTermVectorsResponse fetchResponse(Client client, Item[] likeItems, @Nullable Item[] unlikeItems, private MultiTermVectorsResponse fetchResponse(Client client, Item[] items) throws IOException {
SearchContext searchContext) throws IOException {
MultiTermVectorsRequest request = new MultiTermVectorsRequest(); MultiTermVectorsRequest request = new MultiTermVectorsRequest();
for (Item item : likeItems) { for (Item item : items) {
request.add(item.toTermVectorsRequest());
}
for (Item item : unlikeItems) {
request.add(item.toTermVectorsRequest()); request.add(item.toTermVectorsRequest());
} }
return client.multiTermVectors(request).actionGet(); return client.multiTermVectors(request).actionGet();
} }
private static Fields[] getFieldsFor(MultiTermVectorsResponse responses, Item[] items) throws IOException { private static Fields[] getFieldsFor(MultiTermVectorsResponse responses) throws IOException {
List<Fields> likeFields = new ArrayList<>(); List<Fields> likeFields = new ArrayList<>();
Set<Item> selectedItems = new HashSet<>();
for (Item request : items) {
selectedItems.add(new Item(request.index(), request.type(), request.id()));
}
for (MultiTermVectorsItemResponse response : responses) { for (MultiTermVectorsItemResponse response : responses) {
if (!hasResponseFromRequest(response, selectedItems)) {
continue;
}
if (response.isFailed()) { if (response.isFailed()) {
continue; continue;
} }
@ -1163,10 +1152,6 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
return likeFields.toArray(Fields.EMPTY_ARRAY); return likeFields.toArray(Fields.EMPTY_ARRAY);
} }
private static boolean hasResponseFromRequest(MultiTermVectorsItemResponse response, Set<Item> selectedItems) {
return selectedItems.contains(new Item(response.getIndex(), response.getType(), response.getId()));
}
private static void handleExclude(BooleanQuery.Builder boolQuery, Item[] likeItems) { private static void handleExclude(BooleanQuery.Builder boolQuery, Item[] likeItems) {
// artificial docs get assigned a random id and should be disregarded // artificial docs get assigned a random id and should be disregarded
List<BytesRef> uids = new ArrayList<>(); List<BytesRef> uids = new ArrayList<>();

View File

@ -145,6 +145,32 @@ public class MoreLikeThisIT extends ESIntegTestCase {
assertThat(response.getHits().getAt(0).id(), equalTo("3")); assertThat(response.getHits().getAt(0).id(), equalTo("3"));
} }
// Issue #14944
public void testMoreLikeThisWithAliasesInLikeDocuments() throws Exception {
String indexName = "foo";
String aliasName = "foo_name";
String typeName = "bar";
String mapping = XContentFactory.jsonBuilder().startObject().startObject("bar")
.startObject("properties")
.endObject()
.endObject().endObject().string();
client().admin().indices().prepareCreate(indexName).addMapping(typeName, mapping).execute().actionGet();
client().admin().indices().aliases(indexAliasesRequest().addAlias(aliasName, indexName)).actionGet();
assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN));
client().index(indexRequest(indexName).type(typeName).id("1").source(jsonBuilder().startObject().field("text", "elasticsearch index").endObject())).actionGet();
client().index(indexRequest(indexName).type(typeName).id("2").source(jsonBuilder().startObject().field("text", "lucene index").endObject())).actionGet();
client().index(indexRequest(indexName).type(typeName).id("3").source(jsonBuilder().startObject().field("text", "elasticsearch index").endObject())).actionGet();
refresh(indexName);
SearchResponse response = client().prepareSearch().setQuery(
new MoreLikeThisQueryBuilder(null, new Item[] {new Item(aliasName, typeName, "1")}).minTermFreq(1).minDocFreq(1)).get();
assertHitCount(response, 2L);
assertThat(response.getHits().getAt(0).id(), equalTo("3"));
}
public void testMoreLikeThisIssue2197() throws Exception { public void testMoreLikeThisIssue2197() throws Exception {
Client client = client(); Client client = client();
String mapping = XContentFactory.jsonBuilder().startObject().startObject("bar") String mapping = XContentFactory.jsonBuilder().startObject().startObject("bar")
@ -564,6 +590,7 @@ public class MoreLikeThisIT extends ESIntegTestCase {
.maxQueryTerms(100) .maxQueryTerms(100)
.include(true) .include(true)
.minimumShouldMatch("0%"); .minimumShouldMatch("0%");
response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get(); response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
assertSearchResponse(response); assertSearchResponse(response);
assertHitCount(response, numFields - (i + 1)); assertHitCount(response, numFields - (i + 1));