Skip to content

Commit 2546c06

Browse files
committed
More Like This Query: allow for both 'like_text' and 'docs/ids' to be specified.
Closes elastic#6246
1 parent a717af5 commit 2546c06

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

docs/reference/query-dsl/queries/mlt-query.asciidoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The `more_like_this` top level parameters include:
7474
|`fields` |A list of the fields to run the more like this query against.
7575
Defaults to the `_all` field.
7676

77-
|`like_text` |The text to find documents like it, *required* if `ids` is
77+
|`like_text` |The text to find documents like it, *required* if `ids` or `docs` are
7878
not specified.
7979

8080
|`ids` or `docs` |A list of documents following the same syntax as the

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
158158
}
159159
}
160160

161-
if ((mltQuery.getLikeText() == null && items.isEmpty()) || (mltQuery.getLikeText() != null && !items.isEmpty())) {
162-
throw new QueryParsingException(parseContext.index(), "more_like_this requires either 'like_text' or 'ids/docs' to be specified");
161+
if (mltQuery.getLikeText() == null && items.isEmpty()) {
162+
throw new QueryParsingException(parseContext.index(), "more_like_this requires at least 'like_text' or 'ids/docs' to be specified");
163163
}
164164

165165
if (analyzer == null) {
@@ -217,6 +217,10 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
217217
ConstantScoreQuery query = new ConstantScoreQuery(filter);
218218
boolQuery.add(query, BooleanClause.Occur.MUST_NOT);
219219
}
220+
// add the possible mlt query with like_text
221+
if (mltQuery.getLikeText() != null) {
222+
boolQuery.add(mltQuery, BooleanClause.Occur.SHOULD);
223+
}
220224
return boolQuery;
221225
}
222226

src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -1696,16 +1696,25 @@ public void testMoreLikeThisIds() throws Exception {
16961696
Query parsedQuery = queryParser.parse(query).query();
16971697
assertThat(parsedQuery, instanceOf(BooleanQuery.class));
16981698
BooleanQuery booleanQuery = (BooleanQuery) parsedQuery;
1699-
assertThat(booleanQuery.getClauses().length, is(likeTexts.size()));
1699+
assertThat(booleanQuery.getClauses().length, is(likeTexts.size() + 1));
17001700

1701+
// check each clause is for each item
1702+
BooleanClause[] boolClauses = booleanQuery.getClauses();
17011703
for (int i=0; i<likeTexts.size(); i++) {
1702-
BooleanClause booleanClause = booleanQuery.getClauses()[i];
1703-
assertThat(booleanClause.getOccur(), is(BooleanClause.Occur.SHOULD));
1704-
assertThat(booleanClause.getQuery(), instanceOf(MoreLikeThisQuery.class));
1705-
MoreLikeThisQuery mltQuery = (MoreLikeThisQuery) booleanClause.getQuery();
1704+
assertThat(boolClauses[i].getOccur(), is(BooleanClause.Occur.SHOULD));
1705+
assertThat(boolClauses[i].getQuery(), instanceOf(MoreLikeThisQuery.class));
1706+
MoreLikeThisQuery mltQuery = (MoreLikeThisQuery) boolClauses[i].getQuery();
17061707
assertThat(mltQuery.getLikeText(), is(likeTexts.get(i).text));
17071708
assertThat(mltQuery.getMoreLikeFields()[0], equalTo(likeTexts.get(i).field));
17081709
}
1710+
1711+
// check last clause is for 'like_text'
1712+
BooleanClause boolClause = boolClauses[boolClauses.length - 1];
1713+
assertThat(boolClause.getOccur(), is(BooleanClause.Occur.SHOULD));
1714+
assertThat(boolClause.getQuery(), instanceOf(MoreLikeThisQuery.class));
1715+
MoreLikeThisQuery mltQuery = (MoreLikeThisQuery) boolClause.getQuery();
1716+
assertArrayEquals("Not the same more like this 'fields'", new String[] {"name.first", "name.last"}, mltQuery.getMoreLikeFields());
1717+
assertThat(mltQuery.getLikeText(), equalTo("Apache Lucene"));
17091718
}
17101719

17111720
private static class MockMoreLikeThisFetchService extends MoreLikeThisFetchService {

src/test/java/org/elasticsearch/index/query/mlt-ids.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
more_like_this:{
33
"fields" : ["name.first", "name.last"],
4+
"like_text": "Apache Lucene",
45
"docs" : [
56
{
67
"_index" : "test",

0 commit comments

Comments
 (0)