Easysearch、Elasticsearch 还是 Opensearch,是个问题

如何查询nested下数据条数大于2条的数据

Elasticsearch | 作者 bufanyun | 发布于2021年07月23日 | 阅读数:2169

如何查询nested下数据条数大于2条的数据

PUT /test
{
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text"
},
"no": {
"type": "keyword"
},
"sub": {
"type": "nested",
"properties": {
"id": {
"type": "long"
},
"title": {
"type": "text"
},
"msg": {
"type": "text"
},
"num": {
"type": "keyword"
}
}
}
}
}
}
已邀请:

bufanyun - es小白

赞同来自:

测试数据:
{
"took" : 107,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"id" : "5",
"name" : "孙七",
"no" : "777",
"sub" : [
{
"id" : "1",
"title" : "好热",
"msg" : "太阳好大",
"num" : "789"
},
{
"id" : "2",
"name" : "阴天",
"msg" : "没有太阳了",
"num" : "000"
},
{
"id" : "3",
"name" : "暴雨",
"msg" : "湿了",
"num" : "000"
},
{
"id" : "4",
"name" : "冰雹",
"msg" : "湿了",
"num" : "000"
}
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : "2",
"name" : "李四",
"no" : "444",
"sub" : [
{
"id" : "1",
"title" : "好热",
"msg" : "太阳好大",
"num" : "789"
},
{
"id" : "2",
"name" : "阴天",
"msg" : "没有太阳了",
"num" : "000"
}
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"id" : "3",
"name" : "王五",
"no" : "555",
"sub" : [
{
"id" : "1",
"title" : "好热",
"msg" : "太阳好大",
"num" : "789"
},
{
"id" : "2",
"name" : "阴天",
"msg" : "没有太阳了",
"num" : "000"
}
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"id" : "4",
"name" : "马六",
"no" : "666",
"sub" : [
{
"id" : "1",
"title" : "好热",
"msg" : "太阳好大",
"num" : "789"
},
{
"id" : "2",
"name" : "阴天",
"msg" : "没有太阳了",
"num" : "000"
},
{
"id" : "3",
"name" : "暴雨",
"msg" : "湿了",
"num" : "000"
}
]
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : "1",
"name" : "张三",
"no" : "333",
"sub" : [
{
"id" : "1",
"title" : "晴天",
"msg" : "太阳好大",
"num" : "123"
},
{
"id" : "2",
"title" : "雨天",
"msg" : "下雨了",
"num" : "456"
}
]
}
}
]
},
"aggregations" : {
"price_ranges" : {
"buckets" : [
{
"key" : "4.0-*",
"from" : 4.0,
"doc_count" : 1
}
]
}
}
}

 

a2dou

赞同来自:

你可以将nested里的文档的数量单独索引一个integer类型的字段,然后用range去过滤出这个字段值大于2的文档

tongchuan1992 - 学无止境、学以致用

赞同来自:

我理解你是不是可以直接去查nested字段下面的sub.id 这个大于2的这个查询语句所得到的文档总数就是你要结果。

要回复问题请先登录注册