From dae48d9fe8b0babc2be87547ded64dbc08d335ba Mon Sep 17 00:00:00 2001 From: Alex Ksikes Date: Tue, 6 May 2014 16:19:23 +0200 Subject: [PATCH] Added the ability to include the queried document for More Like This API. By default More Like This API excludes the queried document from the response. However, when debugging or when comparing scores across different queries, it could be useful to have the best possible matched hit. So this option lets users explicitly specify the desired behavior. Closes #6067 --- docs/reference/search/more-like-this.asciidoc | 3 ++ .../action/mlt/MoreLikeThisRequest.java | 26 ++++++++++++++ .../mlt/MoreLikeThisRequestBuilder.java | 8 +++++ .../mlt/TransportMoreLikeThisAction.java | 8 +++-- .../action/mlt/RestMoreLikeThisAction.java | 1 + .../mlt/MoreLikeThisActionTests.java | 36 +++++++++++++++++++ 6 files changed, 79 insertions(+), 3 deletions(-) diff --git a/docs/reference/search/more-like-this.asciidoc b/docs/reference/search/more-like-this.asciidoc index 25e8158c461..ffdad3a4d3d 100644 --- a/docs/reference/search/more-like-this.asciidoc +++ b/docs/reference/search/more-like-this.asciidoc @@ -25,5 +25,8 @@ Rest parameters relating to search are also allowed, including When no `mlt_fields` are specified, all the fields of the document will be used in the `more_like_this` query generated. +By default, the queried document is excluded from the response (`include` +set to false). + Note: In order to use the `mlt` feature a `mlt_field` needs to be either be `stored`, store `term_vector` or `source` needs to be enabled. diff --git a/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java b/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java index 3da63a051fe..5fa6bba4c55 100644 --- a/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java +++ b/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java @@ -21,6 +21,7 @@ package org.elasticsearch.action.mlt; import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.ElasticsearchIllegalArgumentException; +import org.elasticsearch.Version; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ValidateActions; @@ -72,6 +73,7 @@ public class MoreLikeThisRequest extends ActionRequest { private int minWordLength = -1; private int maxWordLength = -1; private float boostTerms = -1; + private boolean include = false; private SearchType searchType = SearchType.DEFAULT; private int searchSize = 0; @@ -313,6 +315,21 @@ public class MoreLikeThisRequest extends ActionRequest { return this.boostTerms; } + /** + * Whether to include the queried document. Defaults to false. + */ + public MoreLikeThisRequest include(boolean include) { + this.include = include; + return this; + } + + /** + * Whether to include the queried document. Defaults to false. + */ + public boolean include() { + return this.include; + } + void beforeLocalFork() { if (searchSourceUnsafe) { searchSource = searchSource.copyBytesArray(); @@ -553,6 +570,12 @@ public class MoreLikeThisRequest extends ActionRequest { minWordLength = in.readVInt(); maxWordLength = in.readVInt(); boostTerms = in.readFloat(); + if (in.getVersion().onOrAfter(Version.V_1_2_0)) { + include = in.readBoolean(); + } else { + include = false; // hard-coded behavior until Elasticsearch 1.2 + } + searchType = SearchType.fromId(in.readByte()); if (in.readBoolean()) { searchQueryHint = in.readString(); @@ -622,6 +645,9 @@ public class MoreLikeThisRequest extends ActionRequest { out.writeVInt(minWordLength); out.writeVInt(maxWordLength); out.writeFloat(boostTerms); + if (out.getVersion().onOrAfter(Version.V_1_2_0)) { + out.writeBoolean(include); + } out.writeByte(searchType.id()); if (searchQueryHint == null) { diff --git a/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequestBuilder.java b/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequestBuilder.java index cbeeb386d97..32300f41666 100644 --- a/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequestBuilder.java +++ b/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequestBuilder.java @@ -140,6 +140,14 @@ public class MoreLikeThisRequestBuilder extends ActionRequestBuilderfalse. + */ + public MoreLikeThisRequestBuilder setInclude(boolean include) { + request.include(include); + return this; + } + /** * An optional search source request allowing to control the search request for the * more like this documents. diff --git a/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java b/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java index 77782f10124..705e00c23c6 100644 --- a/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java +++ b/src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java @@ -178,9 +178,11 @@ public class TransportMoreLikeThisAction extends TransportAction