使用ElasticSearch我想插入一个条件来排序嵌套字段。
我有这个映射
{
"dario": {
"mappings": {
"agents": {
"properties": {
"applications": {
"type": "nested",
"properties": {
"companies": {
"type": "nested",
"properties": {
"active": {
"type": "integer"
},
"application_date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"application_date_month": {
"type": "date",
"format": "yyyy-MM"
},
"application_id": {
"type": "long"
},
"assigned_date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"buy_date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"date_month": {
"type": "date",
"format": "yyyy-MM"
},
"favorite": {
"type": "integer"
},
"id": {
"type": "long"
},
"notes": {
"type": "nested",
"properties": {
"date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"id": {
"type": "integer"
},
"note": {
"type": "string",
"analyzer": "standard"
}
}
},
"score": {
"type": "long"
},
"state": {
"type": "long"
},
"view": {
"type": "integer"
},
"visible": {
"type": "integer"
},
"visible_date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
}
}
},
"count": {
"type": "integer"
},
"sectors": {
"type": "long"
}
}
}
}
}
}
}
}
我想按字段applications.companies.buy_date排序,但是,如果这是null,那么我想考虑applications.companies.date 我尝试使用groovy脚本:
{
"size": 10,
"from": 0,
"sort": [
{
"_script": {
"script": "doc['applications.companies.buy_date'] != null ? doc['applications.companies.buy_date'].date.getMillisOfDay() : doc['applications.companies.date'].date.getMillisOfDay()",
"type": "number",
"nested_filter": {
"match": {
"applications.companies.id": 711
}
},
"order": "desc"
}
}
],
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"nested": {
"path": "applications.companies",
"query": {
"bool": {
"must": [
{
"match": {
"applications.companies.active": 1
}
},
{
"match": {
"applications.companies.id": 711
}
},
{
"bool": {
"should": [
{
"exists": {
"field": "applications.companies.buy_date"
}
},
{
"match": {
"applications.companies.favorite": 1
}
}
]
}
}
]
}
}
}
}
]
}
}
]
}
}
]
}
}
}
但没有任何改变。有什么想法吗?
更新
我解决了此解决方案的问题
{
"size": 10,
"from": 0,
"_source": [
"id"
],
"sort": [
{
"_script": {
"script": {
"script": " if (doc['applications.companies.id'].value == 711) { return (doc['applications.companies.buy_date'].value > 0) ? doc['applications.companies.buy_date'].value : doc['applications.companies.date'].value; } else { return null; } ",
"lang": "groovy"
},
"type": "number",
"order": "desc",
"nested_path": "applications.companies",
"nested_filter": {
"match": {
"applications.companies.id": 711
}
}
}
}
],
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"nested": {
"path": "applications.companies",
"query": {
"bool": {
"must": [
{
"match": {
"applications.companies.active": 1
}
},
{
"match": {
"applications.companies.id": 711
}
},
{
"bool": {
"should": [
{
"exists": {
"field": "applications.companies.buy_date"
}
},
{
"match": {
"applications.companies.favorite": 1
}
}
]
}
}
]
}
}
}
}
]
}
}
]
}
}
]
}
}
}