arrays - ElasticSearch 按数组第一个索引处的 NestedObject 中的字段排序
问题描述
我正在尝试在以下文档中对数组的第一个对象内的字段进行排序每个文档都有一个数组我想检索按它们排序的文档的第一个对象按那里的城市名称让我们在以下结果中命名我想要的第一个第三个文件,因为城市的名称以“L”('london')开头,然后是第二个“M”('Moscow'),然后是第三个“N”('NYC')
该结构是一个记录:
- 有一个数组
- 该数组包含一个对象(称为“地址”)
- 该对象有一个字段(称为“城市”)
我想按第一个 address.cities 对文档进行排序
get hello/_mapping
{
"hello": {
"mappings": {
"jack": {
"properties": {
"houses": {
"type": "nested",
"properties": {
"address": {
"properties": {
"city": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
}
}
这些是我索引的文档
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "hello",
"_type": "jack",
"_id": "2",
"_score": 1,
"_source": {
"houses": [
{
"address": {
"city": "moscow"
}
},
{
"address": {
"city": "belgrade"
}
},
{
"address": {
"city": "Sacramento"
}
}
]
}
},
{
"_index": "hello",
"_type": "jack",
"_id": "1",
"_score": 1,
"_source": {
"houses": [
{
"address": {
"city": "NYC"
}
},
{
"address": {
"city": "PARIS"
}
},
{
"address": {
"city": "TLV"
}
}
]
}
},
{
"_index": "hello",
"_type": "jack",
"_id": "3",
"_score": 1,
"_source": {
"houses": [
{
"address": {
"city": "London"
}
}
]
}
}
]
}
}
解决方案
试试这个(当然,如果字段可能为空,请在脚本中添加一些测试。注意它可能会很慢,因为弹性不会索引这个值。添加一个主地址字段肯定会更快(并且真的更快)并且会做它的好方法。
{
"sort" : {
"_script" : {
"script" : "params._source.houses[0].address.city",
"type" : "string",
"order" : "asc"
}
}
}
您必须使用 _source 而不是 doc[yourfield] 因为您不知道以女巫顺序弹性存储您的数组。
编辑:测试字段是否存在
{
"query": {
"nested": {
"path": "houses",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "houses.address"
}
}
]
}
}
}
},
"sort": {
"_script": {
"script" : "params._source.houses[0].address.city",
"type": "string",
"order": "asc"
}
}
}
推荐阅读
- intellij-idea - IntelliJ - 语言级别 5 不支持菱形类型和 Lambda 表达式
- javascript - 如何使用 jquery 访问 $(this) 关键字下的子元素
- javascript - Webpack 热重载非常慢
- asp.net-mvc - 剑道 UI 多重网格
- javascript - Angular Firestore 根据返回的布尔值显示一个按钮,用于检查 Firestore 文档是否存在
- sql - 如何创建连接两个表的视图以显示一个表中的哪些实体未连接到另一个表中的实体?
- symfony - 如何在 Symfony3.4 中通过用户名使用 Lock 组件
- qt - 如何测量 QML 文件中的导入时间?
- amazon-ec2 - 如何调用具有变量的资源名称
- javascript - 无法在表格行上设置焦点