DudiDude
DudiDude

Reputation: 413

Elasticsearch PHP client returning no results

i am experimenting with the PHP Elasticsearch client, but i cannot find out why my code doesnt show any results

I want to search for this product (by its brand) for example. It exists in the index shop-7

this is a snapshop of a url search

now i used this code to search for the brand with ES PHP

use Elastic\Elasticsearch\ClientBuilder;    
$shop = Shop::find(7);
$query = [];
$query['bool']['must'] = ['match' => ['brand' => 'TRIUSO']];
 
$params = [
        'index' => 'shop-' . $shop->id,
        'size' => 20,
        "track_total_hits" => true,
        'body' => [
            'query' => $query
        ]
    ];

print_r($params);

$client = ClientBuilder::create()->build();
$results = $client->search($params);
print_r($results['hits']['hits']);

and i get an empty results (i added the complete params for completeness)

Array
(
    [index] => shop-7
    [size] => 20
    [track_total_hits] => 1
    [body] => Array
        (
            [query] => Array
                (
                    [bool] => Array
                        (
                            [must] => Array
                                (
                                    [match] => Array
                                        (
                                            [brand] => TRIUSO
                                        )

                                )

                        )

                )

        )

)
Array
(
)

Maybe one of you sees what i am doing wrong here. Sadly ES returns nothing but an empty array :)

Cheers Adrian

Upvotes: 0

Views: 299

Answers (1)

Paolo_Mulder
Paolo_Mulder

Reputation: 1289

Try to use the bool filter clause since you want an exact match. https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

And what is your mapping data type for this field ? https://www.elastic.co/guide/en/elasticsearch/reference/8.6/mapping-types.html

$body =[
            'query' =>
                ['bool' =>
                    ['filter' =>
                        ['term' =>
                            [
                                'brand' =>'TRIUSO'
                            ]
                        ]
                    ]
                ]
            ];

Upvotes: 0

Related Questions