ElasticSearch匹配+父ID查询+片段

时间:2018-05-16 12:43:34

标签: elasticsearch

我有文件(父母)和页面(孩子)的亲子关系。我想编写一个查询来获取某个文档中与搜索字符串匹配的所有页面,以及检索片段。到目前为止我的代码(使用php库):

[
    'size' => 100,
    'from' => 0,
    //this sets the snippets with highlight
    'highlight' => [
        'fields' => [
            'content' => (object)[]
        ]
    ],
    'query' => [
        'bool' => [
            'must' => [
                'match' => [
                    'content' => $_GET['search']
                ],

                //only get pages in this doc
                /*'parent_id' => [
                  'type' => 'page',
                  'id' => $hit['_id']
                ]*/

                'has_parent' => [
                    'type' => 'document',
                    'ids' => [
                        'values' => [$hit['_id']]
                    ]
                ]

            ],
            /*'filter' => [
                'term' => [
                    '_parent' => $hit['_id']
                ]
            ]*/
        ]
    ],


]; 

正如您所看到的,我已经尝试过filter,has_parent和parent_id子句,但似乎没有任何工作,无论是返回空集还是错误。有没有人对如何做到这一点有任何指示?

谢谢!

1 个答案:

答案 0 :(得分:0)

对于任何偶然发现这一点的人,我找到了答案。您必须使用过滤查询:

[
    'size' => 100,
    'from' => 0,
    //this sets the snippets with highlight
    'highlight' => [
        'fields' => [
            'content' => (object)[]
        ]
    ],
    'query' => [
        'filtered' => [
            'query' => [
                'match' => [
                    'content' => $_GET['search']
                ],
            ],
            //only get pages in this doc
            'filter' => [
                'ids' => [
                    'type' => 'document',
                    'values' => [$hit['_id']]
                ]
            ]
        ]
    ],


];