我有以下ES内容,基本上是具有针对不同用户的嵌套视频ID的视频列表。
我需要根据特定用户(例如“ B001”)的“ updated_at”日期对列表进行排序。
{ "_index": "videos",
"_source": {
"id": 123456,
"short_title": "First good title",
"created_at": "2019-05-29T03:19:14",
"user_list": [
{
"user": "A001",
"video_id": "604214",
"updated_at": "2019-05-29T03:26:48"
},
{
"user": "B001",
"video_id": "762001",
"updated_at": "2019-05-29T06:27:56"
},
{
"user": "C001",
"video_id": "604218",
"updated_at": "2019-05-29T06:27:57"
}
]
}
},
{ "_index": "videos",
"_source": {
"id": 127456,
"short_title": "Second good title",
"created_at": "2019-05-29T04:19:14",
"user_list": [
{
"user": "B001",
"video_id": "216001",
"updated_at": "2019-05-29T06:26:58"
},
{
"user": "A001",
"video_id": "604218",
"updated_at": "2019-05-29T06:25:38"
},
{
"user": "C001",
"video_id": "626001",
"updated_at": "2019-05-29T06:25:42"
}
]
}
}
我尝试了以下查询:
GET videos/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "user_list",
"filter": {
"match": {
"user_list.user": "B001"
}
}
}
}
]
}
},
"from": 0,
"size": 10,
"sort": [
{
"user_list.updated_at": {
"order": "asc",
"nested_path": "user_list",
"nested_filter": {
"match": {
"user_list.user": "B001"
}
}
}
}
]
}
但是我不能得到正确的订单。
预期订单:
{ "_index": "videos",
"_source": {
"id": 127456,
"short_title": "Second good title",
"created_at": "2019-05-29T04:19:14",
"user_list": [
{
"user": "B001",
"video_id": "216001",
"updated_at": "2019-05-29T06:26:58"
},
{
"user": "A001",
"video_id": "604218",
"updated_at": "2019-05-29T06:25:38"
},
{
"user": "C001",
"video_id": "626001",
"updated_at": "2019-05-29T06:25:42"
}
]
}
},
{ "_index": "videos",
"_source": {
"id": 123456,
"short_title": "First good title",
"created_at": "2019-05-29T03:19:14",
"user_list": [
{
"user": "A001",
"video_id": "604214",
"updated_at": "2019-05-29T03:26:48"
},
{
"user": "B001",
"video_id": "762001",
"updated_at": "2019-05-29T06:27:56"
},
{
"user": "C001",
"video_id": "604218",
"updated_at": "2019-05-29T06:27:57"
}
]
}
}
此处,id为“ 127456”的视频按顺序排在第一位,因为B001用户的“ updated_at”必须按升序排列。
查询中我缺少什么?
答案 0 :(得分:0)
映射
data10 = [go.Scatter(
x = df.portfolio_pnl,
y = df.sp500_pnl,
mode = 'markers')]
layout10= dict(
title = 'Scatter')
updatemenus = list([
dict(active=-1,
buttons=list([
dict(label = 'S&P 500',
method = 'update',
args = [{'x': 'df.sp500_pnl'},
{}]),
dict(label = 'Portfolio',
method = 'update',
args = [{'x': df.portfolio_pnl},
{}]),
]),
),
])
layout10['updatemenus'] = updatemenus
plot({
'data' : data10,
'layout' : layout10,
})
查询
PUT videos/_mapping
{
"properties" : {
"user_list" : {
"type" : "nested",
"properties" : {
"user" : {
"type" : "text"
},
"video_id" : {
"type" : "text"
},
"updated_at" : {
"type" : "date"
}
}
},
"id" : {
"type" : "integer"
},
"short_title" : {
"type" : "text"
},
"created_at" : {
"type" : "date"
}
}
}
结果
GET videos/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "user_list",
"query": {
"bool": {
"filter": {
"match": {
"user_list.user": "B001"
}
}
}
}
}
}
]
}
},
"from": 0,
"size": 10,
"sort": [
{
"user_list.updated_at": {
"order": "desc",
"nested_path": "user_list",
"nested_filter": {
"match": {
"user_list.user": "B001"
}
}
}
}
]
}
我正在使用弹性搜索7.0。 查询与您的查询相同,我按降序排序 123456作为“ user”:“ B001”,“ updated_at”:“ 2019-05-29T06:27:56”首先出现 大于“用户”:“ B001”,“ updated_at”:“ 2019-05-29T06:26:58”