我有两个表:用户和文章,现在我想获取一个用户列表,该列表按每个人的文章以及日期范围排序。
我设置了elasticsearch索引:
PUT /users/_mapping
{
"properties":{
"id": {
"type": "long"
},
"create_time": {
"type": "date",
"format": "yyyy-MM-dd"
},
"articles":{
"type":"nested",
"properties": {
"id": {
"type": "long"
},
"content": {
"type": "text"
},
"create_time": {
"type": "date",
"format": "yyyy-MM-dd"
}
}
}
}
}
数据如下:
{
"id":1,
"create_time": "2023-12-20",
"articles": [
{"id": 21, "content":"1111", "create_time": "2023-12-20"},
{"id": 22, "content":"1111", "create_time": "2023-12-21"},
{"id": 23, "content":"1111", "create_time": "2023-12-21"},
{"id": 24, "content":"1111", "create_time": "2023-12-22"},
{"id": 24, "content":"1111", "create_time": "2023-12-23"}
]
}
{
"id":2,
"create_time": "2023-12-20",
"articles": [
{"id": 21, "content":"1111", "create_time": "2023-12-20"},
{"id": 22, "content":"1111", "create_time": "2023-12-21"},
{"id": 23, "content":"1111", "create_time": "2023-12-22"},
{"id": 24, "content":"1111", "create_time": "2023-12-22"},
{"id": 24, "content":"1111", "create_time": "2023-12-22"},
{"id": 25, "content":"1111", "create_time": "2024-12-31"}
]
}
{
"id":3,
"create_time": "2023-12-20",
"articles": [
{"id": 21, "content":"1111", "create_time": "2023-12-20"},
{"id": 22, "content":"1111", "create_time": "2023-12-21"},
{"id": 23, "content":"1111", "create_time": "2023-12-21"},
{"id": 24, "content":"1111", "create_time": "2023-12-21"},
{"id": 24, "content":"1111", "create_time": "2023-12-22"},
{"id": 25, "content":"1111", "create_time": "2024-12-31"}
]
}
{
"id":4,
"create_time": "2023-12-10",
"articles": [
{"id": 21, "content":"1111", "create_time": "2023-12-10"},
{"id": 22, "content":"1111", "create_time": "2023-12-11"},
{"id": 23, "content":"1111", "create_time": "2023-12-21"},
{"id": 24, "content":"1111", "create_time": "2023-12-21"},
{"id": 24, "content":"1111", "create_time": "2023-12-21"},
{"id": 25, "content":"1111", "create_time": "2024-12-31"}
]
}
如果客户发布日期:2023-12-20 和 2023-12-21 将得到: create_time在2023年12月20日和2023年12月21日之间的用户; 按create_time在2023-12-20和2023-12-21之间的文章编号排序;
所以,列表数据: id: 3 有 4 篇文章 id: 1 有 3 篇文章 id: 2 有 2 篇文章
没有 id:4,因为 create_time 不在 2023-12-20 和 2023-12-21 之间
我的查询是这样的:
GET /users/_search
{
"query": {
"bool": {
"must": [{
"range": {
"create_time": {
"gte": "2023-12-20",
"lte": "2023-12-21"
}
}
}]
}
},
"aggs": {
"articles": {
"nested": {
"path": "articles"
},
"aggs": {
"articles": {
"filter": {
"bool": {
"must": [{
"range": {
"articles.create_time": {
"gte": "2023-12-20",
"lte": "2023-12-21",
"format": "yyyy-MM-dd"
}
}
}]
}
}
}
}
}
},
"size": 0
}
您需要使用reverse_nested查询。这是您更新的查询:
GET /users/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"create_time": {
"gte": "2023-12-20",
"lte": "2023-12-21"
}
}
}
]
}
},
"aggs": {
"articles": {
"nested": {
"path": "articles"
},
"aggs": {
"filter_date": {
"filter": {
"bool": {
"must": [
{
"range": {
"articles.create_time": {
"gte": "2023-12-20",
"lte": "2023-12-21",
"format": "yyyy-MM-dd"
}
}
}
]
}
},
"aggs": {
"test": {
"reverse_nested": {},
"aggs": {
"NAME": {
"terms": {
"field": "id"
},
"aggs": {
"NAME": {
"nested": {
"path": "articles"
},
"aggs": {
"NAME": {
"filter": {
"bool": {
"must": [
{
"range": {
"articles.create_time": {
"gte": "2023-12-20",
"lte": "2023-12-21",
"format": "yyyy-MM-dd"
}
}
}
]
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}