diff --git a/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryBuilder.java index 7343853c848..cb6c8d3743f 100644 --- a/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryBuilder.java @@ -38,7 +38,7 @@ public class FuzzyLikeThisFieldQueryBuilder extends BaseQueryBuilder implements private Integer maxQueryTerms; private Boolean ignoreTF; private String analyzer; - private boolean failOnUnsupportedField = true; + private Boolean failOnUnsupportedField; /** * A fuzzy more like this query on the provided field. @@ -124,7 +124,7 @@ public class FuzzyLikeThisFieldQueryBuilder extends BaseQueryBuilder implements if (analyzer != null) { builder.field("analyzer", analyzer); } - if (!failOnUnsupportedField) { + if (failOnUnsupportedField != null) { builder.field("fail_on_unsupported_field", failOnUnsupportedField); } builder.endObject(); diff --git a/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryBuilder.java index 44856254194..fb66c820d61 100644 --- a/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryBuilder.java @@ -38,7 +38,7 @@ public class FuzzyLikeThisQueryBuilder extends BaseQueryBuilder implements Boost private Integer maxQueryTerms; private Boolean ignoreTF; private String analyzer; - private boolean failOnUnsupportedField = true;; + private Boolean failOnUnsupportedField; /** * Constructs a new fuzzy like this query which uses the "_all" field. @@ -137,7 +137,7 @@ public class FuzzyLikeThisQueryBuilder extends BaseQueryBuilder implements Boost if (analyzer != null) { builder.field("analyzer", analyzer); } - if (!failOnUnsupportedField) { + if (failOnUnsupportedField != null) { builder.field("fail_on_unsupported_field", failOnUnsupportedField); } builder.endObject(); diff --git a/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryBuilder.java index bf5a3c18161..bbcac6a5b1a 100644 --- a/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryBuilder.java @@ -25,8 +25,6 @@ import java.io.IOException; /** * A more like this query that runs against a specific field. - * - * */ public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder { @@ -44,7 +42,7 @@ public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder implements B private float boostTerms = -1; private float boost = -1; private String analyzer; - private boolean failOnUnsupportedField; + private Boolean failOnUnsupportedField; /** * A more like this query that runs against a specific field. @@ -211,7 +209,7 @@ public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder implements B if (analyzer != null) { builder.field("analyzer", analyzer); } - if (!failOnUnsupportedField) { + if (failOnUnsupportedField != null) { builder.field("fail_on_unsupported_field", failOnUnsupportedField); } builder.endObject(); diff --git a/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java index adb68924401..c22c25a863b 100644 --- a/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java @@ -26,8 +26,6 @@ import java.io.IOException; /** * A more like this query that finds documents that are "like" the provided {@link #likeText(String)} * which is checked against the fields the query is constructed with. - * - * */ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder { @@ -45,7 +43,7 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements Boosta private float boostTerms = -1; private float boost = -1; private String analyzer; - private boolean failOnUnsupportedField = true; + private Boolean failOnUnsupportedField; /** * Constructs a new more like this query which uses the "_all" field. @@ -225,7 +223,7 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements Boosta if (analyzer != null) { builder.field("analyzer", analyzer); } - if (!failOnUnsupportedField) { + if (failOnUnsupportedField != null) { builder.field("fail_on_unsupported_field", failOnUnsupportedField); } builder.endObject(); diff --git a/src/test/java/org/elasticsearch/test/integration/flt/FuzzyLikeThisActionTests.java b/src/test/java/org/elasticsearch/test/integration/flt/FuzzyLikeThisActionTests.java index 80778986514..c8f98bd6548 100644 --- a/src/test/java/org/elasticsearch/test/integration/flt/FuzzyLikeThisActionTests.java +++ b/src/test/java/org/elasticsearch/test/integration/flt/FuzzyLikeThisActionTests.java @@ -28,9 +28,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.fuzzyLikeThisFieldQuery; import static org.elasticsearch.index.query.QueryBuilders.fuzzyLikeThisQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.fail; /** * @@ -61,22 +59,24 @@ public class FuzzyLikeThisActionTests extends AbstractSharedClusterTest { assertThat(searchResponse.getFailedShards(), equalTo(0)); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); - // flt query with at least a numeric field -> fail - try { - searchResponse = client().prepareSearch().setQuery(fuzzyLikeThisQuery("string_value", "int_value").likeText("index")).execute().actionGet(); - fail(); - } catch (SearchPhaseExecutionException e) { - // OK - } + // flt query with at least a numeric field -> fail by default + assertThrows(client().prepareSearch().setQuery(fuzzyLikeThisQuery("string_value", "int_value").likeText("index")), SearchPhaseExecutionException.class); + + // flt query with at least a numeric field -> fail by command + assertThrows(client().prepareSearch().setQuery(fuzzyLikeThisQuery("string_value", "int_value").likeText("index").failOnUnsupportedField(true)), SearchPhaseExecutionException.class); + // flt query with at least a numeric field but fail_on_unsupported_field set to false searchResponse = client().prepareSearch().setQuery(fuzzyLikeThisQuery("string_value", "int_value").likeText("index").failOnUnsupportedField(false)).execute().actionGet(); assertThat(searchResponse.getFailedShards(), equalTo(0)); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); - // flt field query on a numeric field -> failure + // flt field query on a numeric field -> failure by default assertThrows(client().prepareSearch().setQuery(fuzzyLikeThisFieldQuery("int_value").likeText("42")), SearchPhaseExecutionException.class); + // flt field query on a numeric field -> failure by command + assertThrows(client().prepareSearch().setQuery(fuzzyLikeThisFieldQuery("int_value").likeText("42").failOnUnsupportedField(true)), SearchPhaseExecutionException.class); + // flt field query on a numeric field but fail_on_unsupported_field set to false searchResponse = client().prepareSearch().setQuery(fuzzyLikeThisFieldQuery("int_value").likeText("42").failOnUnsupportedField(false)).execute().actionGet(); assertThat(searchResponse.getFailedShards(), equalTo(0)); diff --git a/src/test/java/org/elasticsearch/test/integration/mlt/MoreLikeThisActionTests.java b/src/test/java/org/elasticsearch/test/integration/mlt/MoreLikeThisActionTests.java index 11bf595a903..ab8923e643f 100644 --- a/src/test/java/org/elasticsearch/test/integration/mlt/MoreLikeThisActionTests.java +++ b/src/test/java/org/elasticsearch/test/integration/mlt/MoreLikeThisActionTests.java @@ -33,10 +33,9 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.FilterBuilders.termFilter; import static org.elasticsearch.index.query.QueryBuilders.moreLikeThisFieldQuery; import static org.elasticsearch.index.query.QueryBuilders.moreLikeThisQuery; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.fail; /** * @@ -172,12 +171,7 @@ public class MoreLikeThisActionTests extends AbstractSharedClusterTest { assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); // Explicit list of fields including numeric fields -> fail - try { - searchResponse = client().prepareMoreLikeThis("test", "type", "1").setField("string_value", "int_value").execute().actionGet(); - fail(); - } catch (SearchPhaseExecutionException e) { - // OK - } + assertThrows(client().prepareMoreLikeThis("test", "type", "1").setField("string_value", "int_value"), SearchPhaseExecutionException.class); // mlt query with no field -> OK searchResponse = client().prepareSearch().setQuery(moreLikeThisQuery().likeText("index").minTermFreq(1).minDocFreq(1)).execute().actionGet(); @@ -189,25 +183,24 @@ public class MoreLikeThisActionTests extends AbstractSharedClusterTest { assertThat(searchResponse.getFailedShards(), equalTo(0)); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); - // mlt query with at least a numeric field -> fail - try { - searchResponse = client().prepareSearch().setQuery(moreLikeThisQuery("string_value", "int_value").likeText("index")).execute().actionGet(); - fail(); - } catch (SearchPhaseExecutionException e) { - // OK - } + // mlt query with at least a numeric field -> fail by default + assertThrows(client().prepareSearch().setQuery(moreLikeThisQuery("string_value", "int_value").likeText("index")), SearchPhaseExecutionException.class); + + // mlt query with at least a numeric field -> fail by command + assertThrows(client().prepareSearch().setQuery(moreLikeThisQuery("string_value", "int_value").likeText("index").failOnUnsupportedField(true)), SearchPhaseExecutionException.class); + // mlt query with at least a numeric field but fail_on_unsupported_field set to false - searchResponse = client().prepareSearch().setQuery(moreLikeThisQuery("string_value", "int_value").likeText("index").minTermFreq(1).minDocFreq(1).failOnUnsupportedField(false)).execute().actionGet(); + searchResponse = client().prepareSearch().setQuery(moreLikeThisQuery("string_value", "int_value").likeText("index").minTermFreq(1).minDocFreq(1).failOnUnsupportedField(false)).get(); assertThat(searchResponse.getFailedShards(), equalTo(0)); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); - // mlt field query on a numeric field -> failure - try { - searchResponse = client().prepareSearch().setQuery(moreLikeThisFieldQuery("int_value").likeText("42").minTermFreq(1).minDocFreq(1)).execute().actionGet(); - } catch (SearchPhaseExecutionException e) { - // OK - } + // mlt field query on a numeric field -> failure by default + assertThrows(client().prepareSearch().setQuery(moreLikeThisFieldQuery("int_value").likeText("42").minTermFreq(1).minDocFreq(1)), SearchPhaseExecutionException.class); + + // mlt field query on a numeric field -> failure by command + assertThrows(client().prepareSearch().setQuery(moreLikeThisFieldQuery("int_value").likeText("42").minTermFreq(1).minDocFreq(1).failOnUnsupportedField(true)), + SearchPhaseExecutionException.class); // mlt field query on a numeric field but fail_on_unsupported_field set to false searchResponse = client().prepareSearch().setQuery(moreLikeThisFieldQuery("int_value").likeText("42").minTermFreq(1).minDocFreq(1).failOnUnsupportedField(false)).execute().actionGet();