Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.08% covered (warning)
73.08%
19 / 26
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
FullTextResultsType
73.08% covered (warning)
73.08%
19 / 26
42.86% covered (danger)
42.86%
3 / 7
7.96
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getSourceFiltering
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getFields
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHighlightingConfiguration
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 transformElasticsearchResult
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
1.02
 withFetchPhaseBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createEmptyResult
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch\Search;
4
5use CirrusSearch\Search\Fetch\FetchPhaseConfigBuilder;
6use Elastica\ResultSet as ElasticaResultSet;
7
8/**
9 * Result type for a full text search.
10 */
11final class FullTextResultsType extends BaseResultsType {
12    /**
13     * @var bool
14     */
15    private $searchContainedSyntax;
16
17    /**
18     * @var FetchPhaseConfigBuilder
19     */
20    private $fetchPhaseBuilder;
21    /**
22     * @var TitleHelper
23     */
24    private $titleHelper;
25
26    /**
27     * @var string[] list of extra fields to extract
28     */
29    private $extraFieldsToExtract;
30
31    /**
32     * @param FetchPhaseConfigBuilder $fetchPhaseBuilder
33     * @param bool $searchContainedSyntax
34     * @param TitleHelper $titleHelper
35     * @param string[] $extraFieldsToExtract
36     */
37    public function __construct(
38        FetchPhaseConfigBuilder $fetchPhaseBuilder,
39        $searchContainedSyntax,
40        TitleHelper $titleHelper,
41        array $extraFieldsToExtract = []
42    ) {
43        $this->fetchPhaseBuilder = $fetchPhaseBuilder;
44        $this->searchContainedSyntax = $searchContainedSyntax;
45        $this->titleHelper = $titleHelper;
46        $this->extraFieldsToExtract = $extraFieldsToExtract;
47    }
48
49    /**
50     * @return false|string|array corresponding to Elasticsearch source filtering syntax
51     */
52    public function getSourceFiltering() {
53        return array_merge(
54            parent::getSourceFiltering(),
55            [ 'redirect.*', 'timestamp', 'text_bytes' ],
56            $this->extraFieldsToExtract
57        );
58    }
59
60    /**
61     * @return array
62     */
63    public function getFields() {
64        return [ "text.word_count" ]; // word_count is only a stored field and isn't part of the source.
65    }
66
67    /**
68     * Setup highlighting.
69     * Don't fragment title because it is small.
70     * Get just one fragment from the text because that is all we will display.
71     * Get one fragment from redirect title and heading each or else they
72     * won't be sorted by score.
73     *
74     * @param array $extraHighlightFields (deprecated and ignored)
75     * @return array|null of highlighting configuration
76     */
77    public function getHighlightingConfiguration( array $extraHighlightFields = [] ) {
78        $this->fetchPhaseBuilder->configureDefaultFullTextFields();
79        return $this->fetchPhaseBuilder->buildHLConfig();
80    }
81
82    /**
83     * @param ElasticaResultSet $result
84     * @return CirrusSearchResultSet
85     */
86    public function transformElasticsearchResult( ElasticaResultSet $result ) {
87        // Should we make this a concrete class?
88        return new class( $this->titleHelper, $this->fetchPhaseBuilder, $result, $this->searchContainedSyntax, $this->extraFieldsToExtract )
89                extends BaseCirrusSearchResultSet {
90            /** @var TitleHelper */
91            private $titleHelper;
92            /** @var FullTextCirrusSearchResultBuilder */
93            private $resultBuilder;
94            /** @var ElasticaResultSet */
95            private $results;
96            /** @var bool */
97            private $searchContainedSyntax;
98
99            public function __construct(
100                TitleHelper $titleHelper,
101                FetchPhaseConfigBuilder $builder,
102                ElasticaResultSet $results,
103                $searchContainedSyntax,
104                array $extraFieldsToExtract
105            ) {
106                $this->titleHelper = $titleHelper;
107                $this->resultBuilder = new FullTextCirrusSearchResultBuilder( $this->titleHelper,
108                    $builder->getHLFieldsPerTargetAndPriority(), $extraFieldsToExtract );
109                $this->results = $results;
110                $this->searchContainedSyntax = $searchContainedSyntax;
111            }
112
113            /**
114             * @inheritDoc
115             */
116            protected function transformOneResult( \Elastica\Result $result ) {
117                return $this->resultBuilder->build( $result );
118            }
119
120            /**
121             * @return \Elastica\ResultSet|null
122             */
123            public function getElasticaResultSet() {
124                return $this->results;
125            }
126
127            /**
128             * @inheritDoc
129             */
130            public function searchContainedSyntax() {
131                return $this->searchContainedSyntax;
132            }
133
134            protected function getTitleHelper(): TitleHelper {
135                return $this->titleHelper;
136            }
137        };
138    }
139
140    /**
141     * @param FetchPhaseConfigBuilder $builder
142     * @return FullTextResultsType
143     */
144    public function withFetchPhaseBuilder( FetchPhaseConfigBuilder $builder ): FullTextResultsType {
145        return new self( $builder, $this->searchContainedSyntax, $this->titleHelper );
146    }
147
148    /**
149     * @return CirrusSearchResultSet
150     */
151    public function createEmptyResult() {
152        return BaseCirrusSearchResultSet::emptyResultSet();
153    }
154}