diff --git a/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java index 901afd9fdd0..1e03ae20d45 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java @@ -1081,14 +1081,14 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder 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) { mltQuery.setUnlikeText(unlikeFields); } @@ -1127,30 +1127,19 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder likeFields = new ArrayList<>(); - Set selectedItems = new HashSet<>(); - for (Item request : items) { - selectedItems.add(new Item(request.index(), request.type(), request.id())); - } - for (MultiTermVectorsItemResponse response : responses) { - if (!hasResponseFromRequest(response, selectedItems)) { - continue; - } if (response.isFailed()) { continue; } @@ -1163,10 +1152,6 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder selectedItems) { - return selectedItems.contains(new Item(response.getIndex(), response.getType(), response.getId())); - } - private static void handleExclude(BooleanQuery.Builder boolQuery, Item[] likeItems) { // artificial docs get assigned a random id and should be disregarded List uids = new ArrayList<>(); diff --git a/core/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java b/core/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java index 31accf42962..f6277658230 100644 --- a/core/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java +++ b/core/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java @@ -145,6 +145,32 @@ public class MoreLikeThisIT extends ESIntegTestCase { 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 { Client client = client(); String mapping = XContentFactory.jsonBuilder().startObject().startObject("bar") @@ -564,6 +590,7 @@ public class MoreLikeThisIT extends ESIntegTestCase { .maxQueryTerms(100) .include(true) .minimumShouldMatch("0%"); + response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get(); assertSearchResponse(response); assertHitCount(response, numFields - (i + 1));