These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\NodeSearchBundle\Search; |
||
4 | |||
5 | use Doctrine\ORM\EntityManager; |
||
6 | use Elastica\Query; |
||
7 | use Elastica\Query\BoolQuery; |
||
8 | use Elastica\Query\Match; |
||
9 | use Elastica\Query\QueryString; |
||
10 | use Elastica\Query\Term; |
||
11 | use Elastica\Util; |
||
12 | use Kunstmaan\AdminBundle\Entity\BaseUser; |
||
13 | use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface; |
||
14 | use Kunstmaan\NodeSearchBundle\Helper\SearchBoostInterface; |
||
15 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
16 | |||
17 | /** |
||
18 | * Default node searcher implementation |
||
19 | */ |
||
20 | class NodeSearcher extends AbstractElasticaSearcher |
||
21 | { |
||
22 | /** |
||
23 | * @var TokenStorageInterface |
||
24 | */ |
||
25 | protected $tokenStorage = null; |
||
26 | |||
27 | /** |
||
28 | * @var DomainConfigurationInterface |
||
29 | */ |
||
30 | protected $domainConfiguration; |
||
31 | |||
32 | /** |
||
33 | * @var EntityManager |
||
34 | */ |
||
35 | protected $em; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $useMatchQueryForTitle = false; |
||
41 | |||
42 | /** |
||
43 | * @param TokenStorageInterface $tokenStorage |
||
44 | */ |
||
45 | public function setTokenStorage(TokenStorageInterface $tokenStorage) |
||
46 | { |
||
47 | $this->tokenStorage = $tokenStorage; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param DomainConfigurationInterface $domainConfiguration |
||
52 | */ |
||
53 | public function setDomainConfiguration(DomainConfigurationInterface $domainConfiguration) |
||
54 | { |
||
55 | $this->domainConfiguration = $domainConfiguration; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param EntityManager $em |
||
60 | */ |
||
61 | public function setEntityManager(EntityManager $em) |
||
0 ignored issues
–
show
|
|||
62 | { |
||
63 | $this->em = $em; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param bool $useMatchQueryForTitle |
||
68 | */ |
||
69 | public function setUseMatchQueryForTitle($useMatchQueryForTitle) |
||
70 | { |
||
71 | $this->useMatchQueryForTitle = $useMatchQueryForTitle; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param mixed $query |
||
76 | * @param string $type |
||
77 | * |
||
78 | * @return mixed|void |
||
79 | */ |
||
80 | public function defineSearch($query, $type) |
||
81 | { |
||
82 | $query = Util::escapeTerm($query); |
||
83 | |||
84 | $elasticaQueryString = new Match(); |
||
85 | $elasticaQueryString |
||
86 | ->setFieldMinimumShouldMatch('content', '80%') |
||
87 | ->setFieldQuery('content', $query); |
||
88 | |||
89 | if ($this->useMatchQueryForTitle) { |
||
90 | $elasticaQueryTitle = new Match(); |
||
91 | $elasticaQueryTitle |
||
92 | ->setFieldQuery('title', $query) |
||
93 | ->setFieldMinimumShouldMatch('title', '80%') |
||
94 | ->setFieldBoost(2); |
||
95 | } else { |
||
96 | $elasticaQueryTitle = new QueryString(); |
||
97 | $elasticaQueryTitle |
||
98 | ->setDefaultField('title') |
||
99 | ->setQuery($query); |
||
100 | } |
||
101 | |||
102 | $elasticaQueryBool = new BoolQuery(); |
||
103 | $elasticaQueryBool |
||
104 | ->addShould($elasticaQueryTitle) |
||
105 | ->addShould($elasticaQueryString) |
||
106 | ->setMinimumShouldMatch(1); |
||
107 | |||
108 | $this->applySecurityFilter($elasticaQueryBool); |
||
109 | |||
110 | if (!\is_null($type)) { |
||
111 | $elasticaQueryType = new Term(); |
||
112 | $elasticaQueryType->setTerm('type', $type); |
||
113 | $elasticaQueryBool->addMust($elasticaQueryType); |
||
114 | } |
||
115 | |||
116 | $rootNode = $this->domainConfiguration->getRootNode(); |
||
117 | if (!\is_null($rootNode)) { |
||
118 | $elasticaQueryRoot = new Term(); |
||
119 | $elasticaQueryRoot->setTerm('root_id', $rootNode->getId()); |
||
120 | $elasticaQueryBool->addMust($elasticaQueryRoot); |
||
121 | } |
||
122 | |||
123 | $rescore = new \Elastica\Rescore\Query(); |
||
124 | $rescore->setRescoreQuery($this->getPageBoosts()); |
||
125 | |||
126 | $this->query->setQuery($elasticaQueryBool); |
||
127 | $this->query->setRescore($rescore); |
||
128 | $this->query->setHighlight( |
||
129 | array( |
||
130 | 'pre_tags' => array('<strong>'), |
||
131 | 'post_tags' => array('</strong>'), |
||
132 | 'fields' => array( |
||
133 | 'content' => array( |
||
134 | 'fragment_size' => 150, |
||
135 | 'number_of_fragments' => 3, |
||
136 | ), |
||
137 | ), |
||
138 | ) |
||
139 | ); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Filter search results so only documents that are viewable by the current |
||
144 | * user will be returned... |
||
145 | * |
||
146 | * @param \Elastica\Query\BoolQuery $elasticaQueryBool |
||
147 | */ |
||
148 | protected function applySecurityFilter($elasticaQueryBool) |
||
149 | { |
||
150 | $roles = $this->getCurrentUserRoles(); |
||
151 | |||
152 | $elasticaQueryRoles = new Query\Terms(); |
||
153 | $elasticaQueryRoles |
||
154 | ->setTerms('view_roles', $roles); |
||
155 | $elasticaQueryBool->addMust($elasticaQueryRoles); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return array |
||
160 | */ |
||
161 | protected function getCurrentUserRoles() |
||
162 | { |
||
163 | $roles = array(); |
||
164 | if (!\is_null($this->tokenStorage)) { |
||
165 | $user = $this->tokenStorage->getToken()->getUser(); |
||
166 | if ($user instanceof BaseUser) { |
||
167 | $roles = $user->getRoles(); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | // Anonymous access should always be available for both anonymous & logged in users |
||
172 | if (!\in_array('IS_AUTHENTICATED_ANONYMOUSLY', $roles)) { |
||
173 | $roles[] = 'IS_AUTHENTICATED_ANONYMOUSLY'; |
||
174 | } |
||
175 | |||
176 | return $roles; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Apply PageType specific and Page specific boosts |
||
181 | * |
||
182 | * @return \Elastica\Query\BoolQuery |
||
183 | */ |
||
184 | protected function getPageBoosts() |
||
185 | { |
||
186 | $rescoreQueryBool = new BoolQuery(); |
||
187 | |||
188 | //Apply page type boosts |
||
189 | $pageClasses = $this->em->getRepository('KunstmaanNodeBundle:Node')->findAllDistinctPageClasses(); |
||
0 ignored issues
–
show
The method
findAllDistinctPageClasses() does not exist on Doctrine\Common\Persistence\ObjectRepository . Did you maybe mean findAll() ?
This check marks calls to methods that do not seem to exist on an object. This is most likely the result of a method being renamed without all references to it being renamed likewise.
Loading history...
|
|||
190 | foreach ($pageClasses as $pageClass) { |
||
191 | $page = new $pageClass['refEntityName'](); |
||
192 | |||
193 | if ($page instanceof SearchBoostInterface) { |
||
194 | $elasticaQueryTypeBoost = new QueryString(); |
||
195 | $elasticaQueryTypeBoost |
||
196 | ->setBoost($page->getSearchBoost()) |
||
197 | ->setDefaultField('page_class') |
||
198 | ->setQuery(addslashes($pageClass['refEntityName'])); |
||
199 | |||
200 | $rescoreQueryBool->addShould($elasticaQueryTypeBoost); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | //Apply page specific boosts |
||
205 | $nodeSearches = $this->em->getRepository('KunstmaanNodeSearchBundle:NodeSearch')->findAll(); |
||
206 | foreach ($nodeSearches as $nodeSearch) { |
||
207 | $elasticaQueryNodeId = new QueryString(); |
||
208 | $elasticaQueryNodeId |
||
209 | ->setBoost($nodeSearch->getBoost()) |
||
210 | ->setDefaultField('node_id') |
||
211 | ->setQuery($nodeSearch->getNode()->getId()); |
||
212 | |||
213 | $rescoreQueryBool->addShould($elasticaQueryNodeId); |
||
214 | } |
||
215 | |||
216 | return $rescoreQueryBool; |
||
217 | } |
||
218 | } |
||
219 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.