搜索具有父子关系的嵌套对象

时间:2019-07-23 08:52:52

标签: elasticsearch join search

我在ElaticSearch中有一个映射,有两个嵌套类型:person和item。每个项目都由一个人拥有,并且为了表示这种关系,映射还具有连接这两种类型的联接类型。 目的是能够执行查询以同时获取商品和该商品的所有者(不仅是ID,而且还包括所有所有者的字段)。

我尝试使用带有has_parent过滤器的嵌套查询,但没有成功。我也尝试了相反的操作,一个has_parent查询,里面有嵌套查询,没有运气。每个查询都会返回一组空结果。

这是显式映射:

{
 "mappings":{
  "_doc":{
   "properties":{
    "person":{
     "type": "nested",
     "properties":{
      "name": {
       "type": "text"
      }
     }
    },
    "item": {
     "type": "nested",
     "properties": {
      "label":{
       "type":"text"
      },
      "description":{
       "type":"text"
      }
     }
    },
    "owner": {
     "type": "join",
     "relations": {
      "person": "item"
     }
    }
   }
  }
 }
}

以下是一个示例人物:

XPUT /_doc/1?refresh
{
 "person": {
  "name": "John Smith"
 },
 "owner": "person"
}

还有一个示例项目:

XPUT /_doc/1-1?routing=1
{
 "item": {
  "label": "Bag",
  "description": "Used to carry around stuff"
 },
 "owner": {
  "name": "item",
  "parent": 1
 }
}

这也是一个示例查询:

{
 "query": {
  "nested": {
   "path": "item",
   "query": {
    "has_parent": {
     "parent_type": "person",
     "query": {
      "bool": {
       "must": {
        "match": {
         "item.label": "Bag"
        }
       }
      }
     }, 
     "inner_hits": {}
    }
   }
  }
 }
}

我希望它返回我刚刚添加的项目及其在inner_hits对象中的父数据。取而代之的是,我只是找回了空的点击数。

0 个答案:

没有答案
相关问题