L01*_*01C 2 elasticsearch elastica elastic-stack elasticsearch-6
我的 ElasticSearch 6.5.2 索引看起来像:
{
"_index" : "searches",
"_type" : "searches",
"_id" : "cCYuHW4BvwH6Y3jL87ul",
"_score" : 1.0,
"_source" : {
"querySearched" : "telecom",
}
},
{
"_index" : "searches",
"_type" : "searches",
"_id" : "cSYuHW4BvwH6Y3jL_Lvt",
"_score" : 1.0,
"_source" : {
"querySearched" : "telecom",
}
},
{
"_index" : "searches",
"_type" : "searches",
"_id" : "eCb6O24BvwH6Y3jLP7tM",
"_score" : 1.0,
"_source" : {
"querySearched" : "industry",
}
Run Code Online (Sandbox Code Playgroud)
我想要一个返回这个结果的查询:
"result":
{
"querySearched" : "telecom",
"number" : 2
},
{
"querySearched" : "industry",
"number" : 1
}
Run Code Online (Sandbox Code Playgroud)
我只想按出现次数分组并获得每个的数量,限制为最大的十个数字。我尝试过聚合,但桶是空的。谢谢!
案例你的映射
PUT /index
{
"mappings": {
"doc": {
"properties": {
"querySearched": {
"type": "text",
"fielddata": true
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您的查询应该看起来像
GET index/_search
{
"size": 0,
"aggs": {
"result": {
"terms": {
"field": "querySearched",
"size": 10
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您应该添加fielddata:true
以便为文本类型字段启用聚合更多
"size": 10, => limit to 10
Run Code Online (Sandbox Code Playgroud)
在与@Kamal 进行了简短讨论后,我觉得有必要让您知道,如果您选择启用,fielddata:true
您必须知道它会消耗大量堆空间。
从我分享的链接:
Fielddata 会消耗大量堆空间,尤其是在加载高基数文本字段时。一旦 fielddata 被加载到堆中,它就会在段的生命周期内保持在那里。此外,加载 fielddata 是一个昂贵的过程,可能会导致用户体验延迟命中。这就是默认情况下禁用 fielddata 的原因。
另一种选择(更有效的选择):
PUT /index
{
"mappings": {
"doc": {
"properties": {
"querySearched": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你的聚合查询
GET index/_search
{
"size": 0,
"aggs": {
"result": {
"terms": {
"field": "querySearched.keyword",
"size": 10
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
两种解决方案都有效,但您应该考虑到这一点。
希望能帮助到你
归档时间: |
|
查看次数: |
7270 次 |
最近记录: |