假设我有一组文档,其中包含常见的个人信息,例如:
{
"name": "Samuel",
"Job": "Engineer",
....
}我的客户想要浏览" name“字段中的所有术语,从一个以”选定“名称为中心的页面开始。例如,他们想要搜索"Samuel“,并显示一个包含7个元素的页面,例如:
Eddie: 7
Lucian: 3
Sammy: 1
Samuel: 3
Scott:3
Teddy: 2
Tod: 1其中名称按字母顺序排序,数字是出现的次数。也将是很好的能够在页面上上下移动。这只是一个例子,在现实中,我可能有很多很多唯一的关键字要浏览,所以返回所有的术语并循环它们并不是一个真正的解决方案。
有没有办法用ElasticSearch来实现这一点呢?
发布于 2020-05-11 17:52:42
我建议使用基于name第一个字符的过滤aggs因此来自a to g,h to n等:
正在同步几个文档:
POST jobs/_doc
{"name":"Samuel","Job":"Engineer"}
POST jobs/_doc
{"name":"Lucian","Job":"Engineer"}
POST jobs/_doc
{"name":"Teddy","Job":"Engineer"}
POST jobs/_doc
{"name":"Tod","Job":"Engineer"}
POST jobs/_doc
{"name":"Andrew","Job":"Engineer"}然后应用脚本过滤器:
GET jobs/_search
{
"size": 0,
"aggs": {
"alpha_buckets": {
"filter": {
"match_all": {}
},
"aggs": {
"a_g": {
"filter": {
"script": {
"script": {
"source": """
def first_char = doc['name.keyword'].value.toLowerCase().toCharArray()[0];
def char_int = Character.getNumericValue(first_char);
return char_int >= 10 && char_int <= 16
""",
"lang": "painless"
}
}
},
"aggs": {
"a_g": {
"terms": {
"field": "name.keyword",
"order": {
"_term": "asc"
}
}
}
}
},
"h_n": {
"filter": {
"script": {
"script": {
"source": """
def first_char = doc['name.keyword'].value.toLowerCase().toCharArray()[0];
def char_int = Character.getNumericValue(first_char);
return char_int >= 17 && char_int <= 23
""",
"lang": "painless"
}
}
},
"aggs": {
"h_n": {
"terms": {
"field": "name.keyword",
"order": {
"_term": "asc"
}
}
}
}
}
}
}
}
}让位
"aggregations" : {
"alpha_buckets" : {
"doc_count" : 7,
"h_n" : {
"doc_count" : 2,
"h_n" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Lucian",
"doc_count" : 2
},
{
"key" : "Lupe",
"doc_count" : 1
}
]
}
},
"a_g" : {
"doc_count" : 1,
"a_g" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Andrew",
"doc_count" : 1
},
{
"key" : "Anton",
"doc_count" : 1
}
]
}
}
}现在,您不需要一次获得所有按字母顺序排列的存储桶,上面的内容只是为了说明什么是可能的。
https://stackoverflow.com/questions/61725072
复制相似问题