Created
September 6, 2018 04:25
-
-
Save phanngoc/ca7c59e33881f4bc86d3c61adbf1b37a 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 searchThreadES($query, $lastId, $context, $conditions = []) | |
{ | |
$conditions = $this->convertJsonToAssAr($conditions); | |
$conditions = $this->buildInitEdgeValue($conditions); | |
if ($lastId == 0) { | |
$lastId = Thread::max('id') + 1; | |
} else { | |
$lastId = (int)$lastId; | |
} | |
$filters = [ | |
[ | |
'term' => [ | |
'in_sort' => true, | |
], | |
] | |
]; | |
$filtersNot = [ | |
[ | |
'term' => ['created_at' => strtotime('1000-01-01 00:00:00')], | |
], | |
]; | |
$sorts = []; | |
$searchAfter = []; | |
$querySeri = [ | |
'bool' => [ | |
'should' => [ | |
[ | |
'has_child' => [ | |
'type' => 'message_search', | |
'score_mode' => 'max', | |
'query' => [ | |
'bool' => [ | |
'must' => [ | |
'wildcard' => [ | |
'message' => '*'.$query.'*', | |
], | |
], | |
'filter' => [ | |
['term' => ['stage' => 'operator']], | |
] | |
] | |
], | |
], | |
], | |
[ | |
'match' => ['name' => $query] | |
], | |
], | |
] | |
]; | |
if ($context == config('js.page.arrival')) { | |
$filters = array_merge($filters, [ | |
['term' => ['in_time' => Thread::IN_TIME]], | |
['term' => ['close_online' => Thread::NOT_CLOSE]], | |
]); | |
} elseif ($context == config('js.page.not_respond')) { | |
$filters = array_merge($filters, [ | |
['term' => ['out_time' => Thread::IN_TIME]], | |
['term' => ['close_offline' => Thread::NOT_CLOSE]], | |
]); | |
} elseif ($context == config('js.page.history')) { | |
$rangeMesCreate = $this->buildRangeQuery('created_at', $conditions['date_from'], $conditions['date_to']); | |
$rangeBuy = $this->buildRangeQuery('membLadt', $conditions['date_buy_from'], $conditions['date_buy_to']); | |
$filters = $this->buildNotEmpty([$rangeMesCreate, $rangeBuy]); | |
if (isset($conditions['status_member']) && $conditions['status_member'] != '') { | |
array_push($filters, [ | |
['term' => ['status_member' => $conditions['status_member']]], | |
]); | |
} | |
$querySeri = []; | |
$filters = [ | |
[ | |
'bool' => [ | |
'must' => $filters, | |
], | |
], | |
]; | |
$hasName = isset($conditions['name']) && !empty($conditions['name']); | |
if ($hasName) { | |
$querySeri = [ | |
'bool' => [ | |
'must' => [ | |
[ | |
'bool' => [ | |
'should' => [ | |
[ | |
'wildcard' => [ | |
'name' => '*'.$conditions['name'].'*', | |
], | |
], | |
], | |
], | |
], | |
], | |
], | |
]; | |
} else { | |
$querySeri = [ | |
'bool' => [ | |
'must' => [], | |
], | |
]; | |
} | |
if (isset($conditions['text_search']) && !empty($conditions['text_search'])) { | |
array_push($querySeri['bool']['must'], [ | |
'has_child' => [ | |
'type' => 'message_search', | |
'score_mode' => 'max', | |
'query' => [ | |
'bool' => [ | |
'must' => [ | |
'query_string' => [ | |
'query' => '*'.$conditions['text_search'].'*', | |
'fields' => ['name', 'message'], | |
], | |
], | |
'filter' => [ | |
['term' => ['stage' => 'operator']], | |
] | |
] | |
], | |
], | |
]); | |
} | |
} | |
array_push($sorts, ['_score' => ['order' => 'desc']]); | |
array_push($searchAfter, $conditions['params']['last_score']); | |
if (isset($conditions['sort'])) { | |
switch ($conditions['sort']) { | |
case config('js.sort.thread_sort_time_wait'): | |
array_push($sorts, ['created_at' => ['order' => 'asc']]); | |
array_push($searchAfter, $conditions['params']['created_at']); | |
break; | |
case config('js.sort.thread_sort_arrival'): | |
array_push($sorts, ['created_at' => ['order' => 'desc']]); | |
array_push($searchAfter, $conditions['params']['created_at']); | |
break; | |
case config('js.sort.thread_sort_dont_respond'): | |
array_push($sorts, ['num_respond' => ['order' => 'asc']]); | |
array_push($searchAfter, $conditions['params']['num_respond']); | |
break; | |
case config('js.sort.thread_sort_on_off'): | |
array_push($sorts, ['on_off' => ['order' => 'desc']]); | |
array_push($searchAfter, $conditions['params']['on_off']); | |
break; | |
case config('js.sort.thread_sort_status_member'): | |
array_push($sorts, ['status_member' => ['order' => 'asc']]); | |
array_push($searchAfter, $conditions['params']['status_member']); | |
break; | |
default: | |
array_push($sorts, ['created_at' => ['order' => 'asc']]); | |
array_push($searchAfter, $conditions['params']['created_at']); | |
break; | |
} | |
} | |
array_push($sorts, ['id' => ['order' => 'desc']]); | |
array_push($searchAfter, $lastId); | |
$params = [ | |
'index' => 'message', | |
'type' => 'thread_search', | |
'body' => [ | |
'query' => [ | |
'bool' => [ | |
'must' => $querySeri, | |
'filter' => $filters, | |
'must_not' => $filtersNot, | |
], | |
], | |
'track_scores' => true, | |
'sort' => $sorts, | |
'search_after' => $searchAfter, | |
'size' => 15, | |
], | |
]; | |
$client = SearchService::init()->getClient(); | |
$response = $client->search($params); | |
$last = $client->transport->getLastConnection()->getLastRequestInfo(); | |
\Log::info($last); | |
if (!empty($response['hits']['hits'])) { | |
$threadIds = array_map(function ($item) { | |
return $item['_id']; | |
}, $response['hits']['hits']); | |
return [ | |
'last_score' => end($response['hits']['hits'])['_score'], | |
'total' => $response['hits']['total'], | |
'messages' => MessageService::wrapMessageForThreadIds($threadIds), | |
]; | |
} else { | |
return [ | |
'total' => 0, | |
'messages' => [], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment