Tablestore Go SDK kullanarak ölçüm hesaplayın veya arama dizini sorgu sonuçlarını gruplandırın.
Önkoşullar
Tablestore Go SDK öğesini yükleyin ve bir istemci başlatın.
Açıklama
Toplama işlemleri; sorgu sonuçları üzerinden ortalama, maksimum, minimum, toplam, sayı, benzersiz değer sayısı, yüzdelik dilimler veya en üst satırları hesaplar. Sonuçlar alanlara, aralıklara, tarihlere, coğrafi konumlara, filtrelere veya bileşik anahtarlara göre de gruplanabilir. Ölçüm toplamları veya alan, aralık ve histogram tabanlı gruplandırmalar için kullanılan alanlarda sıralama ve toplama özelliği etkinleştirilmelidir. GroupByFilter içindeki filtreler, ilgili sorgu türünün alan gereksinimlerini karşılar.
Benzersiz değer sayıları, yüzdelik dilimler ve alan değeri gruplandırması yaklaşık hesaplamalar kullanır. 10.000'in altındaki benzersiz değer sayısı gerçek değere yakındır; 100 milyon kayıtta hata oranı yaklaşık %2'dir. Uç noktalara yakın yüzdelik dilimler (örneğin P1 ve P99), genellikle P50'ye göre daha doğrudur. Alan değeri gruplandırması paralel hesaplandığından küçük hatalar içerebilir.
Birden çok toplama işlemi birleştirilebilir. Çok sayıda toplama veya derin iç içe geçme, istek karmaşıklığını ve yanıt gecikme süresini artırır. İç içe geçme sınırları için bkz. Arama dizini sınırları.
Aşağıdaki örnekte bir arama dizinindeki tüm veriler sorgulanır; fiyatların minimum, maksimum, toplam, ortalama, boş olmayan değer sayısı, benzersiz kategori sayısı ve P50 yüzdelik dilimi hesaplanır ve veriler kategoriye göre gruplanır.
searchQuery := search.NewSearchQuery().
SetQuery(&search.MatchAllQuery{}).
SetLimit(0).
Aggregation(
search.NewMinAggregation("min_price", "price"),
search.NewMaxAggregation("max_price", "price"),
search.NewSumAggregation("sum_price", "price"),
search.NewAvgAggregation("avg_price", "price"),
search.NewCountAggregation("price_count", "price"),
search.NewDistinctCountAggregation("category_count", "category"),
search.NewPercentilesAggregation("price_percentiles", "price").
SetPercents([]float64{50}),
).
GroupBy(search.NewGroupByField("category_group", "category").Size(10))
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
})
if err != nil {
log.Fatal(err)
}
minResult, err := response.AggregationResults.Min("min_price")
if err != nil {
log.Fatal(err)
}
maxResult, err := response.AggregationResults.Max("max_price")
if err != nil {
log.Fatal(err)
}
sumResult, err := response.AggregationResults.Sum("sum_price")
if err != nil {
log.Fatal(err)
}
avg, err := response.AggregationResults.Avg("avg_price")
if err != nil {
log.Fatal(err)
}
countResult, err := response.AggregationResults.Count("price_count")
if err != nil {
log.Fatal(err)
}
distinctResult, err := response.AggregationResults.DistinctCount("category_count")
if err != nil {
log.Fatal(err)
}
percentilesResult, err := response.AggregationResults.Percentiles("price_percentiles")
if err != nil {
log.Fatal(err)
}
groups, err := response.GroupByResults.GroupByField("category_group")
if err != nil {
log.Fatal(err)
}
fmt.Println(minResult.Value, maxResult.Value, sumResult.Value)
fmt.Println(avg.Value)
fmt.Println(countResult.Value, distinctResult.Value)
fmt.Println(percentilesResult.PercentilesAggregationItems)
fmt.Println(groups.Items)
Parametreler
Sorgu isteği
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sorgu yapılandırması
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ölçüm toplamları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MinAggregation, MaxAggregation, AvgAggregation ve SumAggregation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CountAggregation ve DistinctCountAggregation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CountAggregation, belirtilen alanın null olmadığı satırları sayar ve seyrek sütunlar için uygundur. Bir sorguyla eşleşen tüm satırları saymak için SetGetTotalCount(true) çağrısı yapın ve SearchResponse.TotalCount değerini okuyun. Bir arama dizinindeki tüm satırları saymak için sorgu koşulu olarak MatchAllQuery kullanın.
PercentilesAggregation
PercentilesAggregation, Tablestore Go SDK 1.7.0 ve sonraki sürümlerde desteklenir.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TopRowsAggregation
TopRowsAggregation'ı bir grubun alt toplamı olarak kullanın. Bu özellik, Tablestore Go SDK 1.7.0 ve sonraki sürümlerde desteklenir.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SearchRequest.ColumnsToGet, hangi nitelik sütunlarının döndürüleceğini kontrol eder. Nitelik sütunlarını doğrudan arama dizininden döndürmek için dizini oluştururken ilgili alanları depolayın. Döndürülecek sütunlar belirtilmezse yalnızca birincil anahtar sütunları döndürülür.
Gruplandırma
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByField
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Grup sıralama
Aşağıdaki grup sıralama türleri desteklenir:
GroupKeyGroupBySort: Grup anahtarlarını artan veya azalan sözcük sırasına göre sıralar.RowCountGroupBySort: Grup satır sayısına göre artan veya azalan düzende sıralar. Azalan satır sayısı sırası varsayılandır.SubAggGroupBySort: Belirtilen alt toplamın sonucuna göre artan veya azalan düzende sıralar.
GroupByRange
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByGeoDistance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByFilter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByHistogram
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByDateHistogram
GroupByDateHistogram, Tablestore Go SDK 1.7.10 ve sonraki sürümlerde desteklenir.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByGeoGrid
GroupByGeoGrid, Tablestore Go SDK 1.7.12 ve sonraki sürümlerde desteklenir.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByComposite
GroupByComposite, Tablestore Go SDK 1.7.15 ve sonraki sürümlerde desteklenir.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Yanıt
Ölçüm sonuçlarını toplama adına göre SearchResponse.AggregationResults üzerinden ve gruplandırma sonuçlarını grup adına göre SearchResponse.GroupByResults üzerinden okuyun.
Ölçüm toplama sonuçları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Gruplandırma sonuçları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Örnekler
Alt toplamları ve alt grupları kullanma
Aşağıdaki örnekte satırlar kategoriye göre gruplanır, her kategorideki maksimum fiyat hesaplanır ve satırlar duruma göre ayrıca gruplanır.
groupBy := search.NewGroupByField("category_group", "category").
Size(10).
SubAggregation(search.NewMaxAggregation("max_price", "price")).
SubGroupBy(search.NewGroupByField("status_group", "status").Size(10))
searchQuery := search.NewSearchQuery().
SetQuery(&search.MatchAllQuery{}).
SetLimit(0).
GroupBy(groupBy)
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
})
if err != nil {
log.Fatal(err)
}
result, err := response.GroupByResults.GroupByField("category_group")
if err != nil {
log.Fatal(err)
}
for _, item := range result.Items {
maxPrice, err := item.SubAggregations.Max("max_price")
if err != nil {
log.Fatal(err)
}
statuses, err := item.SubGroupBys.GroupByField("status_group")
if err != nil {
log.Fatal(err)
}
fmt.Println(item.Key, maxPrice.Value, statuses.Items)
}
Birden çok alana göre gruplandırma ve sonuçları sayfalandırma
GroupByComposite, çok alanlı gruplandırma sonuçlarını düz bileşik anahtarlar olarak döndürür ve sonraki grupları okumak için NextToken kullanır.
composite := search.NewGroupByComposite("category_status_group").
SourceGroupBys(
search.NewGroupByField("category", "category"),
search.NewGroupByField("status", "status"),
).
SetSize(100)
var nextToken *string
for {
if nextToken != nil {
composite.SetNextToken(nextToken)
}
searchQuery := search.NewSearchQuery().
SetQuery(&search.MatchAllQuery{}).
SetLimit(0).
GroupBy(composite)
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
})
if err != nil {
log.Fatal(err)
}
result, err := response.GroupByResults.GroupByComposite(
"category_status_group",
)
if err != nil {
log.Fatal(err)
}
for _, item := range result.Items {
fmt.Println(item.Keys, item.RowCount)
}
nextToken = result.NextToken
if nextToken == nil || *nextToken == "" {
break
}
}
Aralığa, mesafeye ve filtreye göre gruplandırma
Aşağıdaki örnekte aynı sorguda sonuçlar fiyat aralığına, coğrafi mesafeye ve filtreye göre gruplanır.
priceRanges := search.NewGroupByRange("price_ranges", "price").
Range(0, 200).
Range(200, 500)
distanceRanges := search.NewGroupByGeoDistance(
"distance_ranges",
"location",
search.GeoPoint{Lat: 30.2741, Lon: 120.1551},
).
Range(0, 100000).
Range(100000, 1500000)
categoryFilters := search.NewGroupByFilter("category_filters").
Query(&search.TermQuery{FieldName: "category", Term: "book-go"}).
Query(&search.TermQuery{FieldName: "category", Term: "game"})
searchQuery := search.NewSearchQuery().
SetQuery(&search.MatchAllQuery{}).
SetLimit(0).
GroupBy(priceRanges, distanceRanges, categoryFilters)
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
})
if err != nil {
log.Fatal(err)
}
priceResult, err := response.GroupByResults.GroupByRange("price_ranges")
if err != nil {
log.Fatal(err)
}
distanceResult, err := response.GroupByResults.GroupByGeoDistance(
"distance_ranges",
)
if err != nil {
log.Fatal(err)
}
filterResult, err := response.GroupByResults.GroupByFilter("category_filters")
if err != nil {
log.Fatal(err)
}
fmt.Println(priceResult.Items)
fmt.Println(distanceResult.Items)
fmt.Println(filterResult.Items)
Sayısal ve tarih histogramları oluşturma
Aşağıdaki örnekte sabit fiyat aralıkları kullanılarak sayısal bir histogram ve aylık bir tarih histogramı oluşturulur.
groupKeyAscending := []search.GroupBySorter{
&search.GroupKeyGroupBySort{Order: search.SortOrder_ASC.Enum()},
}
priceHistogram := search.NewGroupByHistogram("price_histogram", "price").
SetInterval(int64(200)).
SetFiledRange(int64(0), int64(600)).
SetMinDocCount(1).
SetGroupBySorters(groupKeyAscending)
dateHistogram := search.NewGroupByDateHistogram(
"date_histogram",
"event_time",
).
SetInterval(model.DateTimeValue{
Value: proto.Int32(1),
Unit: model.DateTimeUnit_MONTH.Enum(),
}).
SetFiledRange("2026-01-01T00:00:00", "2026-05-01T00:00:00").
SetTimeZone("+08:00").
SetMinDocCount(1).
SetGroupBySorters(groupKeyAscending)
searchQuery := search.NewSearchQuery().
SetQuery(&search.MatchAllQuery{}).
SetLimit(0).
GroupBy(priceHistogram, dateHistogram)
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
})
if err != nil {
log.Fatal(err)
}
priceResult, err := response.GroupByResults.GroupByHistogram("price_histogram")
if err != nil {
log.Fatal(err)
}
dateResult, err := response.GroupByResults.GroupByDateHistogram("date_histogram")
if err != nil {
log.Fatal(err)
}
fmt.Println(priceResult.Items)
fmt.Println(dateResult.Items)
Coğrafi ızgaraya göre gruplandırma
Aşağıdaki örnekte coğrafi bir alan yaklaşık 39 km × 19 km boyutundaki GeoHash ızgaralarına göre gruplanır.
searchQuery := search.NewSearchQuery().
SetQuery(&search.MatchAllQuery{}).
SetLimit(0).
GroupBy(
search.NewGroupByGeoGrid("geo_grid", "location").
SetPrecision(model.GHP_39KM_19KM_4).
SetSize(100),
)
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
})
if err != nil {
log.Fatal(err)
}
result, err := response.GroupByResults.GroupByGeoGrid("geo_grid")
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Items)
Gruplar içindeki satırları alma
Aşağıdaki örnekte satırlar kategoriye göre gruplanır ve her kategorideki en yüksek fiyata sahip satır döndürülür.
topRows := search.NewTopRowsAggregation("top_price").
SetLimit(1).
SetSort(&search.Sort{Sorters: []search.Sorter{
&search.FieldSort{
FieldName: "price",
Order: search.SortOrder_DESC.Enum(),
},
}})
groupBy := search.NewGroupByField("category_group", "category").
Size(10).
SubAggregation(topRows)
searchQuery := search.NewSearchQuery().
SetQuery(&search.MatchAllQuery{}).
SetLimit(0).
GroupBy(groupBy)
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
ColumnsToGet: &tablestore.ColumnsToGet{
Columns: []string{"category", "price"},
},
})
if err != nil {
log.Fatal(err)
}
groups, err := response.GroupByResults.GroupByField("category_group")
if err != nil {
log.Fatal(err)
}
for _, item := range groups.Items {
rows, err := item.SubAggregations.TopRows("top_price")
if err != nil {
log.Fatal(err)
}
fmt.Println(item.Key, rows.Value)
}
Çok alanlı gruplandırma yöntemlerini karşılaştırma
Birden çok alana göre gruplandırmak için birden çok GroupByField değerini iç içe geçirin veya GroupByComposite kullanın. Sonuç yapısına, sıralama gereksinimlerine ve sayfalamanın gerekli olup olmadığına bağlı olarak seçim yapın.
|
|
|
|
|
|
|
|
|