-
-
Save truongluu/f010b513f84d6c76e4a939f5a976f17e to your computer and use it in GitHub Desktop.
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
public function search(Request $request) | |
{ | |
if($request->has('text') && $request->input('text')) { | |
// Search for given text and return data | |
$data = $this->searchMovies($request->input('text')); | |
$moviesArray = []; | |
// If there are any movies that match given search text "hits" fill their id's in array | |
if($data['hits']['total'] > 0) { | |
foreach ($data['hits']['hits'] as $hit) { | |
$moviesArray[] = $hit['_source']['id']; | |
} | |
} | |
// Retrieve found movies from database | |
$movies = Movie::with('movie_actors') | |
->whereIn('id', $moviesArray) | |
->get(); | |
// Return to view with data | |
return view('movies.index', ['movies' => $movies]); | |
} else { | |
return redirect()->route('movies'); | |
} | |
} | |
private function searchMovies($text) | |
{ | |
$params = [ | |
'index' => Movie::ELASTIC_INDEX, | |
'type' => Movie::ELASTIC_TYPE, | |
'body' => [ | |
'sort' => [ | |
'_score' | |
], | |
'query' => [ | |
'bool' => [ | |
'should' => [ | |
['match' => [ | |
'name' => [ | |
'query' => $text, | |
'fuzziness' => '2' | |
] | |
]], | |
['match' => [ | |
'description' => [ | |
'query' => $text, | |
'fuzziness' => '1' | |
] | |
]], | |
['match' => [ | |
'actors' => [ | |
'query' => $text, | |
'fuzziness' => '1' | |
] | |
]] | |
] | |
], | |
], | |
] | |
]; | |
$data = $this->client->search($params); | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment