【问题标题】:ElasticSearch match + parent ID query + snippetsElasticSearch 匹配 + 父 ID 查询 + 片段
【发布时间】:2018-05-16 21:26:34
【问题描述】:

我有文档(父)和页面(子)的父子关系。我想编写一个查询,从某个文档中获取与搜索字符串匹配的所有页面,以及检索 sn-ps。到目前为止我的代码(使用 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']
                ]
            ]*/
        ]
    ],


]; 

如您所见,我尝试了过滤器、has_parent 和 parent_id 子句,但似乎没有任何效果,返回空集或错误。有没有人对如何做到这一点有任何指示?

谢谢!

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

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

    [
        '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']]
                    ]
                ]
            ]
        ],
    
    
    ]; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      相关资源
      最近更新 更多