gpt4 book ai didi

elasticsearch - ElasticSearch计算嵌套字段

转载 作者:行者123 更新时间:2023-12-02 22:16:35 25 4
gpt4 key购买 nike

如何计算在ElasticSearch中满足一定条件的嵌套字段(嵌套对象列表)的对象?

示例

具有“客户”索引,类型为“客户”,其“服务”嵌套字段具有以下结构:

public class Customer
{
public int Id;
public List<Service> Services;
}

public class Service
{
public int Id;
public DateTime Date;
public decimal Rating;
}

如何计算2017年6月发生的所有服务获得的评分高于5?

最佳答案

好问题 :)
为了获得所需的内容,您应该预先定义映射,并且嵌套的属性映射效果很好。

嵌套类型是对象数据类型的一种特殊版本,它允许对对象数组进行索引并分别查询彼此之间的,以及相互查询
https://www.elastic.co/guide/en/elasticsearch/reference/2.4/nested.html

请在下面找到完整的示例:)

映射

PUT example_nested_test
{
"mappings": {
"nested": {
"properties": {
"Id": {
"type": "long"
},
"Services": {
"type": "nested",
"properties": {
"Id": {
"type": "long"
},
"Date": {
"type": "date"
},
"Rating": {
"type": "long"
}
}
}
}
}
}
}

POST数据
POST example_nested_test/nested/100
{
"Id" : 1,
"Services": [
{
"Id": 1,
"Date" :"2017-05-10",
"Rating" : 5
},
{
"Id": 2,
"Date" :"2013-05-10",
"Rating" : 2
},
{
"Id": 4,
"Date" :"2017-05-10",
"Rating" : 6
}]
}

查询
GET example_nested_test/_search
{
"size":0,
"aggs": {
"Services": {
"nested": {
"path": "Services"
},
"aggs": {
"Rating": {
"filter": {
"bool": {
"must": [
{
"range": {
"Services.Date": {
"gt": "2017",
"format": "yyyy"
}
}
},
{
"range": {
"Services.Rating": {
"gt": "5"
}
}
}
]
}
}
}
}
}
}
}

结果:
 "aggregations": {
"Services": {
"doc_count": 3,
"Rating": {
"doc_count": 1
}
}
}

关于elasticsearch - ElasticSearch计算嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44889690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com