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 #6248

Merged
merged 1 commit into from May 21, 2014
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
4 changes: 2 additions & 2 deletions docs/reference/query-dsl/queries/mlt-query.asciidoc
Expand Up @@ -82,8 +82,8 @@ 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` |When using `ids`, specifies whether the documents should be
excluded from the search. Defaults to `true`.
|`include` |When using `ids` or `docs`, specifies whether the documents should be
included from the search. Defaults to `false`.

|`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,8 @@ public MoreLikeThisQueryBuilder addItem(Item item) {
return this;
}

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

Expand Down Expand Up @@ -336,8 +336,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("include", include);
}
builder.endObject();
}
Expand Down
Expand Up @@ -65,7 +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 EXCLUDE = new ParseField("exclude");
public static final ParseField INCLUDE = new ParseField("include");
}

public MoreLikeThisQueryParser() {
Expand All @@ -92,7 +92,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 +131,8 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
failOnUnsupportedField = parser.booleanValue();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if (Fields.EXCLUDE.match(currentFieldName, parseContext.parseFlags())) {
exclude = parser.booleanValue();
} else if (Fields.INCLUDE.match(currentFieldName, parseContext.parseFlags())) {
include = parser.booleanValue();
} else {
throw new QueryParsingException(parseContext.index(), "[mlt] query does not support [" + currentFieldName + "]");
}
Expand Down Expand Up @@ -212,7 +212,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 Expand Up @@ -472,7 +472,7 @@ public void testSimpleMoreLikeThisIdsMultipleTypes() throws Exception {
indexRandom(true, builders);

logger.info("Running MoreLikeThis");
MoreLikeThisQueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery("text").exclude(false).minTermFreq(1).minDocFreq(1)
MoreLikeThisQueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery("text").include(true).minTermFreq(1).minDocFreq(1)
.addItem(new MoreLikeThisQueryBuilder.Item("test", "type0", "0"));

String[] types = new String[numOfTypes];
Expand Down