Elasticsearch - Has_Parent 或 Has_Child 查询返回空结果
Elasticsearch - Has_Parent or Has_Child query returning empty results
我正在使用 Elasticsearch RC 2.0.0。
我的 Elasticsearch 数据库中有一些父子关系。我想检索与父对象相关的所有子对象。我总是收到一个空的结果列表。我按照 elasticsearch 文档的说明进行操作,并将我的代码与几本书进行了比较。我不明白,为什么我的查询应该 return 一个空结果。
在这种情况下,我构建了一个简化的示例。我把两个对象放到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":{}
}
}
}
}
这将return
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
错误在这里: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": {}
}
}
}
}
我正在使用 Elasticsearch RC 2.0.0。
我的 Elasticsearch 数据库中有一些父子关系。我想检索与父对象相关的所有子对象。我总是收到一个空的结果列表。我按照 elasticsearch 文档的说明进行操作,并将我的代码与几本书进行了比较。我不明白,为什么我的查询应该 return 一个空结果。
在这种情况下,我构建了一个简化的示例。我把两个对象放到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":{}
}
}
}
}
这将return
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
错误在这里: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": {}
}
}
}
}