use of org.opensearch.client.opensearch.core.SearchResponse in project opensearch-java by opensearch-project.
the class RequestTest method testDataIngestion.
@Test
public void testDataIngestion() throws Exception {
String index = "ingest-test";
// Create an index
CreateIndexResponse createIndexResponse = highLevelClient().indices().create(b -> b.index(index));
assertEquals(index, createIndexResponse.index());
// Check that it actually exists. Example of a boolean response
BooleanResponse existsResponse = highLevelClient().indices().exists(b -> b.index(index));
assertTrue(existsResponse.value());
// Ingest some data
AppData appData = new AppData();
appData.setIntValue(1337);
appData.setMsg("foo");
String docId = highLevelClient().index(b -> b.index(index).id(// test with url-unsafe string
"my/Id").document(appData).refresh(// Make it visible for search
Refresh.True)).id();
assertEquals("my/Id", docId);
// Check auto-created mapping
GetMappingResponse mapping = highLevelClient().indices().getMapping(b -> b.index(index));
assertEquals(Property.Kind.Long, mapping.get("ingest-test").mappings().properties().get("intValue")._kind());
// Query by id
AppData esData = highLevelClient().get(b -> b.index(index).id(docId), AppData.class).source();
assertEquals(1337, esData.getIntValue());
assertEquals("foo", esData.getMsg());
// Query by id a non-existing document
final GetResponse<AppData> notExists = highLevelClient().get(b -> b.index(index).id("some-random-id"), AppData.class);
assertFalse(notExists.found());
assertNull(notExists.source());
// Search
SearchResponse<AppData> search = highLevelClient().search(b -> b.index(index), AppData.class);
long hits = search.hits().total().value();
assertEquals(1, hits);
esData = search.hits().hits().get(0).source();
assertEquals(1337, esData.getIntValue());
assertEquals("foo", esData.getMsg());
RequestItem item = RequestItem.of(_1 -> _1.header(_2 -> _2.index("test")).body(_2 -> _2.size(4)));
// MSearch: 1st search on an existing index, 2nd one on a non-existing index
final MsearchResponse<AppData> msearch = highLevelClient().msearch(_0 -> _0.searches(_1 -> _1.header(_3 -> _3.index(index)).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))).searches(_1 -> _1.header(_3 -> _3.index("non-existing")).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))), AppData.class);
assertEquals(2, msearch.responses().size());
assertTrue(msearch.responses().get(0).isResult());
assertEquals(1, msearch.responses().get(0).result().hits().hits().size());
assertTrue(msearch.responses().get(1).isFailure());
assertEquals(404, msearch.responses().get(1).failure().status());
}
use of org.opensearch.client.opensearch.core.SearchResponse in project opensearch-java by opensearch-project.
the class RequestTest method testSearchAggregation.
@Test
public void testSearchAggregation() throws IOException {
highLevelClient().create(_1 -> _1.index("products").id("A").document(new Product(5)).refresh(Refresh.True));
highLevelClient().create(_1 -> _1.index("products").id("B").document(new Product(15)).refresh(Refresh.True));
highLevelClient().create(_1 -> _1.index("products").id("C").document(new Product(25)).refresh(Refresh.True));
SearchResponse<Product> searchResponse = highLevelClient().search(_1 -> _1.index("products").size(0).aggregations("prices", _3 -> _3.histogram(_4 -> _4.field("price").interval(10.0)).aggregations("average", _5 -> _5.avg(_6 -> _6.field("price")))), Product.class);
HistogramAggregate prices = searchResponse.aggregations().get("prices").histogram();
assertEquals(3, prices.buckets().array().size());
assertEquals(1, prices.buckets().array().get(0).docCount());
assertEquals(5.0, prices.buckets().array().get(0).aggregations().get("average").avg().value(), 0.01);
// We've set "size" to zero
assertEquals(0, searchResponse.hits().hits().size());
}
use of org.opensearch.client.opensearch.core.SearchResponse in project opensearch-java by opensearch-project.
the class TypedKeysTest method testAdditionalProperties.
@Test
public void testAdditionalProperties() {
Aggregate avg1 = AvgAggregate.of(_1 -> _1.value(1.0))._toAggregate();
Aggregate avg2 = AvgAggregate.of(_1 -> _1.value(2.0))._toAggregate();
Aggregate aggregate = StringTermsAggregate.of(_0 -> _0.sumOtherDocCount(1).buckets(b -> b.array(ListBuilder.of(StringTermsBucket.Builder::new).add(_1 -> _1.key("key_1").docCount(1).aggregations(MapBuilder.of("bar", avg1))).add(_1 -> _1.key("key_2").docCount(2).aggregations(MapBuilder.of("bar", avg2))).build())))._toAggregate();
SearchResponse<Void> resp = new SearchResponse.Builder<Void>().aggregations("foo", aggregate).took(1).shards(_1 -> _1.successful(1).failed(0).total(1)).hits(_1 -> _1.total(_2 -> _2.value(0).relation(TotalHitsRelation.Eq)).hits(Collections.emptyList())).timedOut(false).build();
String json = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0}," + "\"hits\":{\"total\":{\"relation\":\"eq\",\"value\":0},\"hits\":[]}," + "\"aggregations\":{\"sterms#foo\":{\"buckets\":[" + "{\"avg#bar\":{\"value\":1.0},\"doc_count\":1,\"key\":\"key_1\"}," + "{\"avg#bar\":{\"value\":2.0},\"doc_count\":2,\"key\":\"key_2\"}" + "],\"sum_other_doc_count\":1}}}";
assertEquals(json, toJson(resp));
resp = fromJson(json, SearchResponse.createSearchResponseDeserializer(JsonpDeserializer.voidDeserializer()));
StringTermsAggregate foo = resp.aggregations().get("foo").sterms();
assertEquals(1, foo.sumOtherDocCount());
assertEquals(1, foo.buckets().array().get(0).docCount());
assertEquals("key_1", foo.buckets().array().get(0).key());
assertEquals(1.0, foo.buckets().array().get(0).aggregations().get("bar").avg().value(), 0.01);
assertEquals("key_2", foo.buckets().array().get(1).key());
assertEquals(2.0, foo.buckets().array().get(1).aggregations().get("bar").avg().value(), 0.01);
}
use of org.opensearch.client.opensearch.core.SearchResponse in project opensearch-java by opensearch-project.
the class ApiConventionsTest method builderIntervals.
@Test(expected = TransportException.class)
public void builderIntervals() throws Exception {
OpenSearchClient client = new OpenSearchClient(transport);
// tag::builder-intervals
SearchResponse<SomeApplicationData> results = client.search(_0 -> _0.query(_1 -> _1.intervals(_2 -> _2.field("my_text").allOf(_3 -> _3.ordered(true).intervals(_4 -> _4.match(_5 -> _5.query("my favorite food").maxGaps(0).ordered(true))).intervals(_4 -> _4.anyOf(_5 -> _5.intervals(_6 -> _6.match(_7 -> _7.query("hot water"))).intervals(_6 -> _6.match(_7 -> _7.query("cold porridge")))))))), // <1>
SomeApplicationData.class);
// end::builder-intervals
}
use of org.opensearch.client.opensearch.core.SearchResponse in project opensearch-java by opensearch-project.
the class ConnectingTest method createClient.
// we don't have a running ES
@Ignore
@Test
public void createClient() throws Exception {
// tag::create-client
// Create the low-level client
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
// Create the transport with a Jackson mapper
OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// And create the API client
OpenSearchClient client = new OpenSearchClient(transport);
// end::create-client
// tag::first-request
SearchResponse<Product> search = client.search(s -> s.index("products").query(q -> q.term(t -> t.field("name").value(v -> v.stringValue("bicycle")))), Product.class);
for (Hit<Product> hit : search.hits().hits()) {
processProduct(hit.source());
}
// end::first-request
}
Aggregations