2

我正在建立一个 Elasticsearch 索引,并希望它以某种方式运行(返回结果)。我已经建立了父/子关系。

curl -XPUT 'http://127.0.0.1:9200/parent/' -d '
{
  "mappings": {
    "parent": {},
    "child": {
      "_parent": {
        "type": "parent" 
      }
    }
  }
} '

我已经用一些“父”文档和一堆“子”文档填充了它,它们的父设置正确。

当我使用普通搜索查询搜索内容时,我当然会取回所有匹配的文档。父子文件,但两者之间没有联系。如果我使用 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"
      }
    }
  }
} 
}'

问题是,我想搜索孩子并在一个文档中取回父母和孩子。有没有办法做到这一点?是不是亲子关系搞错了?

4

2 回答 2

1

I want to do the same thing recently. And I figured out to print out the parent document and child documents into one elasticsearch result. But ideally it may be able to put parent and child into one json block.

Here is what I can do for now to get all the matching parent and child documents together.

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"
               }
            }
         ]
      }
   }
}'

Let's say you have 1 parent and 3 children match your condition, it will return 4 documents. 1 parent document and 3 children documents. I guess you probably want to have just 3 documents with some of the parent fields and child fields together.

That is what I want to solve next.

Hope this helps you.

于 2015-03-02T23:50:21.537 回答
1

这可能是最有效的方法,将“inner_hits”用于父/子关系

参考:https ://github.com/elastic/elasticsearch/pull/8153

于 2015-07-30T18:05:15.480 回答