use of org.opensearch.client.opensearch.core.SearchRequest in project opensearch-java by opensearch-project.
the class VariantsTest method testNestedVariantsWithContainerProperties.
@Test
public void testNestedVariantsWithContainerProperties() {
SearchRequest search = SearchRequest.of(s -> s.aggregations("agg1", a -> a.meta("m1", JsonData.of("m1 value")).valueCount(v -> v.field("f")).meta("m2", JsonData.of("m2 value"))));
assertEquals("m1 value", search.aggregations().get("agg1").meta().get("m1").to(String.class));
assertEquals("m2 value", search.aggregations().get("agg1").meta().get("m2").to(String.class));
}
use of org.opensearch.client.opensearch.core.SearchRequest in project opensearch-java by opensearch-project.
the class ClassStructureTest method testMapSetters.
@Test
public void testMapSetters() {
ValueCountAggregation countA = ValueCountAggregation.of(v -> v.field("a"));
ValueCountAggregation countB = ValueCountAggregation.of(v -> v.field("b"));
ValueCountAggregation countC = ValueCountAggregation.of(v -> v.field("c"));
Map<String, Aggregation> aggs = new HashMap<>();
aggs.put("aggA", countA._toAggregation());
aggs.put("aggB", countB._toAggregation());
{
// Appending doesn't modify the original collection
SearchRequest search = SearchRequest.of(b -> b.aggregations(aggs).aggregations("aggC", countC._toAggregation()).aggregations("aggD", a -> a.valueCount(c -> c.field("d"))));
// Original map wasn't modified
assertEquals(2, aggs.size());
assertEquals(4, search.aggregations().size());
assertEquals("a", search.aggregations().get("aggA").valueCount().field());
assertEquals("b", search.aggregations().get("aggB").valueCount().field());
assertEquals("c", search.aggregations().get("aggC").valueCount().field());
assertEquals("d", search.aggregations().get("aggD").valueCount().field());
}
{
// Map cannot be null
assertThrows(NullPointerException.class, () -> {
Map<String, Aggregation> nullMap = null;
SearchRequest.of(b -> b.aggregations(nullMap));
});
}
}
use of org.opensearch.client.opensearch.core.SearchRequest in project opensearch-java by opensearch-project.
the class RequestEncodingTest method testParametersNotInJson.
@Test
public void testParametersNotInJson() {
// This checks that path parameters ("q") are not serialized as json
// and variant containers ser/deser
SearchRequest request = new SearchRequest.Builder().q("blah").query(b1 -> b1.type(b2 -> b2.value("foo"))).aggregations("myagg", b2 -> b2.avg(b3 -> b3.field("foo"))).build();
JsonbJsonpMapper mapper = new JsonbJsonpMapper();
String str = toJson(request, mapper);
assertEquals("{\"aggregations\":{\"myagg\":{\"avg\":{\"field\":\"foo\"}}},\"query\":{\"type\":{\"value\":\"foo\"}}}", str);
request = fromJson(str, SearchRequest.class, mapper);
assertTrue(request.query().isType());
assertEquals("foo", request.query().type().value());
assertNull(request.q());
}
use of org.opensearch.client.opensearch.core.SearchRequest in project opensearch-java by opensearch-project.
the class ApiConventionsTest method collections.
@Test
public void collections() {
// tag::collections-list
// Prepare a list of index names
List<String> names = Arrays.asList("idx-a", "idx-b", "idx-c");
// Prepare cardinality aggregations for fields "foo" and "bar"
Map<String, Aggregation> cardinalities = new HashMap<>();
cardinalities.put("foo-count", Aggregation.of(a -> a.cardinality(c -> c.field("foo"))));
cardinalities.put("bar-count", Aggregation.of(a -> a.cardinality(c -> c.field("bar"))));
// Prepare an aggregation that computes the average of the "size" field
final Aggregation avgSize = Aggregation.of(a -> a.avg(v -> v.field("size")));
SearchRequest search = SearchRequest.of(r -> r.index(names).index("idx-d").index("idx-e", "idx-f", "idx-g").sort(s -> s.field(f -> f.field("foo").order(SortOrder.Asc))).sort(s -> s.field(f -> f.field("bar").order(SortOrder.Desc))).aggregations(cardinalities).aggregations("avg-size", avgSize).aggregations("price-histogram", a -> a.histogram(h -> h.field("price"))));
// end::collections-list
}
Aggregations