Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for both like_text and docs/ids to be specified. #6246

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference/query-dsl/queries/mlt-query.asciidoc
Expand Up @@ -74,7 +74,7 @@ The `more_like_this` top level parameters include:
|`fields` |A list of the fields to run the more like this query against.
Defaults to the `_all` field.

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

|`ids` or `docs` |A list of documents following the same syntax as the
Expand Down
Expand Up @@ -158,8 +158,8 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
}

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

if (analyzer == null) {
Expand Down Expand Up @@ -217,6 +217,10 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
ConstantScoreQuery query = new ConstantScoreQuery(filter);
boolQuery.add(query, BooleanClause.Occur.MUST_NOT);
}
// add the possible mlt query with like_text
if (mltQuery.getLikeText() != null) {
boolQuery.add(mltQuery, BooleanClause.Occur.SHOULD);
}
return boolQuery;
}

Expand Down
Expand Up @@ -1696,16 +1696,25 @@ public void testMoreLikeThisIds() throws Exception {
Query parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) parsedQuery;
assertThat(booleanQuery.getClauses().length, is(likeTexts.size()));
assertThat(booleanQuery.getClauses().length, is(likeTexts.size() + 1));

// check each clause is for each item
BooleanClause[] boolClauses = booleanQuery.getClauses();
for (int i=0; i<likeTexts.size(); i++) {
BooleanClause booleanClause = booleanQuery.getClauses()[i];
assertThat(booleanClause.getOccur(), is(BooleanClause.Occur.SHOULD));
assertThat(booleanClause.getQuery(), instanceOf(MoreLikeThisQuery.class));
MoreLikeThisQuery mltQuery = (MoreLikeThisQuery) booleanClause.getQuery();
assertThat(boolClauses[i].getOccur(), is(BooleanClause.Occur.SHOULD));
assertThat(boolClauses[i].getQuery(), instanceOf(MoreLikeThisQuery.class));
MoreLikeThisQuery mltQuery = (MoreLikeThisQuery) boolClauses[i].getQuery();
assertThat(mltQuery.getLikeText(), is(likeTexts.get(i).text));
assertThat(mltQuery.getMoreLikeFields()[0], equalTo(likeTexts.get(i).field));
}

// check last clause is for 'like_text'
BooleanClause boolClause = boolClauses[boolClauses.length - 1];
assertThat(boolClause.getOccur(), is(BooleanClause.Occur.SHOULD));
assertThat(boolClause.getQuery(), instanceOf(MoreLikeThisQuery.class));
MoreLikeThisQuery mltQuery = (MoreLikeThisQuery) boolClause.getQuery();
assertArrayEquals("Not the same more like this 'fields'", new String[] {"name.first", "name.last"}, mltQuery.getMoreLikeFields());
assertThat(mltQuery.getLikeText(), equalTo("Apache Lucene"));
}

private static class MockMoreLikeThisFetchService extends MoreLikeThisFetchService {
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/elasticsearch/index/query/mlt-ids.json
@@ -1,6 +1,7 @@
{
more_like_this:{
"fields" : ["name.first", "name.last"],
"like_text": "Apache Lucene",
"docs" : [
{
"_index" : "test",
Expand Down