|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/manganel |
|
7
|
|
|
* @license AGPL, Commercial |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
|
10
|
|
|
* @link https://maslosoft.com/manganel/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel; |
|
14
|
|
|
|
|
15
|
|
|
use function is_array; |
|
16
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
17
|
|
|
use Maslosoft\Mangan\Criteria; |
|
18
|
|
|
use Maslosoft\Mangan\Interfaces\DataProviderInterface; |
|
19
|
|
|
use Maslosoft\Mangan\Interfaces\Decorators\ConditionDecoratorTypeAwareInterface; |
|
20
|
|
|
use Maslosoft\Mangan\Interfaces\Decorators\ConditionDecoratorTypeInterface; |
|
21
|
|
|
use Maslosoft\Mangan\Transformers\CriteriaArray; |
|
22
|
|
|
use Maslosoft\Manganel\Options\MoreLike; |
|
23
|
|
|
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* SearchCriteria |
|
27
|
|
|
* |
|
28
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
29
|
|
|
*/ |
|
30
|
|
|
class SearchCriteria extends Criteria implements ConditionDecoratorTypeAwareInterface |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
use UniqueModelsAwareTrait; |
|
34
|
|
|
|
|
35
|
|
|
private $query = ''; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var MoreLike|null |
|
39
|
|
|
*/ |
|
40
|
|
|
private $moreLike = null; |
|
41
|
|
|
|
|
42
|
72 |
|
public function __construct($criteria = null, AnnotatedInterface $model = null) |
|
43
|
|
|
{ |
|
44
|
72 |
|
parent::__construct($criteria, $model); |
|
45
|
72 |
|
if (!empty($model)) |
|
46
|
|
|
{ |
|
47
|
42 |
|
$this->add($model); |
|
48
|
|
|
} |
|
49
|
72 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @internal This is used by condition decorator |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
72 |
|
public function getDecoratorType() |
|
56
|
|
|
{ |
|
57
|
72 |
|
return SearchCriteriaArray::class; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set type for searching |
|
62
|
|
|
* @param AnnotatedInterface $model |
|
63
|
|
|
* @return Criteria|void |
|
64
|
|
|
*/ |
|
65
|
46 |
|
public function setModel(AnnotatedInterface $model) |
|
66
|
|
|
{ |
|
67
|
46 |
|
$this->add($model); |
|
68
|
46 |
|
parent::setModel($model); |
|
69
|
46 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Add model to search more types |
|
73
|
|
|
* @param $model |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
46 |
|
public function add($model) |
|
77
|
|
|
{ |
|
78
|
46 |
|
$this->addModel($model); |
|
79
|
46 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Perform scored full text search |
|
84
|
|
|
* @param $query |
|
85
|
|
|
*/ |
|
86
|
41 |
|
public function search($query) |
|
87
|
|
|
{ |
|
88
|
41 |
|
$this->query = $query; |
|
89
|
41 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Fond document similar to provided document(s) or other options |
|
93
|
|
|
* @param MoreLike $options |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function moreLike(MoreLike $options) |
|
96
|
|
|
{ |
|
97
|
1 |
|
$this->moreLike = $options; |
|
98
|
|
|
// Example query |
|
99
|
|
|
// { |
|
100
|
|
|
// "query": { |
|
101
|
|
|
// "more_like_this": { |
|
102
|
|
|
// "like": [ |
|
103
|
|
|
// { |
|
104
|
|
|
// "_index": "dev_maslosoft_com", |
|
105
|
|
|
// "_type": "Content_Page", |
|
106
|
|
|
// "_id": "5ad49885a3d24b6a4d288be3" |
|
107
|
|
|
// } |
|
108
|
|
|
// ], |
|
109
|
|
|
// "min_term_freq": 1, |
|
110
|
|
|
// "max_query_terms": 25 |
|
111
|
|
|
// } |
|
112
|
|
|
// } |
|
113
|
|
|
//} |
|
114
|
1 |
|
$this->moreLike = $options; |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return MoreLike|null |
|
119
|
|
|
*/ |
|
120
|
71 |
|
public function getMoreLike() |
|
121
|
|
|
{ |
|
122
|
71 |
|
return $this->moreLike; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
71 |
|
public function getSearch() |
|
126
|
|
|
{ |
|
127
|
71 |
|
return $this->query; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|