poc: anime search with elasticsearch

This commit is contained in:
Irfan 2022-04-10 07:59:41 +05:00
parent 54e485215e
commit bc960669e3
5 changed files with 67 additions and 97 deletions

View File

@ -20,6 +20,17 @@ DB_ADMIN=jikan
DB_USERNAME= DB_USERNAME=
DB_PASSWORD= DB_PASSWORD=
###
# ElasticSearch
###
ELASTICSEARCH=true
ELASTICSEARCH_HOST=localhost
ELASTICSEARCH_PORT=9200
ELASTICSEARCH_SCHEME=http
ELASTICSEARCH_USER=elastic
ELASTICSEARCH_PASS=
### ###
# Database query default values # Database query default values
### ###

View File

@ -0,0 +1,51 @@
<?php
namespace App\Http\Controllers\V4DB;
use Elastic\Elasticsearch\ClientBuilder;
use Elastic\Elasticsearch\Exception\AuthenticationException;
use Illuminate\Http\Request;
class ElasticSearchController extends Controller
{
/**
* @throws AuthenticationException
*/
public function anime(Request $request)
{
$page = $request->get('page') ?? 1;
$limit = $request->get('limit') ?? env('MAX_RESULTS_PER_PAGE', 25);
$q = $request->get('q') ?? '';
if (!is_null($limit)) {
$limit = (int) $limit;
if ($limit <= 0)
$limit = 1;
if ($limit > env('MAX_RESULTS_PER_PAGE', 25))
$limit = env('MAX_RESULTS_PER_PAGE', 25);
}
$client = ClientBuilder::create()
->setHosts([env('ELASTICSEARCH_HOST', 'localhost').":".env('ELASTICSEARCH_PORT', 9200)])
->setBasicAuthentication(env('ELASTICSEARCH_USER', 'elastic'), env('ELASTICSEARCH_PASS', ''))
->build();
$response = $client->search([
'index' => 'jikan.anime',
'body' => [
'query' => [
'multi_match' => [
'query' => $q,
'fields' => ['title^3', 'title_english', 'title_japanese', 'title_synonyms']
]
]
]
]);
var_dump($response['hits']['hits']);
die;
}
}

View File

@ -25,8 +25,7 @@
"sentry/sentry-laravel": "^2.8", "sentry/sentry-laravel": "^2.8",
"symfony/yaml": "^4.1", "symfony/yaml": "^4.1",
"vlucas/phpdotenv": "^5", "vlucas/phpdotenv": "^5",
"zircote/swagger-php": "3.*", "zircote/swagger-php": "3.*"
"cviebrock/laravel-elasticsearch": "dev-master"
}, },
"require-dev": { "require-dev": {
"mockery/mockery": "^1.3.1", "mockery/mockery": "^1.3.1",

96
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "55e468117b87175f71fe77f3db671e9e", "content-hash": "1168ceaafba4b640016a4f14164f3d07",
"packages": [ "packages": [
{ {
"name": "brick/math", "name": "brick/math",
@ -199,97 +199,6 @@
], ],
"time": "2022-02-21T13:15:14+00:00" "time": "2022-02-21T13:15:14+00:00"
}, },
{
"name": "cviebrock/laravel-elasticsearch",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/jikan-me/laravel-elasticsearch.git",
"reference": "c06032c816760c63f046f636f5e487db2d368215"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/jikan-me/laravel-elasticsearch/zipball/c06032c816760c63f046f636f5e487db2d368215",
"reference": "c06032c816760c63f046f636f5e487db2d368215",
"shasum": ""
},
"require": {
"elasticsearch/elasticsearch": "^8.1",
"ext-json": "*",
"guzzlehttp/psr7": "^1.7",
"illuminate/contracts": "^7.0|^8.0|^9.0",
"illuminate/support": "^7.0|^8.0|^9.0",
"php": "^7.3|^8.0",
"psr/http-message": "^1.0"
},
"require-dev": {
"limedeck/phpunit-detailed-printer": "^6.0",
"mockery/mockery": "^1.4.3",
"orchestra/testbench": "^6.5|^7.0",
"phpunit/phpunit": "^9.4"
},
"suggest": {
"aws/aws-sdk-php": "Required to connect to an Elasticsearch host on AWS (^3.80)"
},
"default-branch": true,
"type": "library",
"extra": {
"laravel": {
"providers": [
"Cviebrock\\LaravelElasticsearch\\ServiceProvider"
],
"aliases": {
"Elasticsearch": "Cviebrock\\LaravelElasticsearch\\Facade"
}
}
},
"autoload": {
"psr-4": {
"Cviebrock\\LaravelElasticsearch\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Cviebrock\\LaravelElasticsearch\\Tests\\": "tests"
}
},
"scripts": {
"fresh": [
"rm -rf vendor composer.lock",
"composer install"
],
"tests": [
"php vendor/bin/phpunit"
]
},
"license": [
"MIT"
],
"authors": [
{
"name": "Colin Viebrock",
"email": "colin@viebrock.ca"
}
],
"description": "An easy way to use the official PHP ElasticSearch client in your Laravel applications.",
"homepage": "https://github.com/cviebrock/laravel-elasticsearch",
"keywords": [
"client",
"elasticsearch",
"laravel",
"search"
],
"support": {
"source": "https://github.com/jikan-me/laravel-elasticsearch/tree/master"
},
"funding": [
{
"type": "github",
"url": "https://github.com/cviebrock"
}
],
"time": "2022-04-10T02:10:18+00:00"
},
{ {
"name": "danielmewes/php-rql", "name": "danielmewes/php-rql",
"version": "dev-master", "version": "dev-master",
@ -10502,8 +10411,7 @@
"minimum-stability": "dev", "minimum-stability": "dev",
"stability-flags": { "stability-flags": {
"danielmewes/php-rql": 20, "danielmewes/php-rql": 20,
"jikan-me/jikan": 20, "jikan-me/jikan": 20
"cviebrock/laravel-elasticsearch": 20
}, },
"prefer-stable": true, "prefer-stable": true,
"prefer-lowest": false, "prefer-lowest": false,

View File

@ -23,9 +23,10 @@ $router->get('/', function () use ($router) {
}); });
$router->get('/anime', [ $router->get('/anime', [
'uses' => 'SearchController@anime' 'uses' => env('ELASTICSEARCH', false) ? 'ElasticSearchController@anime' : 'SearchController@anime'
]); ]);
$router->group( $router->group(
[ [
'prefix' => 'anime/{id:[0-9]+}' 'prefix' => 'anime/{id:[0-9]+}'