Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 12, 2021 04:52 pm GMT

Query through Elasticsearch Indices with Laravel

Just come across a use case during my daily works, to query through Elasticearch indices, in Laravel.

I'm assuming you have installed Elasticsearch, have indices, index pattern defined and of course, data populated in the index documents.

Then, create a Laravel project and install this Laravel Elasticsearch package.

Then, let's create a file in your laravel project - I've create a directory called tinker/ and created tinker/es.php in it.

Then paste the following codes in the file.

<?php $stat = Elasticsearch::indices()->stats(['index'=>'your-index-*']);$indices = $stat['indices'];$field = 'username.keyword';$keyword = 'xyz';$list = [];foreach ($indices as $key => $value) {    echo $key . PHP_EOL;    $data = [        'index' => $key,        'body'  => [            'query' => [                'match' => [                    $field => $keyword,                ]            ]        ]    ];    $list[] = Elasticsearch::search($data);}dd($list);

Run the above command:

php artisan tinker tinker/es.php

The above code should return something like:

your-index-2021.09.12array:1 [  0 => array:4 [    "took" => 5    "timed_out" => false    "_shards" => array:4 [      "total" => 1      "successful" => 1      "skipped" => 0      "failed" => 0    ]    "hits" => array:3 [      "total" => array:2 [        "value" => 1        "relation" => "eq"      ]      "max_score" => 0.2876821      "hits" => array:1 [        0 => array:5 [          "_index" => "your-index-2021.09.12"          "_type" => "_doc"          "_id" => "xH652nsBOpLioOpWB7gH"          "_score" => 0.2876821          "_source" => array:7 [            "password" => "xyz"            "username" => "xyz"            "@timestamp" => "2021-09-12T15:56:03.935Z"            "port" => 50784            "tags" => array:1 [              0 => "your-index"            ]            "@version" => "1"            "host" => "localhost"          ]        ]      ]    ]  ]]

The cover image is an incoming event for JomLaunch 2021. You can buy tickets from https://launch.jomweb.my/. Every year, the JOMWEB Facebook Group community (facebook.com/groups/jomweb) which consists of programmers, developers, UI/UX designers, security experts, and other ICT activists in the country gather to highlight their projects. This is all to celebrate the country's ICT talents and expertise, and take the opportunity to get to know and learn from each other. This year 2021, JOMLAUNCH continues again, digitally.


Original Link: https://dev.to/nasrulhazim/query-through-elasticsearch-indices-with-laravel-o0g

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To