我正在设置一个Elasticsearch索引,并希望它以某种方式表现(返回结果)。我已经建立了父母/子女关系。
curl -XPUT 'http://127.0.0.1:9200/parent/' -d '
{
"mappings": {
"parent": {},
"child": {
"_parent": {
"type": "parent"
}
}
}
} '
我已经和一些父母一起填充了#34;文件和一堆儿童"正确设置父级的文档。
当我使用普通搜索查询搜索内容时,我当然要取回所有匹配的文档。父母和子女文件,但之间没有关系。如果我使用has_child过滤器搜索内容,它会正确搜索子文档并返回匹配的父文档:
curl -XGET 'http://127.0.0.1:9200/parent/_search' -d '
{
"query": {
"has_child": {
"type": "child",
"query": {
"match": {
"detail": "Stuff I Want To Match"
}
}
}
}
}'
问题是,我想搜索孩子并在单个文档中找回父母和孩子。有没有办法实现这个目标?亲子关系是错误的吗?
答案 0 :(得分:1)
我最近想做同样的事情。我想出将父文档和子文档打印成一个elasticsearch结果。 但理想情况下,它可以将父母和孩子放在一个json区块中。
现在我可以做的就是将所有匹配的父文档和子文档放在一起。
curl -XGET 'http://127.0.0.1:9200/indexname/parent,child/_search' -d '
{
"query": {
"bool": {
"should": [
{
"has_child": {
"type": "child",
"query": {
"match": {
"detail": "Stuff I Want To Match"
}
}
}
},
{
"match": {
"detail": "Stuff I Want To Match"
}
}
]
}
}
}'
假设您有1个父母和3个孩子符合您的条件,它将返回4个文件。 1份父文件和3份儿童文件。我猜你可能想要只有3个文件,其中包含一些父字段和子字段。
这就是我想要解决的问题。
希望这会对你有所帮助。
答案 1 :(得分:1)
这可能是最有效的方法,使用" inner_hits"父母/子女关系