FuzzLikeThis*QueryBuilder and MoreLikeThis*QueryBuilder do not send failOnUnsupportedField if not explicitly specified.

Closes #3402 , a better solution to #3374
This commit is contained in:
Boaz Leskes 2013-07-29 15:02:42 +02:00
parent 7fd54acd66
commit e0e6a58357
6 changed files with 33 additions and 44 deletions

View File

@ -38,7 +38,7 @@ public class FuzzyLikeThisFieldQueryBuilder extends BaseQueryBuilder implements
private Integer maxQueryTerms; private Integer maxQueryTerms;
private Boolean ignoreTF; private Boolean ignoreTF;
private String analyzer; private String analyzer;
private boolean failOnUnsupportedField = true; private Boolean failOnUnsupportedField;
/** /**
* A fuzzy more like this query on the provided field. * A fuzzy more like this query on the provided field.
@ -124,7 +124,7 @@ public class FuzzyLikeThisFieldQueryBuilder extends BaseQueryBuilder implements
if (analyzer != null) { if (analyzer != null) {
builder.field("analyzer", analyzer); builder.field("analyzer", analyzer);
} }
if (!failOnUnsupportedField) { if (failOnUnsupportedField != null) {
builder.field("fail_on_unsupported_field", failOnUnsupportedField); builder.field("fail_on_unsupported_field", failOnUnsupportedField);
} }
builder.endObject(); builder.endObject();

View File

@ -38,7 +38,7 @@ public class FuzzyLikeThisQueryBuilder extends BaseQueryBuilder implements Boost
private Integer maxQueryTerms; private Integer maxQueryTerms;
private Boolean ignoreTF; private Boolean ignoreTF;
private String analyzer; private String analyzer;
private boolean failOnUnsupportedField = true;; private Boolean failOnUnsupportedField;
/** /**
* Constructs a new fuzzy like this query which uses the "_all" field. * 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) { if (analyzer != null) {
builder.field("analyzer", analyzer); builder.field("analyzer", analyzer);
} }
if (!failOnUnsupportedField) { if (failOnUnsupportedField != null) {
builder.field("fail_on_unsupported_field", failOnUnsupportedField); builder.field("fail_on_unsupported_field", failOnUnsupportedField);
} }
builder.endObject(); builder.endObject();

View File

@ -25,8 +25,6 @@ import java.io.IOException;
/** /**
* A more like this query that runs against a specific field. * A more like this query that runs against a specific field.
*
*
*/ */
public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<MoreLikeThisFieldQueryBuilder> { public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<MoreLikeThisFieldQueryBuilder> {
@ -44,7 +42,7 @@ public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder implements B
private float boostTerms = -1; private float boostTerms = -1;
private float boost = -1; private float boost = -1;
private String analyzer; private String analyzer;
private boolean failOnUnsupportedField; private Boolean failOnUnsupportedField;
/** /**
* A more like this query that runs against a specific field. * 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) { if (analyzer != null) {
builder.field("analyzer", analyzer); builder.field("analyzer", analyzer);
} }
if (!failOnUnsupportedField) { if (failOnUnsupportedField != null) {
builder.field("fail_on_unsupported_field", failOnUnsupportedField); builder.field("fail_on_unsupported_field", failOnUnsupportedField);
} }
builder.endObject(); builder.endObject();

View File

@ -26,8 +26,6 @@ import java.io.IOException;
/** /**
* A more like this query that finds documents that are "like" the provided {@link #likeText(String)} * 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. * which is checked against the fields the query is constructed with.
*
*
*/ */
public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<MoreLikeThisQueryBuilder> { public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<MoreLikeThisQueryBuilder> {
@ -45,7 +43,7 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements Boosta
private float boostTerms = -1; private float boostTerms = -1;
private float boost = -1; private float boost = -1;
private String analyzer; private String analyzer;
private boolean failOnUnsupportedField = true; private Boolean failOnUnsupportedField;
/** /**
* Constructs a new more like this query which uses the "_all" field. * 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) { if (analyzer != null) {
builder.field("analyzer", analyzer); builder.field("analyzer", analyzer);
} }
if (!failOnUnsupportedField) { if (failOnUnsupportedField != null) {
builder.field("fail_on_unsupported_field", failOnUnsupportedField); builder.field("fail_on_unsupported_field", failOnUnsupportedField);
} }
builder.endObject(); builder.endObject();

View File

@ -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.fuzzyLikeThisFieldQuery;
import static org.elasticsearch.index.query.QueryBuilders.fuzzyLikeThisQuery; import static org.elasticsearch.index.query.QueryBuilders.fuzzyLikeThisQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; 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.getFailedShards(), equalTo(0));
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
// flt query with at least a numeric field -> fail // flt query with at least a numeric field -> fail by default
try { assertThrows(client().prepareSearch().setQuery(fuzzyLikeThisQuery("string_value", "int_value").likeText("index")), SearchPhaseExecutionException.class);
searchResponse = client().prepareSearch().setQuery(fuzzyLikeThisQuery("string_value", "int_value").likeText("index")).execute().actionGet();
fail(); // flt query with at least a numeric field -> fail by command
} catch (SearchPhaseExecutionException e) { assertThrows(client().prepareSearch().setQuery(fuzzyLikeThisQuery("string_value", "int_value").likeText("index").failOnUnsupportedField(true)), SearchPhaseExecutionException.class);
// OK
}
// flt query with at least a numeric field but fail_on_unsupported_field set to false // 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(); searchResponse = client().prepareSearch().setQuery(fuzzyLikeThisQuery("string_value", "int_value").likeText("index").failOnUnsupportedField(false)).execute().actionGet();
assertThat(searchResponse.getFailedShards(), equalTo(0)); assertThat(searchResponse.getFailedShards(), equalTo(0));
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); 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); 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 // 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(); searchResponse = client().prepareSearch().setQuery(fuzzyLikeThisFieldQuery("int_value").likeText("42").failOnUnsupportedField(false)).execute().actionGet();
assertThat(searchResponse.getFailedShards(), equalTo(0)); assertThat(searchResponse.getFailedShards(), equalTo(0));

View File

@ -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.FilterBuilders.termFilter;
import static org.elasticsearch.index.query.QueryBuilders.moreLikeThisFieldQuery; import static org.elasticsearch.index.query.QueryBuilders.moreLikeThisFieldQuery;
import static org.elasticsearch.index.query.QueryBuilders.moreLikeThisQuery; 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.equalTo;
import static org.hamcrest.Matchers.notNullValue; 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)); assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
// Explicit list of fields including numeric fields -> fail // Explicit list of fields including numeric fields -> fail
try { assertThrows(client().prepareMoreLikeThis("test", "type", "1").setField("string_value", "int_value"), SearchPhaseExecutionException.class);
searchResponse = client().prepareMoreLikeThis("test", "type", "1").setField("string_value", "int_value").execute().actionGet();
fail();
} catch (SearchPhaseExecutionException e) {
// OK
}
// mlt query with no field -> OK // mlt query with no field -> OK
searchResponse = client().prepareSearch().setQuery(moreLikeThisQuery().likeText("index").minTermFreq(1).minDocFreq(1)).execute().actionGet(); 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.getFailedShards(), equalTo(0));
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
// mlt query with at least a numeric field -> fail // mlt query with at least a numeric field -> fail by default
try { assertThrows(client().prepareSearch().setQuery(moreLikeThisQuery("string_value", "int_value").likeText("index")), SearchPhaseExecutionException.class);
searchResponse = client().prepareSearch().setQuery(moreLikeThisQuery("string_value", "int_value").likeText("index")).execute().actionGet();
fail(); // mlt query with at least a numeric field -> fail by command
} catch (SearchPhaseExecutionException e) { assertThrows(client().prepareSearch().setQuery(moreLikeThisQuery("string_value", "int_value").likeText("index").failOnUnsupportedField(true)), SearchPhaseExecutionException.class);
// OK
}
// mlt query with at least a numeric field but fail_on_unsupported_field set to false // 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.getFailedShards(), equalTo(0));
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
// mlt field query on a numeric field -> failure // mlt field query on a numeric field -> failure by default
try { assertThrows(client().prepareSearch().setQuery(moreLikeThisFieldQuery("int_value").likeText("42").minTermFreq(1).minDocFreq(1)), SearchPhaseExecutionException.class);
searchResponse = client().prepareSearch().setQuery(moreLikeThisFieldQuery("int_value").likeText("42").minTermFreq(1).minDocFreq(1)).execute().actionGet();
} catch (SearchPhaseExecutionException e) { // mlt field query on a numeric field -> failure by command
// OK 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 // 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(); searchResponse = client().prepareSearch().setQuery(moreLikeThisFieldQuery("int_value").likeText("42").minTermFreq(1).minDocFreq(1).failOnUnsupportedField(false)).execute().actionGet();