use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.
the class SearchTemplateIT method testTemplateQueryAsEscapedString.
/**
* Test that template can be expressed as a single escaped string.
*/
public void testTemplateQueryAsEscapedString() throws Exception {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("_all");
String query = "{" + " \"inline\" : \"{ \\\"size\\\": \\\"{{size}}\\\", \\\"query\\\":{\\\"match_all\\\":{}}}\"," + " \"params\":{" + " \"size\": 1" + " }" + "}";
SearchTemplateRequest request = RestSearchTemplateAction.parse(createParser(JsonXContent.jsonXContent, query));
request.setRequest(searchRequest);
SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get();
assertThat(searchResponse.getResponse().getHits().getHits().length, equalTo(1));
}
use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.
the class SearchTemplateIT method testSearchRequestFail.
// Relates to #6318
public void testSearchRequestFail() throws Exception {
String query = "{ \"query\": {\"match_all\": {}}, \"size\" : \"{{my_size}}\" }";
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("_all");
expectThrows(Exception.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(searchRequest).setScript(query).setScriptType(ScriptType.INLINE).setScriptParams(randomBoolean() ? null : Collections.emptyMap()).get());
SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(searchRequest).setScript(query).setScriptType(ScriptType.INLINE).setScriptParams(Collections.singletonMap("my_size", 1)).get();
assertThat(searchResponse.getResponse().getHits().getHits().length, equalTo(1));
}
use of org.elasticsearch.action.search.SearchRequest in project crate by crate.
the class FulltextIntegrationTest method testMatchTypes.
@Test
public void testMatchTypes() throws Exception {
this.setup.setUpLocations();
refresh();
SearchRequest searchRequest = new SearchRequest("locations").source("{\"query\": {\"multi_match\": {\"fields\": [\"kind^0.8\", \"name_description_ft^0.6\"], \"query\": \"planet earth\"}}}");
SearchResponse searchResponse = client().search(searchRequest).actionGet();
execute("select name, _score from locations where match((kind 0.8, name_description_ft 0.6), 'planet earth') using best_fields order by _score desc");
SearchHit[] hits = searchResponse.getHits().getHits();
for (int i = 0; i < hits.length; i++) {
assertThat(hits[i].score(), is(((float) response.rows()[i][1])));
}
assertThat(TestingHelpers.printedTable(response.rows()), is("Alpha Centauri| 0.2600391\nBartledan| 0.15168947\n| 0.10750017\nAllosimanius Syneca| 0.053750087\nGalactic Sector QQ7 Active J Gamma| 0.040312566\n"));
execute("select name, _score from locations where match((kind 0.6, name_description_ft 0.8), 'planet earth') using most_fields order by _score desc");
assertThat(TestingHelpers.printedTable(response.rows()), is("Alpha Centauri| 0.13054572\nBartledan| 0.07615167\n| 0.053683124\nAllosimanius Syneca| 0.026841562\nGalactic Sector QQ7 Active J Gamma| 0.02013117\n"));
execute("select name, _score from locations where match((kind 0.4, name_description_ft 1.0), 'planet earth') using cross_fields order by _score desc");
assertThat(TestingHelpers.printedTable(response.rows()), is("Alpha Centauri| 0.14860114\nBartledan| 0.086684\n| 0.061013106\nAllosimanius Syneca| 0.030506553\nGalactic Sector QQ7 Active J Gamma| 0.022879915\n"));
execute("select name, _score from locations where match((kind 1.0, name_description_ft 0.4), 'Alpha Centauri') using phrase");
assertThat(TestingHelpers.printedTable(response.rows()), is("Alpha Centauri| 1.1095\n"));
execute("select name, _score from locations where match(name_description_ft, 'Alpha Centauri') using phrase_prefix");
assertThat(TestingHelpers.printedTable(response.rows()), is("Alpha Centauri| 1.7897208\n"));
}
use of org.elasticsearch.action.search.SearchRequest in project crate by crate.
the class FulltextIntegrationTest method testCopyValuesFromStringArrayToIndex.
@Test
public void testCopyValuesFromStringArrayToIndex() throws Exception {
execute("CREATE TABLE t_string_array (" + " id INTEGER PRIMARY KEY," + " keywords ARRAY(STRING) INDEX USING FULLTEXT," + " INDEX keywords_ft USING FULLTEXT(keywords)" + ")");
execute("INSERT INTO t_string_array (id, keywords) VALUES (1, ['foo', 'bar', 'foo bar'])");
ensureYellow();
refresh();
execute("SELECT id, keywords FROM t_string_array WHERE match(keywords_ft, 'foo')");
assertThat(response.rows().length, is(1));
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("t_string_array");
// we need to use ES search request to aggregate on terms
String q = "{\n" + " \"from\": 0,\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " },\n" + " \"aggregations\": {\n" + " \"kw\": {\n" + " \"terms\": {\"field\": \"keywords_ft\"}\n" + " }\n" + " },\n" + " \"size\": 0\n" + "}";
BytesReference bytesRef = new BytesArray(q);
searchRequest.source(bytesRef);
SearchResponse searchResponse = client().search(searchRequest).get();
StringTerms terms = searchResponse.getAggregations().get("kw");
List<String> termValues = new ArrayList<>(terms.getBuckets().size());
terms.getBuckets().forEach(b -> termValues.add(b.getKeyAsString()));
assertThat(termValues, containsInAnyOrder("foo", "bar"));
}
use of org.elasticsearch.action.search.SearchRequest in project crate by crate.
the class MappingDefaultsTest method testAllFieldsDisabled.
@Test
public void testAllFieldsDisabled() throws Exception {
execute("create table test (col1 string)");
ensureYellow();
SQLResponse sqlResponse = execute("insert into test (col1) values ('foo')");
assertEquals(1, sqlResponse.rowCount());
refresh();
byte[] searchSource = XContentFactory.jsonBuilder().startObject().startObject("query").startObject("query_string").field("query", "foo").endObject().endObject().endObject().bytes().toBytes();
SearchRequest searchRequest = new SearchRequest("test");
searchRequest.source(searchSource);
SearchResponse response = client().search(searchRequest).actionGet();
assertEquals(0L, response.getHits().totalHits());
}
Aggregations