Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
CirrusNearTitleBoostFeature | |
100.00% |
14 / 14 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getKeywords | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
doApply | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
expand | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getBoostFunctionBuilder | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace GeoData\Search; |
4 | |
5 | use CirrusSearch\Parser\AST\KeywordFeatureNode; |
6 | use CirrusSearch\Query\BoostFunctionFeature; |
7 | use CirrusSearch\Query\Builder\QueryBuildingContext; |
8 | use CirrusSearch\Query\SimpleKeywordFeature; |
9 | use CirrusSearch\Search\Rescore\BoostFunctionBuilder; |
10 | use CirrusSearch\Search\SearchContext; |
11 | use CirrusSearch\SearchConfig; |
12 | use CirrusSearch\WarningCollector; |
13 | use MediaWiki\Config\Config; |
14 | |
15 | /** |
16 | * Applies geo boosting to the query by providing a Title. |
17 | * |
18 | * it increases the score of results within the geographic area. All values can be prefixed |
19 | * with a radius in m or km to apply. If not specified this defaults to 5km. |
20 | * |
21 | * Examples: |
22 | * boost-neartitle:"San Francisco" |
23 | * boost-neartitle:50km,Kampala |
24 | */ |
25 | class CirrusNearTitleBoostFeature extends SimpleKeywordFeature implements BoostFunctionFeature { |
26 | use CirrusGeoFeature; |
27 | |
28 | /** |
29 | * @var Config |
30 | */ |
31 | private $config; |
32 | |
33 | /** |
34 | * @param Config $config |
35 | */ |
36 | public function __construct( Config $config ) { |
37 | $this->config = $config; |
38 | } |
39 | |
40 | /** @inheritDoc */ |
41 | protected function getKeywords() { |
42 | return [ 'boost-neartitle' ]; |
43 | } |
44 | |
45 | /** |
46 | * @param SearchContext $context |
47 | * @param string $key The keyword |
48 | * @param string $value The value attached to the keyword with quotes stripped |
49 | * @param string $quotedValue The original value in the search string, including quotes if used |
50 | * @param bool $negated Is the search negated? Not used to generate the returned AbstractQuery, |
51 | * that will be negated as necessary. Used for any other building/context necessary. |
52 | * @return array Two element array, first an AbstractQuery or null to apply to the |
53 | * query. Second a boolean indicating if the quotedValue should be kept in the search |
54 | * string. |
55 | */ |
56 | protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) { |
57 | [ $coord, $radius ] = $this->parseGeoNearbyTitle( $context, $key, $value ); |
58 | if ( $coord ) { |
59 | $context->addCustomRescoreComponent( |
60 | new GeoRadiusFunctionScoreBuilder( $context->getConfig(), $negated ? 0.1 : 1, $coord, $radius ) |
61 | ); |
62 | } |
63 | |
64 | return [ null, false ]; |
65 | } |
66 | |
67 | /** |
68 | * @param KeywordFeatureNode $node |
69 | * @param SearchConfig $config |
70 | * @param WarningCollector $warningCollector |
71 | * @return array |
72 | */ |
73 | public function expand( |
74 | KeywordFeatureNode $node, |
75 | SearchConfig $config, |
76 | WarningCollector $warningCollector |
77 | ) { |
78 | return $this->parseGeoNearbyTitle( $warningCollector, $node->getKey(), $node->getValue() ); |
79 | } |
80 | |
81 | /** |
82 | * @param KeywordFeatureNode $node |
83 | * @param QueryBuildingContext $context |
84 | * @return BoostFunctionBuilder|null |
85 | */ |
86 | public function getBoostFunctionBuilder( |
87 | KeywordFeatureNode $node, |
88 | QueryBuildingContext $context |
89 | ) { |
90 | [ $coord, $radius ] = $context->getKeywordExpandedData( $node ); |
91 | if ( $coord !== null ) { |
92 | return new GeoRadiusFunctionScoreBuilder( $context->getSearchConfig(), 1, |
93 | $coord, $radius ); |
94 | } |
95 | return null; |
96 | } |
97 | } |