我已经阅读了一些关于嵌套对象中的查询/过滤器的文章和文档,但是我无法使这个示例工作。希望你能帮我检查一下出了什么问题。Bellow是我正在使用的索引和映射设置:
# Create Index
PUT agency
# Mapping
PUT agency/site/_mapping
{
"site": {
"properties": {
"name":{
"type":"string"
},
"phones": {
"type": "nested",
"properties":{
"is_confidential": { "type": "string" },
"number": { "type": "string" },
"description": {"type" : "string"}
}
}
}
}
}
# Indexing one document
PUT agency/site/1
{
"site":{
"name":"Burger Queen",
"phones":[
{
"is_confidential":"true",
"number":"10000000000",
"description":"Manager Phone"
},
{
"is_confidential":"false",
"number":"10000000001",
"description":"Public Line"
},
{
"is_confidential":"false",
"number":"10000000002",
"description":"Public Line 2"
},
{
"is_confidential":"false",
"number":"10000000003",
"description":"Complains Phone"
}
]
}
}
# Query the nested document (https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html)
GET /agency/site/_search
{
"query": {
"bool": {
"must": [
{ "match": { "site.name": "Burger" }},
{
"nested": {
"path": "phones",
"query": {
"bool": {
"must": [
{ "match": { "phones.is_confidential": "false" }}
]
}}}}
]
}}}
# Results
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}为什么我不能得到任何结果?
我想要做的是用某种术语过滤父文档,例如: name = Burger,还可以过滤嵌套的文档,以只获得is_confidential = false的电话。
没有在嵌套文档中应用任何过滤器的示例结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "agency",
"_type": "site",
"_id": "1",
"_score": 1,
"_source": {
"site": {
"name": "Burger Queen",
"phones": [
{
"is_confidential": "true",
"number": "10000000000",
"description": "Manager Phone"
},
{
"is_confidential": "false",
"number": "10000000001",
"description": "Public Line"
},
{
"is_confidential": "false",
"number": "10000000002",
"description": "Public Line 2"
},
{
"is_confidential": "false",
"number": "10000000003",
"description": "Complains Phone"
}
]
}
}
}
]
}
}如果站点在电话数组中包含多个嵌套对象,那么elasticsearch应该只返回那些不保密的手机。
当应用is_confidential = false filter时,示例结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "agency",
"_type": "site",
"_id": "1",
"_score": 1,
"_source": {
"site": {
"name": "Burger Queen",
"phones": [
{
"is_confidential": "false",
"number": "10000000001",
"description": "Public Line"
},
{
"is_confidential": "false",
"number": "10000000002",
"description": "Public Line 2"
},
{
"is_confidential": "false",
"number": "10000000003",
"description": "Complains Phone"
}
]
}
}
}
]
}
}当is_confidential =true时,示例结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "agency",
"_type": "site",
"_id": "1",
"_score": 1,
"_source": {
"site": {
"name": "Burger Queen",
"phones": [
{
"is_confidential": "true",
"number": "10000000000",
"description": "Manager Phone"
}
]
}
}
}
]
}
}是否可以使用elasticsearch嵌套过滤器(查询)获得上述示例结果?如果可能的话,你能给我看一下样品吗?
发布于 2016-03-14 17:26:54
您需要更新PUT子句
索引一个文档
PUT agency/site/1
{
"site":{ // <-- need to remove this as it will alter the mapping definition
"name":"Burger Queen",
"phones":[
{
"is_confidential":"true",
"number":"10000000000",
"description":"Manager Phone"
},
{
"is_confidential":"false",
"number":"10000000001",
"description":"Public Line"
},
{
"is_confidential":"false",
"number":"10000000002",
"description":"Public Line 2"
},
{
"is_confidential":"false",
"number":"10000000003",
"description":"Complains Phone"
}
]
}
}
GET agency/site/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "burger"
}
},
{
"nested": {
"path": "phones",
"query": {
"term": {
"phones.is_confidential": {
"value": "true"
}
}
},
"inner_hits":{}
}
}
]
}
}
}匹配的嵌套文档将出现在内部的点击响应中。
抽样答复:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 2.2019112,
"hits": [
{
"_index": "agency",
"_type": "site",
"_id": "1",
"_score": 2.2019112,
"_source": {
"name": "Burger Queen",
"phones": [
{
"is_confidential": "true",
"number": "10000000000",
"description": "Manager Phone"
},
{
"is_confidential": "false",
"number": "10000000001",
"description": "Public Line"
},
{
"is_confidential": "false",
"number": "10000000002",
"description": "Public Line 2"
},
{
"is_confidential": "false",
"number": "10000000003",
"description": "Complains Phone"
}
]
},
"inner_hits": {
"phones": {
"hits": {
"total": 1,
"max_score": 1.9162908,
"hits": [
{
"_index": "agency",
"_type": "site",
"_id": "1",
"_nested": {
"field": "phones",
"offset": 0
},
"_score": 1.9162908,
"_source": {
"is_confidential": "true",
"number": "10000000000",
"description": "Manager Phone"
}
}
]
}
}
}
}
]
}
}https://stackoverflow.com/questions/35993142
复制相似问题