ElasticSearch中的关于对象数组查询,请注意要正确使用Nested类型,使用不当会导致检索时结果不正确。
直接上Demo演示
假设张三和李四分别喜欢喝某品牌的啤酒,青岛(黑啤和白啤)燕京(黑啤和白啤)
curl -X PUT 'http://localhost:9200/my-index-test001/_doc/1' -H 'Content-Type: application/json' -d '
{
"id" : "1",
"name": "张三",
"favoriteBeers" : [
{
"brand" : "青岛",
"type" : "黑啤"
},
{
"brand" : "燕京",
"type" : "白啤"
}
]
}'
curl -X PUT 'http://localhost:9200/my-index-test001/_doc/2' -H 'Content-Type: application/json' -d '
{
"id" : "2",
"name": "李四",
"favoriteBeers" : [
{
"brand" : "青岛",
"type" : "白啤"
},
{
"brand" : "燕京",
"type" : "黑啤"
}
]
}'
要查询喜欢喝青岛白啤的用户
curl -X GET 'http://localhost:9200/my-index-test001/_search?pretty=true' -H 'Content-Type: application/json' -d '
{
"query" :{
"bool":{
"must":[
{"match": {"favoriteBeers.brand": "青岛"}},
{"match": {"favoriteBeers.type": "白啤"}}
]
}
}
}'
查询结果如下:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.7976569,
"hits" : [
{
"_index" : "my-index-test001",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.7976569,
"_source" : {
"id" : "1",
"name" : "张三",
"favoriteBeers" : [
{
"brand" : "青岛",
"type" : "黑啤"
},
{
"brand" : "燕京",
"type" : "白啤"
}
]
}
},
{
"_index" : "my-index-test001",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.7976569,
"_source" : {
"id" : "2",
"name" : "李四",
"favoriteBeers" : [
{
"brand" : "青岛",
"type" : "白啤"
},
{
"brand" : "燕京",
"type" : "黄啤"
}
]
}
}
]
}
}
我们预想中,这个检索应该只能检索出李四,但是结果是张三也被匹配到了?为什么呢?
前面张三的文档将在内部转换为看起来更像这样的文档:
{
"name" : "张三",
"favoriteBeers.brand" : [ "青岛", "燕京" ],
"favoriteBeers.type" : [ "黑啤", "白啤" ]
}
那我们该怎么做呢?
很简单,ElasticSearch官方告诉我们,将nested字段用于对象数组即可。
如果需要索引对象数组并保持数组中每个对象的独立性,请使用nested数据类型而不是object数据类型。
我们先将这条索引删除,重新定义mapping映射文件:
curl -X DELETE 'http://localhost:9200/my-index-test001'
curl -X PUT 'http://localhost:9200/my-index-test001?pretty' -H 'Content-Type: application/json' -d '
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"favoriteBeers": {
"type": "nested",
"properties": {
"brand": {
"type": "keyword"
},
"type": {
"type": "keyword"
}
}
}
}
}
}'
重新索引之前的张三、李四
请注意这个新查询,相应要增加nested path
curl -X GET 'http://localhost:9200/my-index-test001/_search?pretty=true' -H 'Content-Type: application/json' -d '
{
"query" : {
"bool": {
"must":[
{
"nested":{
"path":"favoriteBeers",
"query":{
"bool":{
"must":[
{"match":{"favoriteBeers.brand":"青岛"}},
{"match":{"favoriteBeers.type":"白啤"}}
]
}
}
}
}
]
}
}
}'
得到正常结果:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862942,
"hits" : [
{
"_index" : "my-index-test001",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.3862942,
"_source" : {
"id" : "2",
"name" : "李四",
"favoriteBeers" : [
{
"brand" : "青岛",
"type" : "白啤"
},
{
"brand" : "燕京",
"type" : "黄啤"
}
]
}
}
]
}
}
参考文档:
https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html