use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SimpleQueryStringIT method testKeywordWithWhitespace.
public void testKeywordWithWhitespace() throws Exception {
String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index.json");
prepareCreate("test").setSource(indexBody, XContentType.JSON).get();
ensureGreen("test");
List<IndexRequestBuilder> reqs = new ArrayList<>();
reqs.add(client().prepareIndex("test", "doc", "1").setSource("f2", "Foo Bar"));
reqs.add(client().prepareIndex("test", "doc", "2").setSource("f1", "bar"));
reqs.add(client().prepareIndex("test", "doc", "3").setSource("f1", "foo bar"));
indexRandom(true, false, reqs);
SearchResponse resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo")).get();
assertHits(resp.getHits(), "3");
assertHitCount(resp, 1L);
resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("bar")).get();
assertHits(resp.getHits(), "2", "3");
assertHitCount(resp, 2L);
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SimpleQueryStringIT method testSimpleQueryStringMinimumShouldMatch.
public void testSimpleQueryStringMinimumShouldMatch() throws Exception {
createIndex("test");
ensureGreen("test");
indexRandom(true, false, client().prepareIndex("test", "type1", "1").setSource("body", "foo"), client().prepareIndex("test", "type1", "2").setSource("body", "bar"), client().prepareIndex("test", "type1", "3").setSource("body", "foo bar"), client().prepareIndex("test", "type1", "4").setSource("body", "foo baz bar"));
logger.info("--> query 1");
SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar").minimumShouldMatch("2")).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "3", "4");
logger.info("--> query 2");
searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar").field("body").field("body2").minimumShouldMatch("2")).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "3", "4");
// test case from #13884
logger.info("--> query 3");
searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo").field("body").field("body2").field("body3").minimumShouldMatch("-50%")).get();
assertHitCount(searchResponse, 3L);
assertSearchHits(searchResponse, "1", "3", "4");
logger.info("--> query 4");
searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar baz").field("body").field("body2").minimumShouldMatch("70%")).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "3", "4");
indexRandom(true, false, client().prepareIndex("test", "type1", "5").setSource("body2", "foo", "other", "foo"), client().prepareIndex("test", "type1", "6").setSource("body2", "bar", "other", "foo"), client().prepareIndex("test", "type1", "7").setSource("body2", "foo bar", "other", "foo"), client().prepareIndex("test", "type1", "8").setSource("body2", "foo baz bar", "other", "foo"));
logger.info("--> query 5");
searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar").field("body").field("body2").minimumShouldMatch("2")).get();
assertHitCount(searchResponse, 4L);
assertSearchHits(searchResponse, "3", "4", "7", "8");
logger.info("--> query 6");
searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar").minimumShouldMatch("2")).get();
assertHitCount(searchResponse, 5L);
assertSearchHits(searchResponse, "3", "4", "6", "7", "8");
logger.info("--> query 7");
searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar baz").field("body2").field("other").minimumShouldMatch("70%")).get();
assertHitCount(searchResponse, 3L);
assertSearchHits(searchResponse, "6", "7", "8");
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SimpleQueryStringIT method testSimpleQueryStringLenient.
public void testSimpleQueryStringLenient() throws ExecutionException, InterruptedException {
createIndex("test1", "test2");
indexRandom(true, client().prepareIndex("test1", "type1", "1").setSource("field", "foo"), client().prepareIndex("test2", "type1", "10").setSource("field", 5));
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo").field("field")).get();
assertFailures(searchResponse);
assertHitCount(searchResponse, 1L);
assertSearchHits(searchResponse, "1");
searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo").field("field").lenient(true)).get();
assertNoFailures(searchResponse);
assertHitCount(searchResponse, 1L);
assertSearchHits(searchResponse, "1");
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SimpleQueryStringIT method testExplicitAllFieldsRequested.
public void testExplicitAllFieldsRequested() throws Exception {
String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index-with-all.json");
prepareCreate("test").setSource(indexBody, XContentType.JSON).get();
ensureGreen("test");
List<IndexRequestBuilder> reqs = new ArrayList<>();
reqs.add(client().prepareIndex("test", "doc", "1").setSource("f1", "foo", "f2", "eggplant"));
indexRandom(true, false, reqs);
SearchResponse resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo eggplent").defaultOperator(Operator.AND)).get();
assertHitCount(resp, 0L);
resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo eggplent").defaultOperator(Operator.AND).useAllFields(true)).get();
assertHits(resp.getHits(), "1");
assertHitCount(resp, 1L);
Exception e = expectThrows(Exception.class, () -> client().prepareSearch("test").setQuery(simpleQueryStringQuery("blah").field("f1").useAllFields(true)).get());
assertThat(ExceptionsHelper.detailedMessage(e), containsString("cannot use [all_fields] parameter in conjunction with [fields]"));
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchQueryIT method testMinScore.
public void testMinScore() throws ExecutionException, InterruptedException {
createIndex("test");
client().prepareIndex("test", "test", "1").setSource("score", 1.5).get();
client().prepareIndex("test", "test", "2").setSource("score", 1.0).get();
client().prepareIndex("test", "test", "3").setSource("score", 2.0).get();
client().prepareIndex("test", "test", "4").setSource("score", 0.5).get();
refresh();
SearchResponse searchResponse = client().prepareSearch("test").setQuery(functionScoreQuery(ScoreFunctionBuilders.fieldValueFactorFunction("score").missing(1.0)).setMinScore(1.5f)).get();
assertHitCount(searchResponse, 2);
assertFirstHit(searchResponse, hasId("3"));
assertSecondHit(searchResponse, hasId("1"));
}
Aggregations