use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchQueryIT method testMultiMatchQueryZeroTermsQuery.
public void testMultiMatchQueryZeroTermsQuery() {
assertAcked(prepareCreate("test").addMapping("type1", "field1", "type=text,analyzer=classic", "field2", "type=text,analyzer=classic"));
client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get();
client().prepareIndex("test", "type1", "2").setSource("field1", "value3", "field2", "value4").get();
refresh();
BoolQueryBuilder boolQuery = boolQuery().must(multiMatchQuery("a", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.NONE)).must(// Fields are ORed together
multiMatchQuery("value1", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.NONE));
SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery).get();
assertHitCount(searchResponse, 0L);
boolQuery = boolQuery().must(multiMatchQuery("a", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL)).must(multiMatchQuery("value4", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL));
searchResponse = client().prepareSearch().setQuery(boolQuery).get();
assertHitCount(searchResponse, 1L);
boolQuery = boolQuery().must(multiMatchQuery("a", "field1").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL));
searchResponse = client().prepareSearch().setQuery(boolQuery).get();
assertHitCount(searchResponse, 2L);
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchQueryIT method testBasicQueryById.
public void testBasicQueryById() throws Exception {
createIndex("test");
client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
client().prepareIndex("test", "type2", "2").setSource("field1", "value2").get();
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(idsQuery("type1", "type2").addIds("1", "2")).get();
assertHitCount(searchResponse, 2L);
assertThat(searchResponse.getHits().getHits().length, equalTo(2));
searchResponse = client().prepareSearch().setQuery(idsQuery().addIds("1")).get();
assertHitCount(searchResponse, 1L);
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
searchResponse = client().prepareSearch().setQuery(idsQuery().addIds("1", "2")).get();
assertHitCount(searchResponse, 2L);
assertThat(searchResponse.getHits().getHits().length, equalTo(2));
searchResponse = client().prepareSearch().setQuery(idsQuery("type1").addIds("1", "2")).get();
assertHitCount(searchResponse, 1L);
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
searchResponse = client().prepareSearch().setQuery(idsQuery(Strings.EMPTY_ARRAY).addIds("1")).get();
assertHitCount(searchResponse, 1L);
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
searchResponse = client().prepareSearch().setQuery(idsQuery("type1", "type2", "type3").addIds("1", "2", "3", "4")).get();
assertHitCount(searchResponse, 2L);
assertThat(searchResponse.getHits().getHits().length, equalTo(2));
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchQueryIT method testIdsQueryTestsIdIndexed.
public void testIdsQueryTestsIdIndexed() throws Exception {
assertAcked(client().admin().indices().prepareCreate("test"));
indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1"), client().prepareIndex("test", "type1", "2").setSource("field1", "value2"), client().prepareIndex("test", "type1", "3").setSource("field1", "value3"));
SearchResponse searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsQuery("type1").addIds("1", "3"))).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "1", "3");
// no type
searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsQuery().addIds("1", "3"))).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "1", "3");
searchResponse = client().prepareSearch().setQuery(idsQuery("type1").addIds("1", "3")).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "1", "3");
// no type
searchResponse = client().prepareSearch().setQuery(idsQuery().addIds("1", "3")).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "1", "3");
searchResponse = client().prepareSearch().setQuery(idsQuery("type1").addIds("7", "10")).get();
assertHitCount(searchResponse, 0L);
// repeat..., with terms
searchResponse = client().prepareSearch().setTypes("type1").setQuery(constantScoreQuery(termsQuery("_id", "1", "3"))).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "1", "3");
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchQueryIT method testMustNot.
// see #2926
public void testMustNot() throws IOException, ExecutionException, InterruptedException {
assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, between(2, DEFAULT_MAX_NUM_SHARDS)));
indexRandom(true, client().prepareIndex("test", "test", "1").setSource("description", "foo other anything bar"), client().prepareIndex("test", "test", "2").setSource("description", "foo other anything"), client().prepareIndex("test", "test", "3").setSource("description", "foo other"), client().prepareIndex("test", "test", "4").setSource("description", "foo"));
SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get();
assertHitCount(searchResponse, 4L);
searchResponse = client().prepareSearch("test").setQuery(boolQuery().mustNot(matchQuery("description", "anything").type(Type.BOOLEAN))).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get();
assertHitCount(searchResponse, 2L);
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchQueryIT method testMultiMatchQueryMinShouldMatch.
public void testMultiMatchQueryMinShouldMatch() {
createIndex("test");
client().prepareIndex("test", "type1", "1").setSource("field1", new String[] { "value1", "value2", "value3" }).get();
client().prepareIndex("test", "type1", "2").setSource("field2", "value1").get();
refresh();
MultiMatchQueryBuilder multiMatchQuery = multiMatchQuery("value1 value2 foo", "field1", "field2");
multiMatchQuery.useDisMax(true);
multiMatchQuery.minimumShouldMatch("70%");
SearchResponse searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("1"));
multiMatchQuery.minimumShouldMatch("30%");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasId("1"));
assertSecondHit(searchResponse, hasId("2"));
multiMatchQuery.useDisMax(false);
multiMatchQuery.minimumShouldMatch("70%");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("1"));
multiMatchQuery.minimumShouldMatch("30%");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasId("1"));
assertSecondHit(searchResponse, hasId("2"));
multiMatchQuery = multiMatchQuery("value1 value2 bar", "field1");
multiMatchQuery.minimumShouldMatch("100%");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 0L);
multiMatchQuery.minimumShouldMatch("70%");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("1"));
// Min should match > # optional clauses returns no docs.
multiMatchQuery = multiMatchQuery("value1 value2 value3", "field1", "field2");
multiMatchQuery.minimumShouldMatch("4");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 0L);
}
Aggregations