Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 60 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
EntityResultType | |
0.00% |
0 / 60 |
|
0.00% |
0 / 6 |
72 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getSourceFiltering | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
getFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHighlightingConfiguration | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
6 | |||
transformElasticsearchResult | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createEmptyResult | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | namespace Wikibase\Search\Elastic; |
3 | |
4 | use CirrusSearch\Search\BaseCirrusSearchResultSet; |
5 | use CirrusSearch\Search\BaseResultsType; |
6 | use CirrusSearch\Searcher; |
7 | use Wikibase\Lib\TermLanguageFallbackChain; |
8 | |
9 | /** |
10 | * Result class for fulltext search of entities. |
11 | */ |
12 | class EntityResultType extends BaseResultsType { |
13 | |
14 | /** |
15 | * Display fallback chain. |
16 | * @var TermLanguageFallbackChain |
17 | */ |
18 | private $termFallbackChain; |
19 | /** |
20 | * Display language code |
21 | * @var string |
22 | */ |
23 | private $displayLanguage; |
24 | |
25 | /** |
26 | * @param string $displayLanguage Display Language code |
27 | * @param TermLanguageFallbackChain $displayFallbackChain Fallback chain for display |
28 | */ |
29 | public function __construct( $displayLanguage, TermLanguageFallbackChain $displayFallbackChain ) { |
30 | $this->termFallbackChain = $displayFallbackChain; |
31 | $this->displayLanguage = $displayLanguage; |
32 | } |
33 | |
34 | /** |
35 | * Get the source filtering to be used loading the result. |
36 | * |
37 | * @return false|string|array corresponding to Elasticsearch source filtering syntax |
38 | */ |
39 | public function getSourceFiltering() { |
40 | $fields = parent::getSourceFiltering(); |
41 | $fields[] = 'timestamp'; |
42 | $fields[] = 'sitelink_count'; |
43 | $fields[] = 'statement_count'; |
44 | foreach ( $this->termFallbackChain->getFetchLanguageCodes() as $code ) { |
45 | $fields[] = "labels.$code"; |
46 | $fields[] = "descriptions.$code"; |
47 | } |
48 | return $fields; |
49 | } |
50 | |
51 | /** |
52 | * Get the fields to load. Most of the time we'll use source filtering instead but |
53 | * some fields aren't part of the source. |
54 | * |
55 | * @return array corresponding to Elasticsearch fields syntax |
56 | */ |
57 | public function getFields() { |
58 | return []; |
59 | } |
60 | |
61 | /** |
62 | * Get the highlighting configuration. |
63 | * |
64 | * @param array $highlightSource configuration for how to highlight the source. |
65 | * Empty if source should be ignored. |
66 | * @return array|null highlighting configuration for elasticsearch |
67 | */ |
68 | public function getHighlightingConfiguration( array $highlightSource ) { |
69 | $config = [ |
70 | 'pre_tags' => [ Searcher::HIGHLIGHT_PRE_MARKER ], |
71 | 'post_tags' => [ Searcher::HIGHLIGHT_POST_MARKER ], |
72 | 'fields' => [], |
73 | ]; |
74 | |
75 | $config['fields']['title'] = [ |
76 | 'type' => 'experimental', |
77 | 'fragmenter' => "none", |
78 | 'number_of_fragments' => 0, |
79 | 'matched_fields' => [ 'title.keyword' ] |
80 | ]; |
81 | |
82 | foreach ( $this->termFallbackChain->getFetchLanguageCodes() as $code ) { |
83 | $config['fields']["labels.{$code}.plain"] = [ |
84 | 'type' => 'experimental', |
85 | 'fragmenter' => "none", |
86 | 'number_of_fragments' => 0, |
87 | 'options' => [ |
88 | 'skip_if_last_matched' => true, |
89 | 'return_snippets_and_offsets' => true |
90 | ], |
91 | ]; |
92 | $config['fields']["descriptions.{$code}.plain"] = [ |
93 | 'type' => 'experimental', |
94 | 'fragmenter' => "none", |
95 | 'number_of_fragments' => 0, |
96 | 'options' => [ |
97 | 'skip_if_last_matched' => true, |
98 | ], |
99 | ]; |
100 | } |
101 | |
102 | $config['fields']["labels.*.plain"] = [ |
103 | 'type' => 'experimental', |
104 | 'fragmenter' => "none", |
105 | 'number_of_fragments' => 0, |
106 | 'options' => [ |
107 | 'skip_if_last_matched' => true, |
108 | 'return_snippets_and_offsets' => true |
109 | ], |
110 | ]; |
111 | $config['fields']["descriptions.*.plain"] = [ |
112 | 'type' => 'experimental', |
113 | 'fragmenter' => "none", |
114 | 'number_of_fragments' => 0, |
115 | 'options' => [ |
116 | 'skip_if_last_matched' => true, |
117 | ], |
118 | ]; |
119 | |
120 | return $config; |
121 | } |
122 | |
123 | /** |
124 | * @param \Elastica\ResultSet $result |
125 | * @return mixed Set of search results, the types of which vary by implementation. |
126 | */ |
127 | public function transformElasticsearchResult( \Elastica\ResultSet $result ) { |
128 | return new EntityResultSet( $this->displayLanguage, $this->termFallbackChain, $result ); |
129 | } |
130 | |
131 | /** |
132 | * @return mixed Empty set of search results |
133 | */ |
134 | public function createEmptyResult() { |
135 | return BaseCirrusSearchResultSet::emptyResultSet( false ); |
136 | } |
137 | |
138 | } |