如何在弹性搜索上进行嵌套搜索?

时间:2020-07-24 23:01:19

标签: elasticsearch nested

我在ES 7.4中构建搜索查询时遇到了麻烦

这是我的地图:

    [
            'settings' => [
                'number_of_shards' => 1,
                'number_of_replicas' => 1,
                'analysis' => [
                    'filter' => [
                        'filter_stemmer' => [
                            'type' => 'stemmer',
                            'language' => 'english'
                        ]
                    ],
                    'analyzer' => [
                        'g_analyzer' => [
                            'type' => 'custom',
                            'filter' => ['lowercase', 'stemmer'],
                            'tokenizer' => 'standard'
                        ],
                        "no_stopwords" => [
                            "type" => "standard",
                            "stopwords" => []
                        ],
                    ]
                ]
            ],
            'mappings' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'id' => [
                        'type' => 'integer'
                    ],
                    'title' => [
                        'type' => 'text',
                        "analyzer" => "g_analyzer",
                    ],
                    'description' => [
                        'type' => 'text',
                        "analyzer" => "g_analyzer",
                    ],
                    'jobStatus' => [
                        'type' => 'text'
                    ],
                    'videoId' => [
                        'type' => 'text',
                    ],
                    'thumbnail' => [
                        'type' => 'text'
                    ],
                    'playlistId' => [
                        'type' => 'text'
                    ],
                    'channelId' => [
                        'type' => 'text'
                    ],
                    'publishedDate' => [ 
                        "type" => "date",
                    ],
                    'created_at' => [ //date video was updated
                        "type" => "date",
                    ],
                    'updated_at' => [ //date video was updated
                        "type" => "date",
                    ],
                    'url' => [
                        'type' => 'text'
                    ],
                    'subtitles' => [
                        'type' => 'nested',
                        'properties' => [
                            'id' => [
                                'type' => 'integer'
                            ],
                            'start_time' => [
                                'type' => 'float'
                            ],
                            'end_time' => [
                                'type' => 'float'
                            ],
                            'text' => [
                                'type' => 'text',
                                "analyzer" => "g_analyzer",
                            ],
                            'langcode' => [
                                'type' => 'text'
                            ],
                        ]
                    ]

                ]
            ]
        ];

我需要在字幕中搜索文本“ bill gates”并返回字幕“ bill gates”以及命中上下字幕的问题?

2 个答案:

答案 0 :(得分:1)

到目前为止,我还没有示例文档和预期文档,因此无法在本地尝试并提供完整的查询,但是当您使用nested datatype时,您需要使用nested queries

嵌套查询还用于查询嵌套数据类型和某些示例的相同官方文档,查看是否可以遵循它们,并提供您尝试的内容,然后我们将为您提供帮助。

答案 1 :(得分:0)

我想出了如何执行嵌套查询:

$body = [
        'query' => [
            'nested' => [
                'inner_hits'=>[
                    'size'=>3
                ],
                'path' => 'subtitles',
                'query' => [
                    'bool' => [
                         'must'=>[
                             [
                                 'match'=>[ 'subtitles.text'=>$searchTerm ]
                             ]
                         ]
                    ]

                ]
            ]
        ],
    ];

这样做会在字幕中添加实际找到的字词

相关问题