Skip to content

Commit

Permalink
FuzzyLikeThisFieldQueryBuilder now defaults failOnUnsupportedField to…
Browse files Browse the repository at this point in the history
… true, to be consistent with REST API.

Closes #3374
  • Loading branch information
bleskes committed Jul 24, 2013
1 parent 380c7d1 commit 88a9ba7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 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;
private boolean failOnUnsupportedField = true;

/**
* A fuzzy more like this query on the provided field.
Expand Down
Expand Up @@ -18,20 +18,23 @@
*/
package org.elasticsearch.test.hamcrest;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import java.util.Arrays;

import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.suggest.Suggest;
import org.hamcrest.Matcher;

import java.util.Arrays;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

/**
*
*/
Expand Down Expand Up @@ -63,14 +66,14 @@ public static void assertThirdHit(SearchResponse searchResponse, Matcher<SearchH
public static void assertSearchHit(SearchResponse searchResponse, int number, Matcher<SearchHit> matcher) {
assert number > 0;
assertThat("SearchHit number must be greater than 0", number, greaterThan(0));
assertThat(searchResponse.getHits().totalHits(), greaterThanOrEqualTo((long)number));
assertSearchHit(searchResponse.getHits().getAt(number-1), matcher);
assertThat(searchResponse.getHits().totalHits(), greaterThanOrEqualTo((long) number));
assertSearchHit(searchResponse.getHits().getAt(number - 1), matcher);
}

public static void assertNoFailures(SearchResponse searchResponse) {
assertThat("Unexpectd ShardFailures: " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0));
}

public static void assertNoFailures(BroadcastOperationResponse response) {
assertThat("Unexpectd ShardFailures: " + Arrays.toString(response.getShardFailures()), response.getFailedShards(), equalTo(0));
}
Expand All @@ -86,16 +89,16 @@ public static void assertHighlight(SearchResponse resp, int hit, String field, i
assertThat(resp.getHits().hits()[hit].getHighlightFields().get(field).fragments().length, greaterThan(fragment));
assertThat(resp.getHits().hits()[hit].highlightFields().get(field).fragments()[fragment].string(), matcher);
}

public static void assertSuggestionSize(Suggest searchSuggest, int entry, int size, String key) {
assertThat(searchSuggest, notNullValue());
assertThat(searchSuggest.size(),greaterThanOrEqualTo(1));
assertThat(searchSuggest.size(), greaterThanOrEqualTo(1));
assertThat(searchSuggest.getSuggestion(key).getName(), equalTo(key));
assertThat(searchSuggest.getSuggestion(key).getEntries().size(), greaterThanOrEqualTo(entry));
assertThat(searchSuggest.getSuggestion(key).getEntries().get(entry).getOptions().size(), equalTo(size));

}

public static void assertSuggestion(Suggest searchSuggest, int entry, int ord, String key, String text) {
assertThat(searchSuggest, notNullValue());
assertThat(searchSuggest.size(), greaterThanOrEqualTo(1));
Expand All @@ -119,13 +122,34 @@ public static Matcher<SearchHit> hasType(final String type) {
public static Matcher<SearchHit> hasIndex(final String index) {
return new ElasticsearchMatchers.SearchHitHasIndexMatcher(index);
}

public static <T extends Query> T assertBooleanSubQuery(Query query, Class<T> subqueryType, int i) {
assertThat(query, instanceOf(BooleanQuery.class));
BooleanQuery q = (BooleanQuery) query;
assertThat(q.getClauses().length, greaterThan(i));
assertThat(q.getClauses()[i].getQuery(), instanceOf(subqueryType));
return (T)q.getClauses()[i].getQuery();
return (T) q.getClauses()[i].getQuery();
}

public static <E extends Throwable> void assertThrows(ActionRequestBuilder<?, ?, ?> builder, Class<E> exceptionClass) {
assertThrows(builder.execute(), exceptionClass);
}

public static <E extends Throwable> void assertThrows(ActionFuture future, Class<E> exceptionClass) {
boolean fail = false;
try {
future.actionGet();
fail = true;

} catch (ElasticSearchException esException) {
assertThat(esException.unwrapCause(), instanceOf(exceptionClass));
} catch (Throwable e) {
assertThat(e, instanceOf(exceptionClass));
}
// has to be outside catch clause to get a proper message
if (fail) {
throw new AssertionError("Expected a " + exceptionClass + " exception to be thrown");
}
}

}
Expand Up @@ -27,6 +27,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.testng.Assert.fail;
Expand Down Expand Up @@ -74,11 +75,7 @@ public void testNumericField() throws Exception {
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));

// flt field query on a numeric field -> failure
try {
searchResponse = client().prepareSearch().setQuery(fuzzyLikeThisFieldQuery("int_value").likeText("42")).execute().actionGet();
} catch (SearchPhaseExecutionException e) {
// OK
}
assertThrows(client().prepareSearch().setQuery(fuzzyLikeThisFieldQuery("int_value").likeText("42")), 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();
Expand Down

0 comments on commit 88a9ba7

Please sign in to comment.