Paul Preibisch
Paul Preibisch

Reputation: 4322

How can I do a nested search on elastic search?

I am having troubles constructing a search query in ES 7.4

Here is my mapping:

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

                ]
            ]
        ];

What query do I need to search for the text "bill gates" in the subtitles, and return the subtitle "bill gates" was found in, as well as the subtitle above and below the hit?

Upvotes: 0

Views: 49

Answers (2)

Paul Preibisch
Paul Preibisch

Reputation: 4322

I figured out how to do the nested query:

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

                ]
            ]
        ],
    ];

Doing this will add an inner hits with the subtitles with the actual found terms

Upvotes: 0

Amit
Amit

Reputation: 32376

As of now I am not having your sample docs and expected docs so can't try it local and provide you complete query but as you are using nested datatype, you need to make use of nested queries.

Nested queries are used to query the nested datatype and same official doc as some examples as well, see if you can follow them, and provide what you try and from there we can help you.

Upvotes: 1

Related Questions