How to make search laravel / elasticsearch with joined tables?
In my laravel 5.7/mysql app I use elasticsearch based on
https://michaelstivala.com/learning-elasticsearch-with-laravel/
article.
That is clear for me how to save/delete/search data which are based on one table.
But I have tables :
CREATE TABLE `votes` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`description` MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
with searching on name and description fields
but I also have a table
CREATE TABLE `vote_items` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`vote_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
and I want to make search also on name field.
That seems somewhat tricky. In my vote model I defined methods to sync data with elasticsearch:
<?php
namespace App;
use DB;
class Vote extends Model
{
protected $table = 'votes';
protected $elasticsearch_type = 'vote';
protected $primaryKey = 'id';
...
public function getElasticsearchType(): string
return $this->elasticsearch_type;
protected static function boot()
parent::boot();
static::saved(function ($vote)
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
if ($vote->status == 'A') // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
'body' => [
'id' => $vote->id,
'slug' => $vote->slug,
'name' => $vote->name,
'description' => $vote->description,
]
]);
// if ($vote->status == 'A') // only active votes must be saved in elasticsearch
);
static::deleted(function ($vote )
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
);
public static function bulkVotesToElastic()
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
Vote::chunk(100, function ($Votes) use ($elastic, $elasticsearch_root_index, $elasticsearch_type)
foreach ($Votes as $nextVote)
if ($nextVote->status!= 'A') continue; // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $nextVote->id,
'body' => [
'id' => $nextVote->id,
'slug' => $nextVote->slug,
'name' => $nextVote->name,
'description' => $nextVote->description,
]
]);
);
I suppose that I could make similar methods for vote_items model and save all data with different type, but I think that
is not valid way... Which way is valid? Are there some examples how this could be implemented ?
Thanks!
elasticsearch laravel-5
add a comment |
In my laravel 5.7/mysql app I use elasticsearch based on
https://michaelstivala.com/learning-elasticsearch-with-laravel/
article.
That is clear for me how to save/delete/search data which are based on one table.
But I have tables :
CREATE TABLE `votes` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`description` MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
with searching on name and description fields
but I also have a table
CREATE TABLE `vote_items` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`vote_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
and I want to make search also on name field.
That seems somewhat tricky. In my vote model I defined methods to sync data with elasticsearch:
<?php
namespace App;
use DB;
class Vote extends Model
{
protected $table = 'votes';
protected $elasticsearch_type = 'vote';
protected $primaryKey = 'id';
...
public function getElasticsearchType(): string
return $this->elasticsearch_type;
protected static function boot()
parent::boot();
static::saved(function ($vote)
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
if ($vote->status == 'A') // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
'body' => [
'id' => $vote->id,
'slug' => $vote->slug,
'name' => $vote->name,
'description' => $vote->description,
]
]);
// if ($vote->status == 'A') // only active votes must be saved in elasticsearch
);
static::deleted(function ($vote )
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
);
public static function bulkVotesToElastic()
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
Vote::chunk(100, function ($Votes) use ($elastic, $elasticsearch_root_index, $elasticsearch_type)
foreach ($Votes as $nextVote)
if ($nextVote->status!= 'A') continue; // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $nextVote->id,
'body' => [
'id' => $nextVote->id,
'slug' => $nextVote->slug,
'name' => $nextVote->name,
'description' => $nextVote->description,
]
]);
);
I suppose that I could make similar methods for vote_items model and save all data with different type, but I think that
is not valid way... Which way is valid? Are there some examples how this could be implemented ?
Thanks!
elasticsearch laravel-5
add a comment |
In my laravel 5.7/mysql app I use elasticsearch based on
https://michaelstivala.com/learning-elasticsearch-with-laravel/
article.
That is clear for me how to save/delete/search data which are based on one table.
But I have tables :
CREATE TABLE `votes` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`description` MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
with searching on name and description fields
but I also have a table
CREATE TABLE `vote_items` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`vote_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
and I want to make search also on name field.
That seems somewhat tricky. In my vote model I defined methods to sync data with elasticsearch:
<?php
namespace App;
use DB;
class Vote extends Model
{
protected $table = 'votes';
protected $elasticsearch_type = 'vote';
protected $primaryKey = 'id';
...
public function getElasticsearchType(): string
return $this->elasticsearch_type;
protected static function boot()
parent::boot();
static::saved(function ($vote)
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
if ($vote->status == 'A') // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
'body' => [
'id' => $vote->id,
'slug' => $vote->slug,
'name' => $vote->name,
'description' => $vote->description,
]
]);
// if ($vote->status == 'A') // only active votes must be saved in elasticsearch
);
static::deleted(function ($vote )
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
);
public static function bulkVotesToElastic()
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
Vote::chunk(100, function ($Votes) use ($elastic, $elasticsearch_root_index, $elasticsearch_type)
foreach ($Votes as $nextVote)
if ($nextVote->status!= 'A') continue; // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $nextVote->id,
'body' => [
'id' => $nextVote->id,
'slug' => $nextVote->slug,
'name' => $nextVote->name,
'description' => $nextVote->description,
]
]);
);
I suppose that I could make similar methods for vote_items model and save all data with different type, but I think that
is not valid way... Which way is valid? Are there some examples how this could be implemented ?
Thanks!
elasticsearch laravel-5
In my laravel 5.7/mysql app I use elasticsearch based on
https://michaelstivala.com/learning-elasticsearch-with-laravel/
article.
That is clear for me how to save/delete/search data which are based on one table.
But I have tables :
CREATE TABLE `votes` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`description` MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
with searching on name and description fields
but I also have a table
CREATE TABLE `vote_items` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`vote_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
and I want to make search also on name field.
That seems somewhat tricky. In my vote model I defined methods to sync data with elasticsearch:
<?php
namespace App;
use DB;
class Vote extends Model
{
protected $table = 'votes';
protected $elasticsearch_type = 'vote';
protected $primaryKey = 'id';
...
public function getElasticsearchType(): string
return $this->elasticsearch_type;
protected static function boot()
parent::boot();
static::saved(function ($vote)
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
if ($vote->status == 'A') // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
'body' => [
'id' => $vote->id,
'slug' => $vote->slug,
'name' => $vote->name,
'description' => $vote->description,
]
]);
// if ($vote->status == 'A') // only active votes must be saved in elasticsearch
);
static::deleted(function ($vote )
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
);
public static function bulkVotesToElastic()
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
Vote::chunk(100, function ($Votes) use ($elastic, $elasticsearch_root_index, $elasticsearch_type)
foreach ($Votes as $nextVote)
if ($nextVote->status!= 'A') continue; // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $nextVote->id,
'body' => [
'id' => $nextVote->id,
'slug' => $nextVote->slug,
'name' => $nextVote->name,
'description' => $nextVote->description,
]
]);
);
I suppose that I could make similar methods for vote_items model and save all data with different type, but I think that
is not valid way... Which way is valid? Are there some examples how this could be implemented ?
Thanks!
elasticsearch laravel-5
elasticsearch laravel-5
asked Nov 14 '18 at 7:55
user2054381
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295375%2fhow-to-make-search-laravel-elasticsearch-with-joined-tables%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295375%2fhow-to-make-search-laravel-elasticsearch-with-joined-tables%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown