お世話になっております。今回はlaravelプロジェクトに置いてElasticsearchを導入しようとしています。
ElasticSearchに関しては完全に初心者です。
下の記事を参考にLaravel上で動くか実験中です。
【Laravel】Laravel Scout + Elasticsearchを使った全文検索の実装方法
環境
- Laradock
- PHP 7.2
- laravel 5.7.2
- ElasticSearch 6.6
- mysql 5.5
立ち上げているドッカーコンテナ
- nginx
- php-fpm
- elasticsearch
- mysql
状況
上記の記事と異なる点として、Dockerのworkspaceからlaravelを扱っている所がありエラー部分がいくつか出ましたが、Host名を調整することで動いています。
また、ElasticSearchEngineの部分でflushメソッドがないというエラーが出た為、下の空メソッドを追加して動かしています。
php
1 public function flush($models){}
データも正常に入り、記事の'登録済みのデータを取得する'のところで下のコマンドを実行すると記事と同様にデータが出力される状態です。
登録済みのデータを取得する
cmd
1// 全取得 2$ curl -H "Content-Type:application/json" -X GET elasticsearch:9200/scout/_search -d '{"query":{"match_all":{}}}' 3 4// 検索 5$ curl -H "Content-Type:application/json" -X GET "elasticsearch:9200/scout/_search" -d {\"query\":{\"match\":{\"id\":3}}} 6
問題
記事の'リクエストパラメータを使って検索してみる'を行おうとしたところデータが一つもブラウザ上に表示されず困っています。
リクエストパラメータを使って検索してみる
php
1Route::get('/post/search', function () { 2 return App\Models\Post::search(\request('q'))->paginate(); 3});
http://localhost/post/search?q=1などクエリを入れてアクセスしても以下のようにdataが入らない状態です。
json
1# 整形してあります 2{ 3 "current_page": 1, 4 "data": [], 5 "first_page_url": "http://localhost/post/search?query=&page=1", 6 "from": null, 7 "last_page": 1, 8 "last_page_url": "http://localhost/post/search?query=&page=1", 9 "next_page_url": null, 10 "path": "http://localhost/post/search", 11 "per_page": 15, 12 "prev_page_url": null, 13 "to": null, 14 "total": 0 15}
curlでは正常に検索も行えていますが、laravelを使って表示が上手くいきません。どこが問題かも検討が付かず困っています。ご教授よろしくお願い致します。
追記
** ElasticSearcEngin **
/** * Perform the given search on the engine. * * @param \Laravel\Scout\Builder $builder * @return mixed */ public function search(Builder $builder) { return $this->performSearch($builder, array_filter([ 'filters' => $this->filters($builder), 'limit' => $builder->limit, ])); } /** * @param \Laravel\Scout\Builder $builder * @param array $options * @return array|mixed */ protected function performSearch(Builder $builder, $options = []) { $params = [ 'index' => $this->index, 'type' => $builder->index ?: $builder->model->searchableAs(), 'body' => [ 'query' => [ 'bool' => [ 'must' => [ 'term' => [ 'title' => "{$builder->query}", ] ], ], ], ] ]; if ($sort = $this->sort($builder)) { $params['body']['sort'] = $sort; } if (isset($options['filters']) && count($options['filters'])) { $params['body']['query']['bool']['filter'] = $options['filters']; } if ($builder->callback) { return call_user_func( $builder->callback, $this->elastic, $builder->query, $params ); } return $this->elastic->search($params); }
回答3件
あなたの回答
tips
プレビュー