diff options
author | Luca Cavanna <javanna@users.noreply.github.com> | 2017-05-15 18:15:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-15 18:15:42 +0200 |
commit | 563e7ddc830c991dea210f5ed936b06f5f613ac7 (patch) | |
tree | cb836f8e0285198c39b078ab729629bc0e45df87 | |
parent | e14ba81ac1bf4c576f96e62fbe1c6f6024b10f5b (diff) |
Pass over _routing value with more_like_this items to be retrieved (#24679)
When retrieving documents to extract terms from as part of a more like this query, the _routing value can be set, yet it gets lost. That leads to not being able to retrieve the documents, hence more_like_this used to return no matches all the time.
Closes #23699
4 files changed, 35 insertions, 9 deletions
diff --git a/core/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java b/core/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java index 81a8dfdd1e..28971fc9ca 100644 --- a/core/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java +++ b/core/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java @@ -224,10 +224,6 @@ public class MoreLikeThisQuery extends Query { return likeText; } - public void setLikeText(String likeText) { - setLikeText(new String[]{likeText}); - } - public void setLikeText(String... likeText) { this.likeText = likeText; } @@ -236,7 +232,7 @@ public class MoreLikeThisQuery extends Query { return likeFields; } - public void setLikeText(Fields... likeFields) { + public void setLikeFields(Fields... likeFields) { this.likeFields = likeFields; } @@ -244,7 +240,7 @@ public class MoreLikeThisQuery extends Query { setLikeText(likeText.toArray(Strings.EMPTY_ARRAY)); } - public void setUnlikeText(Fields... unlikeFields) { + public void setUnlikeFields(Fields... unlikeFields) { this.unlikeFields = unlikeFields; } 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 fd83465b7e..553adf88b7 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java @@ -178,6 +178,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ this.index = copy.index; this.type = copy.type; this.id = copy.id; + this.routing = copy.routing; this.doc = copy.doc; this.xContentType = copy.xContentType; this.fields = copy.fields; @@ -343,7 +344,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ /** * Convert this to a {@link TermVectorsRequest} for fetching the terms of the document. */ - public TermVectorsRequest toTermVectorsRequest() { + TermVectorsRequest toTermVectorsRequest() { TermVectorsRequest termVectorsRequest = new TermVectorsRequest(index, type, id) .selectedFields(fields) .routing(routing) @@ -1085,14 +1086,14 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ // fetching the items with multi-termvectors API MultiTermVectorsResponse likeItemsResponse = fetchResponse(context.getClient(), likeItems); // getting the Fields for liked items - mltQuery.setLikeText(getFieldsFor(likeItemsResponse)); + mltQuery.setLikeFields(getFieldsFor(likeItemsResponse)); // getting the Fields for unliked items if (unlikeItems.length > 0) { MultiTermVectorsResponse unlikeItemsResponse = fetchResponse(context.getClient(), unlikeItems); org.apache.lucene.index.Fields[] unlikeFields = getFieldsFor(unlikeItemsResponse); if (unlikeFields.length > 0) { - mltQuery.setUnlikeText(unlikeFields); + mltQuery.setUnlikeFields(unlikeFields); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java index c321ffa965..d19e8e32ff 100644 --- a/core/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.lucene.index.Fields; import org.apache.lucene.index.MultiFields; import org.apache.lucene.index.memory.MemoryIndex; +import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Query; import org.elasticsearch.ElasticsearchException; @@ -61,6 +62,7 @@ import java.util.stream.Stream; import static org.elasticsearch.index.query.QueryBuilders.moreLikeThisQuery; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.instanceOf; public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLikeThisQueryBuilder> { @@ -264,6 +266,13 @@ public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLik protected void doAssertLuceneQuery(MoreLikeThisQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException { if (queryBuilder.likeItems() != null && queryBuilder.likeItems().length > 0) { assertThat(query, instanceOf(BooleanQuery.class)); + BooleanQuery booleanQuery = (BooleanQuery) query; + for (BooleanClause booleanClause : booleanQuery) { + if (booleanClause.getQuery() instanceof MoreLikeThisQuery) { + MoreLikeThisQuery moreLikeThisQuery = (MoreLikeThisQuery) booleanClause.getQuery(); + assertThat(moreLikeThisQuery.getLikeFields().length, greaterThan(0)); + } + } } else { // we rely on integration tests for a deeper check here assertThat(query, instanceOf(MoreLikeThisQuery.class)); @@ -310,6 +319,12 @@ public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLik assertEquals(expectedItem, newItem); } + public void testItemCopy() throws IOException { + Item expectedItem = generateRandomItem(); + Item newItem = new Item(expectedItem); + assertEquals(expectedItem, newItem); + } + public void testItemFromXContent() throws IOException { Item expectedItem = generateRandomItem(); String json = expectedItem.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).string(); 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 0d2c5cf1bd..8d4f2921f2 100644 --- a/core/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java +++ b/core/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java @@ -623,4 +623,18 @@ public class MoreLikeThisIT extends ESIntegTestCase { assertSearchResponse(response); assertHitCount(response, 1); } + + public void testWithRouting() throws IOException { + client().prepareIndex("index", "type", "1").setRouting("3").setSource("text", "this is a document").get(); + client().prepareIndex("index", "type", "2").setRouting("1").setSource("text", "this is another document").get(); + client().prepareIndex("index", "type", "3").setRouting("4").setSource("text", "this is yet another document").get(); + refresh("index"); + + Item item = new Item("index", "type", "2").routing("1"); + MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = new MoreLikeThisQueryBuilder(new String[]{"text"}, null, new Item[]{item}); + moreLikeThisQueryBuilder.minTermFreq(1); + moreLikeThisQueryBuilder.minDocFreq(1); + SearchResponse searchResponse = client().prepareSearch("index").setQuery(moreLikeThisQueryBuilder).get(); + assertEquals(2, searchResponse.getHits().totalHits); + } } |