• -------------------------------------------------------------
  • ====================================

elasticsearch 嵌套对象查询

elasticsearch dewbay 5年前 (2019-04-12) 2596次浏览 已收录 0个评论 扫描二维码

// 创建嵌套对象 mapping
PUT /earth_index
{
“mappings”: {
“earthblog”: {
“properties”: {
“title”:{ “type”:”string”},
“body”:{ “type”:”string”},
“comments”: {
“type”: “nested”,
“properties”: {
“name”: { “type”: “string” },
“comment”: { “type”: “string” },
“age”: { “type”: “short” },
“stars”: { “type”: “short” },
“date”: { “type”: “date” }
}
}
}
}
}
}

// 插入数据
PUT /earth_index/earthblog/1
{
“title”: “Nest eggs”,
“body”: “Making your money work…”,
“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”
}
]
}

// 非嵌套查询(Alice 且 28 岁)
GET /earth_index/earthblog/_search
{
“query”: {
“bool”: {
“must”: [
{ “match”: { “comments.name”: “Alice” }},
{ “match”: { “comments.age”: 28 }}
]
}
}
}
// 输出,无结果。

// 嵌套查询(Alice 且 28 岁,或者,Alice 且 31 岁)
GET /earth_index/earthblog/_search
{
“query”: {
“nested”: {
“path”: “comments”,
“query”: {
“bool”: {
“must”: [
{
“match”: {
“comments.name”: “Alice”
}
},
{
“match”: {
“comments.age”: 28
}
}
]
}
}
}
}
}
// 输出,无结果。

// 嵌套查询(Alice 且 31 岁)
GET /earth_index/earthblog/_search
{
“query”: {
“nested”: {
“path”: “comments”,
“query”: {
“bool”: {
“must”: [
{
“match”: {
“comments.name”: “Alice”
}
},
{
“match”: {
“comments.age”: 31
}
}
]
}
}
}
}
}

// 输出,有结果,为之前插入的那条数据。

// 创建非嵌套对象,字段类型自动映射
PUT /earth_index/earthblog2/1
{
“title”: “Nest eggs”,
“body”: “Making your money work…”,
“comments2”: [
{
“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”
}
]
}

// 非嵌套查询(Alice 且 28 岁)
GET /earth_index/earthblog2/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“comments2.name”: “Alice”
}
},
{
“match”: {
“comments2.age”: 28
}
}
]
}
}
}
// 输出:上面那条数据被当做符合条件的结果。但实际上 Alice 是 31 岁。

// 嵌套查询(Alice 且 31 岁)
GET /earth_index/earthblog2/_search
{
“query”: {
“nested”: {
“path”: “comments”,
“query”: {
“bool”: {
“must”: [
{
“match”: {
“comments.name”: “Alice”
}
},
{
“match”: {
“comments.age”: 31
}
}
]
}
}
}
}
}

// 输出:无结果。
// 查看 mapping
GET /earth_index/earthblog/_mapping

{
“earth_index”: {
“mappings”: {
“earthblog”: {
“properties”: {
“body”: {
“type”: “text”
},
“comments”: {
“type”: “nested”,
“properties”: {
“age”: {
“type”: “short”
},
“comment”: {
“type”: “text”
},
“date”: {
“type”: “date”
},
“name”: {
“type”: “text”
},
“stars”: {
“type”: “short”
}
}
},
“title”: {
“type”: “text”
}
}
}
}
}
}

GET /earth_index/earthblog2/_mapping

{
“earth_index”: {
“mappings”: {
“earthblog2”: {
“properties”: {
“body”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
},
“comments2”: {
“properties”: {
“age”: {
“type”: “long”
},
“comment”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
},
“date”: {
“type”: “date”
},
“name”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
},
“stars”: {
“type”: “long”
}
}
},
“title”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
}
}
}
}
}

}

作者:earthhour
来源:CSDN
原文:https://blog.csdn.net/earthhour/article/details/79312602
版权声明:本文为博主原创文章,转载请附上博文链接!


露水湾 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:elasticsearch 嵌套对象查询
喜欢 (2)
[]
分享 (0)
关于作者:
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址