pdphilip / laravel-elasticsearch

Laravel Elasticsearch: An Elasticsearch implementation of Laravel's Eloquent ORM
MIT License
86 stars 16 forks source link

it possible to get Random documents? #37

Open DevYSM opened 1 week ago

DevYSM commented 1 week ago

Is it possible to get Random documents? I'm trying to know if that is available in your package?

For example like this query.

$query = [
            'size' => 4,
            'query' => [
                'function_score' => [
                    'query' => [
                        'bool' => [
                            'must' => [
                                [
                                    'range' => [
                                        'ended_at' => [
                                            'gte' => now()->format('Y-m-d')
                                        ]
                                    ],

                                ],
                                [
                                    'nested' => [
                                        'path' => 'category',
                                        'query' => [
                                            'bool' => [
                                                'must' => [
                                                    [
                                                        'match' => [
                                                            'category.id' => $category_id
                                                        ]
                                                    ]
                                                ]
                                            ]
                                        ]
                                    ]
                                ],
                                [
                                    'term' => [
                                        'is_active' => true
                                    ]
                                ]
                            ],
                            'must_not' => [
                                [
                                    'terms' => [
                                        'entity_id' => [$entity_id]
                                    ]
                                ]
                            ]
                        ]
                    ],
                    'random_score' => [
                        'seed' => rand(1, 1000000),
                        'field' => 'entity_id'
                    ]
                ]
            ]
        ];
DevYSM commented 1 week ago

@pdphilip Any idea to how to do that in your package without native query?

pdphilip commented 1 week ago

Please link ES documentation so we can determine the value of this feature

DevYSM commented 1 week ago

Here you are https://www.elastic.co/guide/en/elasticsearch/guide/current/random-scoring.html

Thanks for your interested

DevYSM commented 1 week ago

Please link ES documentation so we can determine the value of this feature

Another refernace https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-random