1.object类型
增加object类型的索引
PUT /joining_query_object_test/_doc/1
{
"obj1": {
"name": "blue",
"count": 5
},
"tags": [
"龙虎榜",
"金牛奖"
],
"comments": [
{
"name": "司马懿",
"comment": "智务其实,愚争虚名",
"age": 28,
"stars": 4,
"date": "2022-05-01"
},
{
"name": "刘伯温",
"comment": "平而后清,清而后明",
"age": 31,
"stars": 5,
"date": "2022-05-11"
}
],
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}
PUT /joining_query_object_test/_doc/2
{
"obj1": {
"name": "red",
"count": 3
},
"tags": [
"1h热股",
"24h热股"
],
"comments": [
{
"name": "刘基",
"comment": "有酒且尽欢,听我薤露歌",
"age": 42,
"stars": 3,
"date": "2022-05-02"
},
{
"name": "袁天罡",
"comment": "兴也洛水,衰也洛水",
"age": 32,
"stars": 4,
"date": "2022-05-12"
}
],
"group" : "fans",
"user" : [
{
"first" : "xiaoping",
"last" : "liu"
},
{
"first" : "tianji",
"last" : "li"
}
]
}
查看mapping
GET /joining_query_object_test/_mapping/
##查询年龄是32岁的刘基评论
##理论上应该hit 1,结果 hit 2
##刘基时42岁,32岁的是袁天罡,根本不是同一条评论
##原因:object类型底层数据结构,会将一个json数组中的数据,进行扁平化
GET /joining_query_object_test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"comments.name": "刘基"
}
},
{
"match": {
"comments.age": 32
}
}
]
}
}
}
## 验证:
##+(+(comments.name:刘 comments.name:基) +comments.age:[32 TO 32]) #MatchNoDocsQuery("")
GET /joining_query_object_test/_mapping/_validate/query?explain
{
"query": {
"bool": {
"must": [
{
"match": {
"comments.name": "刘基"
}
},
{
"match": {
"comments.age": 32
}
}
]
}
}
}
##查询first=Alice and last=Smith 出现数据不准的问题
GET joining_query_object_test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "Alice" }},
{
"match": {
"user.last": "Smith" }}
]
}
}
}
2.nested query
- doc必须是nested类型。
- nested类型就是为了解决object类型在对象数组上丢失关联性的问题的
(1)nested query
##设置mapping
DELETE joining_query_nested_test
PUT /joining_query_nested_test
{
"mappings": {
"properties": {
"obj1": {
"type": "nested"
},
"comments": {