我是在弹性搜索中对指标名称建立索引。度量名称的格式为foo.bar.baz.aux
。这是我使用的索引。术语聚合(实现分层刻面)查询性能缓慢
{
"index": {
"analysis": {
"analyzer": {
"prefix-test-analyzer": {
"filter": "dotted",
"tokenizer": "prefix-test-tokenizer",
"type": "custom"
}
},
"filter": {
"dotted": {
"patterns": [
"([^.]+)"
],
"type": "pattern_capture"
}
},
"tokenizer": {
"prefix-test-tokenizer": {
"delimiter": ".",
"type": "path_hierarchy"
}
}
}
}
}
{
"metrics": {
"_routing": {
"required": true
},
"properties": {
"tenantId": {
"type": "string",
"index": "not_analyzed"
},
"unit": {
"type": "string",
"index": "not_analyzed"
},
"metric_name": {
"index_analyzer": "prefix-test-analyzer",
"search_analyzer": "keyword",
"type": "string"
}
}
}
}
上述指标的度量标准名称创建下列条款foo.bar.baz
foo
bar
baz
foo.bar
foo.bar.baz
如果我有一堆指标,像下面
a.b.c.d.e
a.b.c.d
a.b.m.n
x.y.z
我要编写一个查询抢令牌的第n级。在上面的例子中
for level = 0, I should get [a, x]
for level = 1, with 'a' as first token I should get [b]
with 'x' as first token I should get [y]
for level = 2, with 'a.b' as first token I should get [c, m]
我想不出任何其他方式,除了写术语聚合。要找出a.b
的二级令牌,下面是我提出的查询。
time curl -XGET http://localhost:9200/metrics_alias/metrics/_search\?pretty\&routing\=12345 -d '{
"size": 0,
"query": {
"term": {
"tenantId": "12345"
}
},
"aggs": {
"metric_name_tokens": {
"terms": {
"field" : "metric_name",
"include": "a[.]b[.][^.]*",
"execution_hint": "map",
"size": 0
}
}
}
}'
这会导致下面的问题。我解析输出并从那里抓取[c,m]。
"buckets" : [ {
"key" : "a.b.c",
"doc_count" : 2
}, {
"key" : "a.b.m",
"doc_count" : 1
} ]
到目前为止好。该查询适用于大多数租户(请注意上面的tenantId
term
查询)。对于有大量数据(大约1Million)的某些租户来说,表现确实很慢。我猜测聚合的所有术语都需要时间。
我想知道术语聚合是否是这种数据的正确选择,并且还在寻找其他可能的查询类型。
我不清楚你需要什么。你需要计数吗?或者你需要c和m吗?或者您是否需要包含以下文件:a.b.c.d.e - a.b.c.d - a.b.m.n? –
我只需要c和m。 – Chandra
@JettroCoenradie我只需要c和m。基本上什么是给定前缀的下一级可能令牌。 – Chandra