Skip to content

Commit

Permalink
make the search to follow the the nested objects #67
Browse files Browse the repository at this point in the history
  • Loading branch information
abidulrmdn committed Mar 20, 2017
1 parent 0d50334 commit 381734f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 70 deletions.
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ deployment:
branch: master
commands:
- rsync -avz /home/ubuntu/youtubeSearch/ [email protected]:/youtubeSearch
- ssh -i ~/.ssh/id_rsa [email protected] 'make -C /youtubeSearch/ upProduction; chmod -R 777 /youtubeSearch/y2bsearch/storage/; chmod -R 777 /youtubeSearch/y2bsearch/public/counter.txt; mv /youtubeSearch/y2bsearch/.env.prod /youtubeSearch/y2bsearch/.env'
- ssh -i ~/.ssh/id_rsa [email protected] 'make -C /youtubeSearch/ upProduction; chmod -R 777 /youtubeSearch/y2bsearch/storage/; chmod -R 777 /youtubeSearch/y2bsearch/public/counter.txt;'
8 changes: 4 additions & 4 deletions y2bsearch/app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ public function show(Request $request)
// (new GoogleAnalyticsService)->printResultss();
$search_keywords = strtolower($this->searchKeywords);
if (empty($search_keywords)) {
return $this->mainPage($search_keywords);
return $this->mainPage();
} else {
return $this->searchPage($search_keywords);
}

}

public function mainPage($search_keywords)
public function mainPage()
{
$searchProcessor = new SearchProcessor();
$client = EsService::generateESConnection();
$params = $searchProcessor->generateTopSearchQuery();
$response = $client->search($params);
$subtitlesService = new SubtitleAnalyzer();
$response = $subtitlesService->analyzeAndProcess($response, $search_keywords);
$response = $subtitlesService->analyzeAndProcess($response);
$data['videos'] = $response['hits']['hits'];
$data['mainPage'] = true;

Expand All @@ -58,7 +58,7 @@ public function searchPage($search_keywords)
$params = $searchProcessor->generateSearchQuery($search_keywords);
$response = $client->search($params);
$subtitlesService = new SubtitleAnalyzer();
$response = $subtitlesService->analyzeAndProcess($response, $search_keywords);
$response = $subtitlesService->analyzeAndProcess($response);
$data['videos'] = $response['hits']['hits'];
$data['searchKeywords'] = $this->searchKeywords;

Expand Down
49 changes: 32 additions & 17 deletions y2bsearch/app/Src/SearchService/SearchProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,45 @@ public function generateSearchQuery($searchKeywords)
'body' => [
'size' => 9,
'query' => [
'query_string' => [
'default_field' => "subtitles",
'query' => "",
'bool' => [
'must' => [
"nested" => [
'path' => "subtitles",
'inner_hits' => [
'highlight' => [
'pre_tags' => ['<b>'],
'post_tags' => ['</b>'],
"order" => "score",
'fields' => [
"subtitles.sentence" => [
"fragment_size" => 300,
"number_of_fragments" => 100,
],
],
],
],
'query' => [
'bool' => [
'must' => [],
],
],
],
],
],
],
'sort' => [
'_score' => ['order' => 'desc'],
],
'highlight' => [
'pre_tags' => ['<b>'],
'post_tags' => ['</b>'],
"order" => "score",
'fields' => [
"subtitles" => [
"fragment_size" => 300,
"number_of_fragments" => 100,
],
],
],
],
];
$searchKeywords = '*' . str_replace(' ', ' AND ', $searchKeywords);
$searchKeywords .= '*';
$params['body']['query']['query_string']['query'] = $searchKeywords;
$searchKeywords = explode(' ', $searchKeywords);
foreach ($searchKeywords as $keyword) {
$params['body']['query']['bool']['must']['nested']['query']['bool']['must'][] = [
'term' => [
'subtitles.sentence' => $keyword,
],
];
}

return $params;
}
Expand Down
51 changes: 5 additions & 46 deletions y2bsearch/app/Src/SubtitleAnalyzer/SubtitleAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,10 @@

class SubtitleAnalyzer
{
/**
* @param array $videos
* @param $searchTerm
*
* @return array
*/
public function getTiming(array $videos, $searchTerm)
{
$terms = explode(' ', $searchTerm);
$termsCount = count($terms);
foreach ($videos as $videoKey => $value) {
$index = 0;
$subtitles = $value['_source']['_subtitles'];
foreach ($subtitles['sentence'] as $key => $subtitle) {
$matchedTerms = 0;
foreach ($terms as $term) {
if (str_contains($subtitle, $term)) {
$matchedTerms++;
} else {
break;
}
}
if ($matchedTerms === $termsCount) {
$index = $key;
break;
}
}
$start = strtotime($subtitles['start'][$index]) - strtotime('TODAY');
// $end = strtotime($subtitles['end'][$index]) - strtotime('TODAY');
$sentence = $subtitles['sentence'][$index];
if($start === 0){
unset($videos[$videoKey]);
continue;
}
$videos[$videoKey]['_source']['start'] = $start;
$videos[$videoKey]['_source']['sentence'] = $sentence;
$videos[$videoKey]['_source']['video_url'] = $videos[$videoKey]['_source']['video_url'] . '&t=' . $start;
}

return $videos;
}

public function analyzeAndProcess($response, $searchKeywords)
public function analyzeAndProcess($response)
{
$responseProcessed = $response['hits']['hits'];
$responseProcessed = $this->getTiming($responseProcessed, $searchKeywords);
// $responseProcessed = $this->getTiming($responseProcessed);
$responseProcessed = $this->cleanHighlights($responseProcessed);
$response['hits']['hits'] = $responseProcessed;

Expand All @@ -70,9 +28,10 @@ private function cleanHighlights($videos)
{
$sanitizer = new Sanitizer();
foreach ($videos as $index => $video) {
$highlight = $video['highlight']['subtitles'][0];
$highlight = $video['inner_hits']['subtitles']['hits']['hits'][0]['highlight']['subtitles.sentence'];
$highlight = $sanitizer->cleanFromXMLTags($highlight);
$videos[$index]['highlight']['subtitles'][0] = $highlight;
$videos[$index]['inner_hits']['subtitles']['hits']['hits'][0]['highlight']['subtitles.sentence'] =
$highlight;
}

return $videos;
Expand Down
2 changes: 1 addition & 1 deletion y2bsearch/resources/views/components/videoCard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="content title">
{{ $video['video_title'] }}
</div>
<a target="_blank" data-embedhref="https://www.youtube.com/embed/{{ $video['video_hash_id'] }}?start={{ $video['start'] }}&cc_load_policy=1&autoplay=1"
<a target="_blank" data-embedhref="https://www.youtube.com/embed/{{ $video['video_hash_id'] }}?start={{ strtotime($start ) - strtotime('TODAY')}}&cc_load_policy=1&autoplay=1"
data-id="{{ $video['video_hash_id'] }}" class="image .valencia .lark">
<img src="{{$video['image_medium']}}">
</a>
Expand Down
5 changes: 4 additions & 1 deletion y2bsearch/resources/views/subviews/videos.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<div id="videos" class="ui link cards three column stackable grid container">
@foreach ($videos as $video)
@include('components.videoCard', ['video' => $video['_source'], 'highlighted_search' => $video['highlight']['subtitles'][0]])
@include('components.videoCard', ['video' => $video['_source'],
'highlighted_search' => $video['inner_hits']['subtitles']['hits']['hits'][0]['highlight']['subtitles.sentence'][0],
'start' => $video['inner_hits']['subtitles']['hits']['hits'][0]['_source']['start']
])
@endforeach
</div>

0 comments on commit 381734f

Please sign in to comment.