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

Replaced exclude with include to avoid double negation #6275

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 5 additions & 2 deletions docs/reference/query-dsl/queries/mlt-query.asciidoc
Expand Up @@ -84,8 +84,11 @@ not specified.
`like_text` is not specified. The texts are fetched from `fields` unless
specified in each `doc`, and cannot be set to `_all`.

|`exclude` | coming[1.2.0] When using `ids`, specifies whether the documents should be
excluded from the search. Defaults to `true`.
|`include` | coming[1.2.0] When using `ids` or `docs`, specifies whether the documents should be
included from the search. Defaults to `false`.

|`exclude` | deprecated[1.3.0,Replaced by `include`] When using `ids` or `docs`, specifies whether
the documents should be excluded from the search. Defaults to `true`.

|`percent_terms_to_match` |The percentage of terms to match on (float
value). Defaults to `0.3` (30 percent).
Expand Down
Expand Up @@ -102,7 +102,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
private String likeText;
private List<String> ids = new ArrayList<>();
private List<Item> docs = new ArrayList<>();
private Boolean exclude = null;
private Boolean include = null;
private float percentTermsToMatch = -1;
private int minTermFreq = -1;
private int maxQueryTerms = -1;
Expand Down Expand Up @@ -156,8 +156,17 @@ public MoreLikeThisQueryBuilder addItem(Item item) {
return this;
}

public MoreLikeThisQueryBuilder include(boolean include) {
this.include = include;
return this;
}

/**
* @deprecated This is replaced by <code>include</code>.
*/
@Deprecated
public MoreLikeThisQueryBuilder exclude(boolean exclude) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an @Deprecated and point the the include method?

this.exclude = exclude;
this.include = !exclude;
return this;
}

Expand Down Expand Up @@ -336,8 +345,8 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
if (!docs.isEmpty()) {
builder.array("docs", docs.toArray());
}
if (exclude != null) {
builder.field("exclude", exclude);
if (include != null) {
builder.field("exclude", !include); // keep on using exclude for bw compat with 1.x
}
builder.endObject();
}
Expand Down
Expand Up @@ -65,6 +65,7 @@ public static class Fields {
public static final ParseField STOP_WORDS = new ParseField("stop_words");
public static final ParseField DOCUMENT_IDS = new ParseField("ids");
public static final ParseField DOCUMENTS = new ParseField("docs");
public static final ParseField INCLUDE = new ParseField("include");
public static final ParseField EXCLUDE = new ParseField("exclude");
}

Expand Down Expand Up @@ -92,7 +93,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
List<String> moreLikeFields = null;
boolean failOnUnsupportedField = true;
String queryName = null;
boolean exclude = true;
boolean include = false;

XContentParser.Token token;
String currentFieldName = null;
Expand Down Expand Up @@ -131,8 +132,10 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
failOnUnsupportedField = parser.booleanValue();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if (Fields.INCLUDE.match(currentFieldName, parseContext.parseFlags())) {
include = parser.booleanValue();
} else if (Fields.EXCLUDE.match(currentFieldName, parseContext.parseFlags())) {
exclude = parser.booleanValue();
include = !parser.booleanValue();
} else {
throw new QueryParsingException(parseContext.index(), "[mlt] query does not support [" + currentFieldName + "]");
}
Expand Down Expand Up @@ -212,7 +215,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
addMoreLikeThis(boolQuery, mltQuery, likeText.field, likeText.text);
}
// exclude the items from the search
if (exclude) {
if (!include) {
TermsFilter filter = new TermsFilter(UidFieldMapper.NAME, Uid.createUids(items));
ConstantScoreQuery query = new ConstantScoreQuery(filter);
boolQuery.add(query, BooleanClause.Occur.MUST_NOT);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/elasticsearch/index/query/mlt-ids.json
Expand Up @@ -14,7 +14,7 @@
}
],
"ids" : ["3", "4"],
"exclude" : false,
"include" : true,
"min_term_freq" : 1,
"max_query_terms" : 12
}
Expand Down
Expand Up @@ -367,7 +367,7 @@ public void testSimpleMoreLikeThisIds() throws Exception {
indexRandom(true, builders);

logger.info("Running MoreLikeThis");
MoreLikeThisQueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery("text").ids("1").exclude(false).minTermFreq(1).minDocFreq(1);
MoreLikeThisQueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery("text").ids("1").include(true).minTermFreq(1).minDocFreq(1);
SearchResponse mltResponse = client().prepareSearch().setTypes("type1").setQuery(queryBuilder).execute().actionGet();
assertHitCount(mltResponse, 3l);
}
Expand Down