Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ExplainPrinter | |
0.00% |
0 / 38 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
format | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
30 | |||
formatText | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
processExplain | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace CirrusSearch; |
4 | |
5 | use LuceneExplain\ExplainFactory; |
6 | |
7 | /** |
8 | * Formats the result of elasticsearch explain to a (slightly) more |
9 | * readable html format than raw json. |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License as published by |
13 | * the Free Software Foundation; either version 2 of the License, or |
14 | * (at your option) any later version. |
15 | * |
16 | * This program is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU General Public License for more details. |
20 | * |
21 | * You should have received a copy of the GNU General Public License along |
22 | * with this program; if not, write to the Free Software Foundation, Inc., |
23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 | * http://www.gnu.org/copyleft/gpl.html |
25 | */ |
26 | class ExplainPrinter { |
27 | /** @var string */ |
28 | private $type; |
29 | /** @var ExplainFactory */ |
30 | private $explainFactory; |
31 | |
32 | /** |
33 | * @param string $type Type of explain to print |
34 | */ |
35 | public function __construct( $type ) { |
36 | $this->type = $type; |
37 | $this->explainFactory = new ExplainFactory; |
38 | } |
39 | |
40 | /** |
41 | * @param array[] $queryResult Elasticsearch result |
42 | * @return string |
43 | */ |
44 | public function format( array $queryResult ) { |
45 | $result = []; |
46 | if ( isset( $queryResult['result']['hits']['hits'] ) ) { |
47 | $queryResult = [ $queryResult ]; |
48 | } |
49 | foreach ( $queryResult as $qr ) { |
50 | $result[] = "<div><h2>{$qr['description']} on {$qr['path']}</h2></div>"; |
51 | foreach ( $qr['result']['hits']['hits'] as $hit ) { |
52 | $explain = $this->processExplain( $hit['_explanation'] ); |
53 | $result[] = |
54 | "<div>" . |
55 | "<h3>" . htmlentities( $hit['_source']['title'] ) . "</h3>" . |
56 | ( isset( $hit['highlight']['text'][0] ) ? "<div>" . $hit['highlight']['text'][0] . "</div>" : "" ) . |
57 | "<table>" . |
58 | "<tr>" . |
59 | "<td>article id</td>" . |
60 | "<td>" . htmlentities( $hit['_id'] ) . "</td>" . |
61 | "</tr><tr>" . |
62 | "<td>ES score</td>" . |
63 | "<td>" . htmlentities( $hit['_score'] ) . "</td>" . |
64 | "</tr><tr>" . |
65 | "<td>ES explain</td>" . |
66 | "<td><pre>" . htmlentities( $explain ) . "</pre></td>" . |
67 | "</tr>" . |
68 | "</table>" . |
69 | "</div>"; |
70 | } |
71 | } |
72 | |
73 | return "<div>" . implode( '', $result ) . "</div>"; |
74 | } |
75 | |
76 | private function formatText( array $explanation, $indent = "" ) { |
77 | $line = $indent . $explanation['value'] . ' | ' . $explanation['description'] . "\n"; |
78 | if ( isset( $explanation['details'] ) ) { |
79 | foreach ( $explanation['details'] as $subExplanation ) { |
80 | $line .= $this->formatText( $subExplanation, "$indent " ); |
81 | } |
82 | } |
83 | |
84 | return $line; |
85 | } |
86 | |
87 | /** |
88 | * Only visible for test purposes |
89 | * |
90 | * @param array $explanation |
91 | * @return string |
92 | */ |
93 | protected function processExplain( array $explanation ) { |
94 | if ( $this->type === 'verbose' ) { |
95 | return $this->formatText( $explanation ); |
96 | } |
97 | $explain = $this->explainFactory->createExplain( $explanation ); |
98 | if ( $this->type === 'hot' ) { |
99 | return (string)$explain->vectorize(); |
100 | } else { |
101 | return (string)$explain; |
102 | } |
103 | } |
104 | |
105 | } |