-
-
Save domenicozinzidea/20ced7f6222773f13d6a59e2e7085497 to your computer and use it in GitHub Desktop.
This file contains hidden or 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