你可以的,加油

ES查询nested类型的数组元素个数大于20的集合

Elasticsearch | 作者 throwable | 发布于2020年09月09日 | 阅读数:2729

Mapping如下:
{
"user-tag" : {
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"createTime" : {
"type" : "long"
},
"editTime" : {
"type" : "long"
},
"handleTime" : {
"type" : "long"
},
"identityNo" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"phone" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"sex" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"tags" : {
"type" : "nested",
"properties" : {
"tagCode" : {
"type" : "keyword"
},
"tagDescription" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"userId" : {
"type" : "long"
}
}
}
}
}
一条数据如下:
{
"_index" : "user-tag",
"_type" : "_doc",
"_id" : "111",
"_score" : 1.0,
"_source" : {
"age" : 50,
"createTime" : 1598457626303,
"editTime" : 1599581034922,
"handleTime" : 1599581034922,
"identityNo" : "22",
"name" : "张小狗",
"phone" : "33",
"sex" : "female",
"tags" : [
{
"tagCode" : "age_20_50"
},
{
"tagCode" : "tag-B"
},
{
"tagCode" : "tag-C"
}
],
"userId" : 111
}
}
请求有什么办法查询嵌套对象数组tags下元素大于20个的所有记录的集合或者数量?此问题在Stackoverflow和多个国内站点都找不到解决方案,请求大佬们支招。
已邀请:

FFFrp

赞同来自:

GET tags/_search
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "script" : {
                    "source": "params._source.tags.size()"
                },
                "ranges" : [
                    { "from" : 20 }
                ]
            }
        }
    }
}
试试

要回复问题请先登录注册