elasticsearch - 如何查询关系

时间:2014-07-29 11:57:13

标签: elasticsearch

我有现在的结构:

localhost:9200/objects/content
{
    id:1,
    author:{
        name:"john"
    },
    body:"abc"
}

localhost:9200/objects/reaction
{
    content_id:1
    message:'I like it'
}

如何通过" john"来查询以获取所有内容的反应? 这意味着查询反应,检查id指定的内容,如果作者是"某人"。

2 个答案:

答案 0 :(得分:1)

Elasticsearch blog post描述了如何管理Elasticsearch内的关系。

您的反应和内容之间需要set a parent mapping

{
    "reaction" : {
        "_parent" : {
            "type" : "content"
        }
    }
}

然后,您将反应索引为内容ID为1的孩子:

curl -XPOST localhost:9200/test/homes?parent=1 -d'
{
    message:'I like it'
}

然后,您可以使用Has Parent Query检索对名为john的作者的所有反应:

{
    "has_parent" : {
        "parent_type" : "content",
        "query" : {
            "term" : {
                "author.name" : "john"
            }
        }
    }
}

答案 1 :(得分:0)

我建议您为模型添加一些冗余:

localhost:9200/objects/reaction
{
    content_id:1
    message:'I like it'
    author_name:'john'
}

这当然会增加索引。另一方面,获取所有reactions of contents writed by "john"的查询将简单快速。