Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Kunstmaan/NodeSearchBundle/Search/NodeSearcher.php (1 issue)

Upgrade to new PHP Analysis Engine

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)
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('title', 2)
95
            ;
96
        } else {
97
            $elasticaQueryTitle = new QueryString();
98
            $elasticaQueryTitle
99
              ->setDefaultField('title')
100
              ->setQuery($query);
101
        }
102
103
        $elasticaQueryBool = new BoolQuery();
104
        $elasticaQueryBool
105
            ->addShould($elasticaQueryTitle)
106
            ->addShould($elasticaQueryString)
107
            ->setMinimumShouldMatch(1);
108
109
        $this->applySecurityFilter($elasticaQueryBool);
110
111
        if (!\is_null($type)) {
112
            $elasticaQueryType = new Term();
113
            $elasticaQueryType->setTerm('type', $type);
114
            $elasticaQueryBool->addMust($elasticaQueryType);
115
        }
116
117
        $rootNode = $this->domainConfiguration->getRootNode();
118
        if (!\is_null($rootNode)) {
119
            $elasticaQueryRoot = new Term();
120
            $elasticaQueryRoot->setTerm('root_id', $rootNode->getId());
121
            $elasticaQueryBool->addMust($elasticaQueryRoot);
122
        }
123
124
        $rescore = new \Elastica\Rescore\Query();
125
        $rescore->setRescoreQuery($this->getPageBoosts());
126
127
        $this->query->setQuery($elasticaQueryBool);
128
        $this->query->setRescore($rescore);
129
        $this->query->setHighlight(
130
            array(
131
                'pre_tags' => array('<strong>'),
132
                'post_tags' => array('</strong>'),
133
                'fields' => array(
134
                    'content' => array(
135
                        'fragment_size' => 150,
136
                        'number_of_fragments' => 3,
137
                    ),
138
                ),
139
            )
140
        );
141
    }
142
143
    /**
144
     * Filter search results so only documents that are viewable by the current
145
     * user will be returned...
146
     *
147
     * @param \Elastica\Query\BoolQuery $elasticaQueryBool
148
     */
149
    protected function applySecurityFilter($elasticaQueryBool)
150
    {
151
        $roles = $this->getCurrentUserRoles();
152
153
        $elasticaQueryRoles = new Query\Terms('view_roles', $roles);
154
        $elasticaQueryBool->addMust($elasticaQueryRoles);
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    protected function getCurrentUserRoles()
161
    {
162
        $roles = array();
163
        if (!\is_null($this->tokenStorage)) {
164
            $user = $this->tokenStorage->getToken()->getUser();
165
            if ($user instanceof BaseUser) {
166
                $roles = $user->getRoles();
167
            }
168
        }
169
170
        // Anonymous access should always be available for both anonymous & logged in users
171
        if (!\in_array('IS_AUTHENTICATED_ANONYMOUSLY', $roles, true)) {
172
            $roles[] = 'IS_AUTHENTICATED_ANONYMOUSLY';
173
        }
174
175
        // Return a re-indexed array to make sure the array keys are incremental and don't skip a number. Otherwise
176
        // this causes issues in ES7.
177
        return array_values($roles);
178
    }
179
180
    /**
181
     * Apply PageType specific and Page specific boosts
182
     *
183
     * @return \Elastica\Query\BoolQuery
184
     */
185
    protected function getPageBoosts()
186
    {
187
        $rescoreQueryBool = new BoolQuery();
188
189
        //Apply page type boosts
190
        $pageClasses = $this->em->getRepository('KunstmaanNodeBundle:Node')->findAllDistinctPageClasses();
0 ignored issues
show
The method findAllDistinctPageClasses() does not exist on Doctrine\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...
191
        foreach ($pageClasses as $pageClass) {
192
            $page = new $pageClass['refEntityName']();
193
194
            if ($page instanceof SearchBoostInterface) {
195
                $elasticaQueryTypeBoost = new QueryString();
196
                $elasticaQueryTypeBoost
197
                    ->setBoost($page->getSearchBoost())
198
                    ->setDefaultField('page_class')
199
                    ->setQuery(addslashes($pageClass['refEntityName']));
200
201
                $rescoreQueryBool->addShould($elasticaQueryTypeBoost);
202
            }
203
        }
204
205
        //Apply page specific boosts
206
        $nodeSearches = $this->em->getRepository('KunstmaanNodeSearchBundle:NodeSearch')->findAll();
207
        foreach ($nodeSearches as $nodeSearch) {
208
            $elasticaQueryNodeId = new QueryString();
209
            $elasticaQueryNodeId
210
                ->setBoost($nodeSearch->getBoost())
211
                ->setDefaultField('node_id')
212
                ->setQuery($nodeSearch->getNode()->getId());
213
214
            $rescoreQueryBool->addShould($elasticaQueryNodeId);
215
        }
216
217
        return $rescoreQueryBool;
218
    }
219
}
220