-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from moitran/3-publish-books-from-master-data-m…
…ysql-to-elasticsearch-and-switch-api-query-endpoint-to-elasticsearch Publish MYSQL data to ES & searching books on ES
- Loading branch information
Showing
15 changed files
with
428 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\ElasticSearch; | ||
|
||
use App\ElasticSearch\QueryBuilder\BookElasticQueryBuilder; | ||
use App\ElasticSearch\QueryBuilder\ElasticQueryBuilderInterface; | ||
|
||
class ElasticSearchQueryBuilderFactory | ||
{ | ||
public function createBookElasticQueryBuilder(): ElasticQueryBuilderInterface | ||
{ | ||
return new BookElasticQueryBuilder(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
app/ElasticSearch/QueryBuilder/BookElasticQueryBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace App\ElasticSearch\QueryBuilder; | ||
|
||
use App\Http\Requests\Books\IndexRequest; | ||
use App\Models\Book; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use InvalidArgumentException; | ||
use JeroenG\Explorer\Domain\Query\QueryProperties\TrackTotalHits; | ||
use JeroenG\Explorer\Domain\Syntax\Compound\BoolQuery; | ||
use JeroenG\Explorer\Domain\Syntax\Matching; | ||
use JeroenG\Explorer\Domain\Syntax\Nested; | ||
use JeroenG\Explorer\Domain\Syntax\Term; | ||
use JeroenG\Explorer\Infrastructure\Scout\Builder; | ||
|
||
class BookElasticQueryBuilder implements ElasticQueryBuilderInterface | ||
{ | ||
public function build(FormRequest $request): Builder | ||
{ | ||
if (! $request instanceof IndexRequest) { | ||
throw new InvalidArgumentException('Request must be an instance of IndexRequest'); | ||
} | ||
|
||
$query = $request->query('query'); | ||
$categoryId = $request->query('category_id'); | ||
$providerId = $request->query('provider_id'); | ||
|
||
/** @var Builder $builder */ | ||
$builder = Book::search(); | ||
|
||
if ($query) { | ||
$bool = new BoolQuery(); | ||
// title match query OR author match query OR book_number match query | ||
$bool->should(new Matching('title', $query)); | ||
$bool->should(new Matching('author', $query)); | ||
$bool->should(new Matching('book_number', $query)); | ||
$builder->filter($bool); | ||
} | ||
|
||
if ($categoryId) { | ||
$builder->must(new Nested('category', new Term('category.id', $categoryId))); | ||
} | ||
|
||
if ($providerId) { | ||
$builder->must(new Nested('provider', new Term('provider.id', $providerId))); | ||
} | ||
|
||
$builder->property(TrackTotalHits::all()); | ||
|
||
return $builder; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/ElasticSearch/QueryBuilder/ElasticQueryBuilderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\ElasticSearch\QueryBuilder; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use JeroenG\Explorer\Infrastructure\Scout\Builder; | ||
|
||
interface ElasticQueryBuilderInterface | ||
{ | ||
public function build(FormRequest $request): Builder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.