ES学习和使用笔记之arrays of objects查询

ES中JSON文档在索引中被扁平化化的 键-值 形式。所以当出现json对象数组时,扁平化后失去关联性,示例如下
PUT /my_index/blogpost/1
{
“title”: “Nest eggs”,
“body”: “Making your money work…”,
“tags”: [ “cash”, “shares” ],
“comments”: [
{
“name”: “John Smith”,
“comment”: “Great article”,
“age”: 28,
“stars”: 4,
“date”: “2014-09-01”
},
{
“name”: “Alice White”,
“comment”: “More like this please”,
“age”: 31,
“stars”: 5,
“date”: “2014-10-22”
}
]
}
最终在索引中会变成如下(structured JSON document is flattened into a simple key-value format )
{
“title”: [ eggs, nest ],
“body”: [ making, money, work, your ],
“tags”: [ cash, shares ],
“comments.name”: [ alice, john, smith, white ],
“comments.comment”: [ article, great, like, more, please, this ],
“comments.age”: [ 28, 31 ],
“comments.stars”: [ 4, 5 ],
“comments.date”: [ 2014-09-01, 2014-10-22 ]
}
nested objects 可以用来解决这个问题,每个嵌套对象会被索引为一个隐藏分割文档
{
“comments.name”: [ john, smith ],
“comments.comment”: [ article, great ],
“comments.age”: [ 28 ],
“comments.stars”: [ 4 ],
“comments.date”: [ 2014-09-01 ]
}
{
“comments.name”: [ alice, white ],
“comments.comment”: [ like, more, please, this ],
“comments.age”: [ 31 ],
“comments.stars”: [ 5 ],
“comments.date”: [ 2014-10-22 ]
}
{
“title”: [ eggs, nest ],
“body”: [ making, money, work, your ],
“tags”: [ cash, shares ]
}

设置 nested类型
PUT /my_index
{
“mappings”: {
“blogpost”: {
“properties”: {
“comments”: {
“type”: “nested”,
“properties”: {
“name”: { “type”: “string” },
“comment”: { “type”: “string” },
“age”: { “type”: “short” },
“stars”: { “type”: “short” },
“date”: { “type”: “date” }
}
}
}
}
}
}

然后查询
GET /my_index2/blogpost/_search
{
“query”: {
“nested”: {
“path”: “comments”,
“query”: {
“bool”: {
“must”: [
{
“match”: {
“comments.name”: “White”
}
},
{
“match”: {
“comments.age”: 31
}
}
]
}
}
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值