Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.32% covered (success)
90.32%
28 / 31
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SemanticResultsType
90.32% covered (success)
90.32%
28 / 31
83.33% covered (warning)
83.33%
5 / 6
6.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
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
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHighlightingConfiguration
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 transformElasticsearchResult
85.00% covered (warning)
85.00%
17 / 20
0.00% covered (danger)
0.00%
0 / 1
1.00
 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 Elastica\ResultSet as ElasticaResultSet;
6
7/**
8 * Result type for a full text search.
9 */
10final class SemanticResultsType extends BaseResultsType {
11    private TitleHelper $titleHelper;
12    /* @var string[] list of extra fields to extract */
13    private array $extraFieldsToExtract;
14    private array $queryBuilderProfile;
15
16    /**
17     * @param TitleHelper $titleHelper
18     * @param string[] $extraFieldsToExtract
19     */
20    public function __construct(
21        TitleHelper $titleHelper,
22        array $extraFieldsToExtract,
23        array $queryBuilderProfile,
24    ) {
25        $this->titleHelper = $titleHelper;
26        $this->extraFieldsToExtract = $extraFieldsToExtract;
27        $this->queryBuilderProfile = $queryBuilderProfile;
28    }
29
30    /**
31     * @return false|string|array corresponding to Elasticsearch source filtering syntax
32     */
33    public function getSourceFiltering() {
34        return array_merge(
35            parent::getSourceFiltering(),
36            [ 'timestamp', 'text_bytes' ],
37            $this->extraFieldsToExtract
38        );
39    }
40
41    /**
42     * @return array
43     */
44    public function getFields() {
45        return [ "text.word_count" ]; // word_count is only a stored field and isn't part of the source.
46    }
47
48    /**
49     * @param array $extraHighlightFields (deprecated and ignored)
50     * @return array|null of highlighting configuration
51     */
52    public function getHighlightingConfiguration( array $extraHighlightFields = [] ) {
53        return null;
54    }
55
56    /**
57     * @param ElasticaResultSet $result
58     * @return CirrusSearchResultSet
59     */
60    public function transformElasticsearchResult( ElasticaResultSet $result ) {
61        // Should we make this a concrete class?
62        return new class(
63            $this->titleHelper,
64            $result,
65            $this->extraFieldsToExtract,
66            $this->queryBuilderProfile,
67        ) extends BaseCirrusSearchResultSet {
68            private TitleHelper $titleHelper;
69            private SemanticSearchResultBuilder $resultBuilder;
70            private ElasticaResultSet $results;
71
72            /**
73             * @param TitleHelper $titleHelper
74             * @param ElasticaResultSet $results
75             * @param string[] $extraFieldsToExtract
76             */
77            public function __construct(
78                TitleHelper $titleHelper,
79                ElasticaResultSet $results,
80                array $extraFieldsToExtract,
81                array $queryBuilderProfile,
82            ) {
83                $this->titleHelper = $titleHelper;
84                $this->resultBuilder = new SemanticSearchResultBuilder(
85                    $this->titleHelper,
86                    // This is a bit awkward, we didn't have a nice way to pass between the qb
87                    // and the results type. For now it's this hack.
88                    $queryBuilderProfile['settings']['nested_field'],
89                    $queryBuilderProfile['settings']['snippet_field'],
90                    $queryBuilderProfile['settings']['anchor_field'],
91                    $extraFieldsToExtract
92                );
93                $this->results = $results;
94            }
95
96            /**
97             * @inheritDoc
98             */
99            protected function transformOneResult( \Elastica\Result $result ) {
100                return $this->resultBuilder->build( $result );
101            }
102
103            /**
104             * @return \Elastica\ResultSet|null
105             */
106            public function getElasticaResultSet() {
107                return $this->results;
108            }
109
110            /**
111             * @inheritDoc
112             */
113            public function searchContainedSyntax() {
114                return false;
115            }
116
117            protected function getTitleHelper(): TitleHelper {
118                return $this->titleHelper;
119            }
120        };
121    }
122
123    /**
124     * @return CirrusSearchResultSet
125     */
126    public function createEmptyResult() {
127        return BaseCirrusSearchResultSet::emptyResultSet();
128    }
129}