我来自相关的数据库背景,在那里这样的事情很简单,但是我无法弄清楚。我已经尝试学习Elasticsearch了大约一个星期,并且试图找出我认为是嵌套查询的内容。以下是一些示例数据:
PUT /myindex/pets/_mapping
{
"pets": {
"properties": {
"name": {
"type": "string"
},
"pet": {
"type": "nested",
"properties": {
"name": {"type": "string"}
}
}
}
}
}
POST /myindex/pets/
{"pet": {"name": "rosco"}, "name": "sam smith"}
POST /myindex/pets/
{"pet": {"name": "patches"}, "name": "sam smith"}
POST /myindex/pets
{"pet": {"name": "rosco"}, "name": "jack mitchell"}
查询看起来像什么,只返回匹配的文档:
我已经尝试过
bool
,match
,nested
,filtered/filter
类型查询的混合匹配,但是我一直在出错。这样的东西在错误中脱颖而出:nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"nested\"];
这是查询:
GET /myindex/pets/_search
{
"query": {
"match": {
"name": "sam smith"
},
"nested": {
"path": "pet",
"query": {
"match": {
"pet.name": "rosco"
}
}
}
}
}
我开始认为,由于Elasticsearch的相关性质,我无法针对这一特定目标。
有任何想法吗?
最佳答案
伙计,这些查询有时很棘手...这似乎起作用:
GET /myindex/pets/_search
{
"query": {
"filtered": {
"query": {
"match": {
"name": "sam smith"
}
},
"filter": {
"nested": {
"path": "pet",
"query": {
"match": {
"pet.name": "rosco"
}
}
}
}
}
}
}
关于elasticsearch - 我认为elasticsearch嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182927/