Search in sources :

Example 1 with Aggregation

use of org.opensearch.client.opensearch._types.aggregations.Aggregation in project opensearch-java by opensearch-project.

the class SearchRequest method serializeInternal.

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
    if (this.source != null) {
        generator.writeKey("_source");
        this.source.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.aggregations)) {
        generator.writeKey("aggregations");
        generator.writeStartObject();
        for (Map.Entry<String, Aggregation> item0 : this.aggregations.entrySet()) {
            generator.writeKey(item0.getKey());
            item0.getValue().serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (this.collapse != null) {
        generator.writeKey("collapse");
        this.collapse.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.docvalueFields)) {
        generator.writeKey("docvalue_fields");
        generator.writeStartArray();
        for (FieldAndFormat item0 : this.docvalueFields) {
            item0.serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (this.explain != null) {
        generator.writeKey("explain");
        generator.write(this.explain);
    }
    if (ApiTypeHelper.isDefined(this.fields)) {
        generator.writeKey("fields");
        generator.writeStartArray();
        for (FieldAndFormat item0 : this.fields) {
            item0.serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (this.from != null) {
        generator.writeKey("from");
        generator.write(this.from);
    }
    if (this.highlight != null) {
        generator.writeKey("highlight");
        this.highlight.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.indicesBoost)) {
        generator.writeKey("indices_boost");
        generator.writeStartArray();
        for (Map<String, Double> item0 : this.indicesBoost) {
            generator.writeStartObject();
            if (item0 != null) {
                for (Map.Entry<String, Double> item1 : item0.entrySet()) {
                    generator.writeKey(item1.getKey());
                    generator.write(item1.getValue());
                }
            }
            generator.writeEnd();
        }
        generator.writeEnd();
    }
    if (this.minScore != null) {
        generator.writeKey("min_score");
        generator.write(this.minScore);
    }
    if (this.postFilter != null) {
        generator.writeKey("post_filter");
        this.postFilter.serialize(generator, mapper);
    }
    if (this.profile != null) {
        generator.writeKey("profile");
        generator.write(this.profile);
    }
    if (this.query != null) {
        generator.writeKey("query");
        this.query.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.rescore)) {
        generator.writeKey("rescore");
        generator.writeStartArray();
        for (Rescore item0 : this.rescore) {
            item0.serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.runtimeMappings)) {
        generator.writeKey("runtime_mappings");
        generator.writeStartObject();
        for (Map.Entry<String, RuntimeField> item0 : this.runtimeMappings.entrySet()) {
            generator.writeKey(item0.getKey());
            item0.getValue().serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.scriptFields)) {
        generator.writeKey("script_fields");
        generator.writeStartObject();
        for (Map.Entry<String, ScriptField> item0 : this.scriptFields.entrySet()) {
            generator.writeKey(item0.getKey());
            item0.getValue().serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.searchAfter)) {
        generator.writeKey("search_after");
        generator.writeStartArray();
        for (String item0 : this.searchAfter) {
            generator.write(item0);
        }
        generator.writeEnd();
    }
    if (this.seqNoPrimaryTerm != null) {
        generator.writeKey("seq_no_primary_term");
        generator.write(this.seqNoPrimaryTerm);
    }
    if (this.size != null) {
        generator.writeKey("size");
        generator.write(this.size);
    }
    if (this.slice != null) {
        generator.writeKey("slice");
        this.slice.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.sort)) {
        generator.writeKey("sort");
        generator.writeStartArray();
        for (SortOptions item0 : this.sort) {
            item0.serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.stats)) {
        generator.writeKey("stats");
        generator.writeStartArray();
        for (String item0 : this.stats) {
            generator.write(item0);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.storedFields)) {
        generator.writeKey("stored_fields");
        generator.writeStartArray();
        for (String item0 : this.storedFields) {
            generator.write(item0);
        }
        generator.writeEnd();
    }
    if (this.suggest != null) {
        generator.writeKey("suggest");
        this.suggest.serialize(generator, mapper);
    }
    if (this.terminateAfter != null) {
        generator.writeKey("terminate_after");
        generator.write(this.terminateAfter);
    }
    if (this.timeout != null) {
        generator.writeKey("timeout");
        generator.write(this.timeout);
    }
    if (this.trackScores != null) {
        generator.writeKey("track_scores");
        generator.write(this.trackScores);
    }
    if (this.trackTotalHits != null) {
        generator.writeKey("track_total_hits");
        this.trackTotalHits.serialize(generator, mapper);
    }
    if (this.version != null) {
        generator.writeKey("version");
        generator.write(this.version);
    }
}
Also used : Rescore(org.opensearch.client.opensearch.core.search.Rescore) RuntimeField(org.opensearch.client.opensearch._types.mapping.RuntimeField) FieldAndFormat(org.opensearch.client.opensearch._types.query_dsl.FieldAndFormat) Aggregation(org.opensearch.client.opensearch._types.aggregations.Aggregation) ScriptField(org.opensearch.client.opensearch._types.ScriptField) HashMap(java.util.HashMap) Map(java.util.Map) SortOptions(org.opensearch.client.opensearch._types.SortOptions)

Example 2 with Aggregation

use of org.opensearch.client.opensearch._types.aggregations.Aggregation 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));
        });
    }
}
Also used : Aggregation(org.opensearch.client.opensearch._types.aggregations.Aggregation) ValueCountAggregation(org.opensearch.client.opensearch._types.aggregations.ValueCountAggregation) Buckets(org.opensearch.client.opensearch._types.aggregations.Buckets) Arrays(java.util.Arrays) Aggregation(org.opensearch.client.opensearch._types.aggregations.Aggregation) GetRequest(org.opensearch.client.opensearch.core.GetRequest) HashMap(java.util.HashMap) Aggregate(org.opensearch.client.opensearch._types.aggregations.Aggregate) RangeBucket(org.opensearch.client.opensearch._types.aggregations.RangeBucket) ValueCountAggregation(org.opensearch.client.opensearch._types.aggregations.ValueCountAggregation) ObjectBuilder(org.opensearch.client.util.ObjectBuilder) TotalHitsRelation(org.opensearch.client.opensearch.core.search.TotalHitsRelation) ApiTypeHelper(org.opensearch.client.util.ApiTypeHelper) Map(java.util.Map) HitsMetadata(org.opensearch.client.opensearch.core.search.HitsMetadata) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) ErrorCause(org.opensearch.client.opensearch._types.ErrorCause) JsonData(org.opensearch.client.json.JsonData) Test(org.junit.Test) TotalHits(org.opensearch.client.opensearch.core.search.TotalHits) Collectors(java.util.stream.Collectors) IntervalsQuery(org.opensearch.client.opensearch._types.query_dsl.IntervalsQuery) DateRangeAggregate(org.opensearch.client.opensearch._types.aggregations.DateRangeAggregate) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) List(java.util.List) SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) CardinalityAggregate(org.opensearch.client.opensearch._types.aggregations.CardinalityAggregate) FieldAndFormat(org.opensearch.client.opensearch._types.query_dsl.FieldAndFormat) Collections(java.util.Collections) SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) HashMap(java.util.HashMap) ValueCountAggregation(org.opensearch.client.opensearch._types.aggregations.ValueCountAggregation) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with Aggregation

use of org.opensearch.client.opensearch._types.aggregations.Aggregation 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
}
Also used : Aggregation(org.opensearch.client.opensearch._types.aggregations.Aggregation) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) Arrays(java.util.Arrays) Aggregation(org.opensearch.client.opensearch._types.aggregations.Aggregation) Alias(org.opensearch.client.opensearch.indices.Alias) Query(org.opensearch.client.opensearch._types.query_dsl.Query) CreateIndexRequest(org.opensearch.client.opensearch.indices.CreateIndexRequest) LogManager(java.util.logging.LogManager) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) Test(org.junit.Test) HashMap(java.util.HashMap) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) Logger(java.util.logging.Logger) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) OpenSearchTransport(org.opensearch.client.transport.OpenSearchTransport) TransportException(org.opensearch.client.transport.TransportException) List(java.util.List) SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) NodeStatistics(org.opensearch.client.opensearch._types.NodeStatistics) SortOrder(org.opensearch.client.opensearch._types.SortOrder) ApiTypeHelper(org.opensearch.client.util.ApiTypeHelper) Map(java.util.Map) Assert(org.junit.Assert) SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)3 Map (java.util.Map)3 Aggregation (org.opensearch.client.opensearch._types.aggregations.Aggregation)3 Arrays (java.util.Arrays)2 List (java.util.List)2 Test (org.junit.Test)2 FieldAndFormat (org.opensearch.client.opensearch._types.query_dsl.FieldAndFormat)2 SearchRequest (org.opensearch.client.opensearch.core.SearchRequest)2 ApiTypeHelper (org.opensearch.client.util.ApiTypeHelper)2 Method (java.lang.reflect.Method)1 Collections (java.util.Collections)1 LogManager (java.util.logging.LogManager)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 Assert (org.junit.Assert)1 JsonData (org.opensearch.client.json.JsonData)1 OpenSearchAsyncClient (org.opensearch.client.opensearch.OpenSearchAsyncClient)1 OpenSearchClient (org.opensearch.client.opensearch.OpenSearchClient)1 ErrorCause (org.opensearch.client.opensearch._types.ErrorCause)1