Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CirrusNearTitleBoostFeature
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 getKeywords
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 doApply
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 expand
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBoostFunctionBuilder
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace GeoData\Search;
4
5use CirrusSearch\Parser\AST\KeywordFeatureNode;
6use CirrusSearch\Query\BoostFunctionFeature;
7use CirrusSearch\Query\Builder\QueryBuildingContext;
8use CirrusSearch\Query\SimpleKeywordFeature;
9use CirrusSearch\Search\Rescore\BoostFunctionBuilder;
10use CirrusSearch\Search\SearchContext;
11use CirrusSearch\SearchConfig;
12use CirrusSearch\WarningCollector;
13use GeoData\Coord;
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 */
25class CirrusNearTitleBoostFeature extends SimpleKeywordFeature implements BoostFunctionFeature {
26    use CirrusGeoFeature;
27
28    /** @inheritDoc */
29    protected function getKeywords() {
30        return [ 'boost-neartitle' ];
31    }
32
33    /** @inheritDoc */
34    protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) {
35        [ $coord, $radius ] = $this->parseGeoNearbyTitle( $context, $key, $value );
36        if ( $coord ) {
37            $context->addCustomRescoreComponent(
38                new GeoRadiusFunctionScoreBuilder( $context->getConfig(), $negated ? 0.1 : 1, $coord, $radius )
39            );
40        }
41
42        return [ null, false ];
43    }
44
45    /**
46     * @param KeywordFeatureNode $node
47     * @param SearchConfig $config
48     * @param WarningCollector $warningCollector
49     * @return array{?Coord,int,int|string}
50     */
51    public function expand(
52        KeywordFeatureNode $node,
53        SearchConfig $config,
54        WarningCollector $warningCollector
55    ) {
56        return $this->parseGeoNearbyTitle( $warningCollector, $node->getKey(), $node->getValue() );
57    }
58
59    /**
60     * @param KeywordFeatureNode $node
61     * @param QueryBuildingContext $context
62     * @return BoostFunctionBuilder|null
63     */
64    public function getBoostFunctionBuilder(
65        KeywordFeatureNode $node,
66        QueryBuildingContext $context
67    ) {
68        [ $coord, $radius ] = $context->getKeywordExpandedData( $node );
69        if ( $coord !== null ) {
70            return new GeoRadiusFunctionScoreBuilder( $context->getSearchConfig(), 1,
71                $coord, $radius );
72        }
73        return null;
74    }
75}