More Like This Query: replaced 'exclude' with 'include' to avoid double negation when set.

Closes #6248
This commit is contained in:
Alex Ksikes 2014-05-20 16:28:09 +02:00
parent 8cca9b28df
commit a29b4a800d
5 changed files with 15 additions and 15 deletions

View File

@ -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).

View File

@ -102,7 +102,7 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements Boosta
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;
@ -156,8 +156,8 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements Boosta
return this;
}
public MoreLikeThisQueryBuilder exclude(boolean exclude) {
this.exclude = exclude;
public MoreLikeThisQueryBuilder include(boolean include) {
this.include = include;
return this;
}
@ -336,8 +336,8 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements Boosta
if (!docs.isEmpty()) {
builder.array("docs", docs.toArray());
}
if (exclude != null) {
builder.field("exclude", exclude);
if (include != null) {
builder.field("include", include);
}
builder.endObject();
}

View File

@ -65,7 +65,7 @@ public class MoreLikeThisQueryParser implements QueryParser {
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() {
@ -92,7 +92,7 @@ public class MoreLikeThisQueryParser implements QueryParser {
List<String> moreLikeFields = null;
boolean failOnUnsupportedField = true;
String queryName = null;
boolean exclude = true;
boolean include = false;
XContentParser.Token token;
String currentFieldName = null;
@ -131,8 +131,8 @@ public class MoreLikeThisQueryParser implements QueryParser {
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 + "]");
}
@ -212,7 +212,7 @@ public class MoreLikeThisQueryParser implements QueryParser {
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);

View File

@ -14,7 +14,7 @@
}
],
"ids" : ["3", "4"],
"exclude" : false,
"include" : true,
"min_term_freq" : 1,
"max_query_terms" : 12
}

View File

@ -367,7 +367,7 @@ public class MoreLikeThisActionTests extends ElasticsearchIntegrationTest {
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);
}
@ -472,7 +472,7 @@ public class MoreLikeThisActionTests extends ElasticsearchIntegrationTest {
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];