我是elasticsearch的新手,刚开始尝试利用elasticsearch在nodejs上进行全文搜索。目前我有一个问题,找出计数嵌套字段的项目数。例如,假设这是我的数据:
{
user1: {
movies: [
{
title: 'eat'
},
{
title: 'drink'
}
],
books: [
{
author: 'apple'
}
]
},
user2: {
movies: [
{
title: 'jump'
},
{
title: 'punch'
},
{
title: 'sleep'
},
{
title: 'eat'
}
],
books: [
{
author: 'orange'
}
]
}
}
如何简单计算user1或user2有多少部电影?(在这种情况下为2和4)。
如果结果令人惊讶地困难,我仍然可以回退到将该号码存储在其他地方。
答案 0 :(得分:0)
let users = {
user1: {
movies: [
{
title: 'eat'
},
{
title: 'drink'
}
],
books: [
{
author: 'apple'
}
]
},
user2: {
movies: [
{
title: 'jump'
},
{
title: 'punch'
},
{
title: 'sleep'
},
{
title: 'eat'
}
],
books: [
{
author: 'orange'
}
]
}
}
console.log(`User1 count: ${users.user1.movies.length}`);
console.log(`User2 count: ${users.user2.movies.length}`);