我正在使用Elasticsearch RC 2.0.0。
我的Elasticsearch数据库中有一些父子关系。我想检索与父对象相关的所有子项。我总是收到一个空的结果列表。我按照elasticsearch文档的说明进行操作,并将我的代码与几本书进行了比较。我不明白,为什么我的查询应该返回一个空结果。
在这种情况下,我构建了一个简化的例子。我将两个对象放到elasticsearch中,并将对象a设置为对象b的父对象。然后我尝试检索所有具有类型a的父类的对象。
这是我的输入:
PUT test
PUT test/myobject/_mapping
{
"myobject":{
"_parent" : {"type":"pobject"},
"properties" : {
"name" : {"type":"string"}
}
}
}
PUT test/pobject/_mapping
{
"pobject" : {
"properties": {
"name": {"type":"string"}
}
}
}
PUT test/pobject/1
{
"name":"theParent"
}
PUT test/myobject/1?_parent=1&routing=_id
{
"name":"theChild"
}
POST test/myobject/_search?routing=_id
{
"query":{
"has_parent":{
"type":"pobject",
"query":{
"match_all":{}
}
}
}
}
这将返回
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
答案 0 :(得分:3)
错误在于:PUT test/myobject/1?_parent=1&routing=_id
参数为parent
,而不是_parent
。
POST test/myobject/1?parent=1
{
"name": "theChild"
}
此外,您不需要使用routing=_id
。请参阅documentation。
要测试的完整命令列表:
DELETE test
PUT test
PUT test/myobject/_mapping
{
"myobject": {
"_parent": {
"type": "pobject"
},
"properties": {
"name": {
"type": "string"
}
}
}
}
PUT test/pobject/_mapping
{
"pobject": {
"properties": {
"name": {
"type": "string"
}
}
}
}
POST test/pobject/1
{
"name": "theParent"
}
POST test/myobject/1?parent=1
{
"name": "theChild"
}
POST test/myobject/_search
{
"query": {
"has_parent": {
"parent_type": "pobject",
"query": {
"match_all": {}
}
}
}
}