要求
- 使用与 custom_field_id 匹配的 custom_field_values 对产品进行排序
映射
{
"mapping": {
"product": {
"properties": {
"user_id": {
"type": "integer"
}
"custom_field_values": {
"type": "nested",
"properties": {
"custom_field_id": {
"type": "integer"
},
"id": {
"type": "integer"
},
"value": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
示例数据
{
{
"_type": "product",
"_source": {
"user_id": 1,
"custom_field_values": [
{ "id": 1, "custom_field_id": 1, "value": "A"},
{ "id": 2, "custom_field_id": 2, "value": "B"},
{ "id": 3, "custom_field_id": 3, "value": "C"},
]
}
},
{
"_type": "product",
"_source": {
"user_id": 2,
"custom_field_values": [
{ "id": 4, "custom_field_id": 1, "value": "Y"},
{ "id": 5, "custom_field_id": 2, "value": "Z"},
]
}
},
{
"_type": "product",
"_source": {
"user_id": 3,
"custom_field_values": [
{ "id": 6, "custom_field_id": 2, "value": "P"},
{ "id": 7, "custom_field_id": 3, "value": "Q"},
]
}
}
}
预期的
- 我应该能够对整个
product
过滤器进行custom_field_values.custom_field_id
排序,按custom_field_values.value
示例查询
{
"size":100,
"from":0,
"query":{
"bool":{
"filter":{
"match":{
"user_id":1
}
}
}
},
"sort":[
{
"custom_field_values.value.keyword":{
"order":"desc",
"nested":{
"path":"custom_field_values",
"filter":{
"match":{
"custom_field_values.custom_field_id": 2
}
}
}
}
}
]
}
更新查询
{
"size":100,
"from":0,
"query":{
"bool":{
"filter":{
"match":{
"user_id":1
}
}
},
"nested": {
"path": "comments",
"filter": {
"custom_field_values.custom_field_id": 2
}
}
},
"sort":[
{
"custom_field_values.value.keyword":{
"order":"desc",
"nested":{
"path":"custom_field_values",
"filter":{
"match":{
"custom_field_values.custom_field_id": 2
}
}
}
}
}
]
}
结果顺序应该是2nd product
, then3rd product
和1st product
。如果我想循环浏览产品并打印,custom_field_values.value
我应该得到Z
, P
, B
.