Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are typedapi planned for continued optimization? #539

Closed
ryan961 opened this issue Oct 14, 2022 · 1 comment
Closed

Are typedapi planned for continued optimization? #539

ryan961 opened this issue Oct 14, 2022 · 1 comment

Comments

@ryan961
Copy link

ryan961 commented Oct 14, 2022

I tried to assemble 'search.Request' using 'typedapi', but it's a little difficult and troublesome to use, hope it will be better in the future

// {"aggregations":{"composite":{"aggregations":{"ad":{"aggregations":{"ad_sum":{"sum":{"field":"ad_revenue"}}},"filter":{"query_string":{"query":"LogType:AdRev"}}},"iap":{"aggregations":{"count":{"cardinality":{"field":"uuid"}},"sum":{"sum":{"field":"price"}}},"filter":{"query_string":{"query":"LogType:iapLog"}}}},"composite":{"sources":[{"regDay":{"date_histogram":{"calendar_interval":"day","field":"register_time","time_zone":"UTC"}}},{"ad_id":{"terms":{"field":"ad_id","missing_bucket":true}}}]}}},"query":{"bool":{"filter":[{"query_string":{"query":"*"}},{"range":{"@timestamp":{"gte":1665331200,"lt":1665590400}}}]}}}

body := search.NewRequestBuilder().
		Aggregations(
			map[string]*types.AggregationContainerBuilder{
				"composite": types.NewAggregationContainerBuilder().
					Composite(
						types.NewCompositeAggregationBuilder().Sources(
							map[string]types.CompositeAggregationSource{
								"regDay": types.NewCompositeAggregationSourceBuilder().DateHistogram(
									types.NewDateHistogramAggregationBuilder().Field("register_time").CalendarInterval(calendarinterval.Day).TimeZone("UTC"),
								).Build(),
							},
							map[string]types.CompositeAggregationSource{
								"ad_id": types.NewCompositeAggregationSourceBuilder().Terms(
									types.NewTermsAggregationBuilder().Field("ad_id").MissingBucket(true),
								).Build(),
							},
						).Size(10000),
					).Aggregations(
					map[string]*types.AggregationContainerBuilder{
						"iap": types.NewAggregationContainerBuilder().
							Filter(types.NewQueryContainerBuilder().QueryString(types.NewQueryStringQueryBuilder().Query("LogType:iapLog"))).
							Aggregations(
								map[string]*types.AggregationContainerBuilder{
									"count": types.NewAggregationContainerBuilder().Cardinality(types.NewCardinalityAggregationBuilder().Field("uuid")),
									"sum":   types.NewAggregationContainerBuilder().Sum(types.NewSumAggregationBuilder().Field("price")),
								},
							),
						"ad": types.NewAggregationContainerBuilder().
							Filter(types.NewQueryContainerBuilder().QueryString(types.NewQueryStringQueryBuilder().Query("LogType:AdRev"))).Aggregations(
							map[string]*types.AggregationContainerBuilder{
								"ad_sum": types.NewAggregationContainerBuilder().Sum(types.NewSumAggregationBuilder().Field("ad_revenue")),
							},
						),
					},
				),
			},
		).
		Query(
			types.NewQueryContainerBuilder().Bool(
				types.NewBoolQueryBuilder().Filter(
					[]types.QueryContainer{
						types.NewQueryContainerBuilder().QueryString(types.NewQueryStringQueryBuilder().Query("*")).Build(),
						types.NewQueryContainerBuilder().Range(map[types.Field]*types.RangeQueryBuilder{
							types.NewFieldBuilder().Field("@timestamp").Build(): types.NewRangeQueryBuilder().NumberRangeQuery(types.NewNumberRangeQueryBuilder().Gte(1665331200).Lt(1665590400)),
						}).Build(),
					},
				),
			),
		).Build()

This is an example of using olivere/elastic

aggs := elastic.NewCompositeAggregation().Sources(
		elastic.NewCompositeAggregationDateHistogramValuesSource("regDay").Field("register_time_format").TimeZone("UTC").CalendarInterval("1d"),
		elastic.NewCompositeAggregationTermsValuesSource("ad_id").Field("ad_id"),
	).
		SubAggregation("iap", elastic.NewFilterAggregation().Filter(elastic.NewQueryStringQuery("LogType:iapLog").AnalyzeWildcard(true)).SubAggregation("count", elastic.NewCardinalityAggregation().Field("uuid")).SubAggregation("sum", elastic.NewSumAggregation().Field("price"))).
		SubAggregation("ad", elastic.NewFilterAggregation().Filter(elastic.NewQueryStringQuery("LogType:AdRev").AnalyzeWildcard(true)).SubAggregation("ad_sum", elastic.NewSumAggregation().Field("ad_revenue"))).
		Size(10000)

query := elastic.NewBoolQuery().Filter(
		elastic.NewQueryStringQuery("*"),
		elastic.NewRangeQuery("@timestamp").Format("epoch_second").Gte(1665331200).Lt(1665590400),
	)
@Anaethelion
Copy link
Contributor

Hi @ryan961

Thank you for you feedback, this is a really interesting use case.
To answer you question, yes, the typedapi is definitely planned for further improvements!

One way we could make this easier would be to generate some quick access method on the search request builder for query and aggregations, so that you wouldn't have to go deep to add one of those. Another improvement would be to add a single setter for maps and slices which would allow for simpler usage on the likes of filter.

For now, I'm linking this to #526 for future reference and closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants