Skip to content

Commit

Permalink
FuzzLikeThis*QueryBuilder and MoreLikeThis*QueryBuilder do not send f…
Browse files Browse the repository at this point in the history
…ailOnUnsupportedField if not explicitly specified.

Closes elastic#3402 , a better solution to elastic#3374
  • Loading branch information
bleskes committed Jul 29, 2013
1 parent f6df675 commit a44fa9f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 44 deletions.
Expand Up @@ -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.
Expand Down Expand Up @@ -124,7 +124,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
if (!failOnUnsupportedField) {
if (failOnUnsupportedField != null) {
builder.field("fail_on_unsupported_field", failOnUnsupportedField);
}
builder.endObject();
Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -137,7 +137,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
if (!failOnUnsupportedField) {
if (failOnUnsupportedField != null) {
builder.field("fail_on_unsupported_field", failOnUnsupportedField);
}
builder.endObject();
Expand Down
Expand Up @@ -25,8 +25,6 @@

/**
* A more like this query that runs against a specific field.
*
*
*/
public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<MoreLikeThisFieldQueryBuilder> {

Expand All @@ -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.
Expand Down Expand Up @@ -211,7 +209,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
if (!failOnUnsupportedField) {
if (failOnUnsupportedField != null) {
builder.field("fail_on_unsupported_field", failOnUnsupportedField);
}
builder.endObject();
Expand Down
Expand Up @@ -26,8 +26,6 @@
/**
* 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<MoreLikeThisQueryBuilder> {

Expand All @@ -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.
Expand Down Expand Up @@ -225,7 +223,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
if (!failOnUnsupportedField) {
if (failOnUnsupportedField != null) {
builder.field("fail_on_unsupported_field", failOnUnsupportedField);
}
builder.endObject();
Expand Down
Expand Up @@ -28,9 +28,7 @@
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;

/**
*
Expand Down Expand Up @@ -61,22 +59,24 @@ public void testNumericField() throws Exception {
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));
Expand Down
Expand Up @@ -33,10 +33,9 @@
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;

/**
*
Expand Down Expand Up @@ -172,12 +171,7 @@ public void testNumericField() throws Exception {
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();
Expand All @@ -189,25 +183,24 @@ public void testNumericField() throws Exception {
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();
Expand Down

0 comments on commit a44fa9f

Please sign in to comment.