1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Front\Search; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\Arch\Model; |
6
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
7
|
|
|
|
8
|
|
|
class EntitySearchMain extends Model |
9
|
|
|
{ |
10
|
|
|
const ITEM_PER_APP = 10; |
11
|
|
|
|
12
|
|
|
public $results = []; |
13
|
|
|
public $query; |
14
|
|
|
|
15
|
|
|
private $_configs; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* EntitySearchMain constructor. Pass query inside |
19
|
|
|
* @param string $query |
20
|
|
|
* @param array|null $configs |
21
|
|
|
*/ |
22
|
|
|
public function __construct($query, array $configs = null) |
23
|
|
|
{ |
24
|
|
|
$this->query = $query; |
25
|
|
|
$this->_configs = $configs; |
26
|
|
|
parent::__construct(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Try to search in classic apps |
31
|
|
|
*/ |
32
|
|
|
public function make() |
33
|
|
|
{ |
34
|
|
|
// search content items |
35
|
|
|
$content = new SearchContent($this->query, (int)$this->_configs['itemPerApp']); |
36
|
|
|
$this->results['Content'] = $content->getResult(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get sorted by relevance search response. Method return result as array: [relevance => [title, snippet, uri, date], ...] |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function getRelevanceSortedResult() |
44
|
|
|
{ |
45
|
|
|
$result = []; |
46
|
|
|
// each every content type |
47
|
|
|
foreach ($this->results as $type => $items) { |
48
|
|
|
if (!Obj::isArray($items)) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
// each every element |
52
|
|
|
foreach ($items as $item) { |
53
|
|
|
/** @var AbstractSearchResult $item */ |
54
|
|
|
// build unique relevance. Problem: returned relevance from query is integer |
55
|
|
|
// and can be duplicated. So, we add random complex float value and make it string to sort in feature |
56
|
|
|
$uniqueRelevance = (string)($item->getRelevance() + (mt_rand(0, 999)/10000)); |
57
|
|
|
// build response |
58
|
|
|
$result[$uniqueRelevance] = [ |
59
|
|
|
'title' => $item->getTitle(), |
60
|
|
|
'snippet' => $item->getSnippet(), |
61
|
|
|
'uri' => $item->getUri(), |
62
|
|
|
'date' => $item->getDate() |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
ksort($result); |
68
|
|
|
|
69
|
|
|
return $result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
} |