Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
NearMatchQueryBuilder
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 build
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace CirrusSearch\Query;
4
5use CirrusSearch\Search\SearchContext;
6use Elastica\Query\MultiMatch;
7
8/**
9 * Build a query suited for exact title/redirect match.
10 */
11class NearMatchQueryBuilder {
12    use QueryBuilderTraits;
13
14    /**
15     * @param SearchContext $searchContext
16     * @param string $term the original search term
17     */
18    public function build( SearchContext $searchContext, $term ) {
19        if ( !$this->checkTitleSearchRequestLength( $term, $searchContext ) ) {
20            return;
21        }
22
23        $searchContext->setOriginalSearchTerm( $term );
24        // Elasticsearch seems to have trouble extracting the proper terms to highlight
25        // from the default query we make so we feed it exactly the right query to highlight.
26        $highlightQuery = new MultiMatch();
27        $highlightQuery->setQuery( $term );
28        $highlightQuery->setFields( [
29            'title.near_match', 'redirect.title.near_match',
30            'title.near_match_asciifolding', 'redirect.title.near_match_asciifolding',
31        ] );
32        // Instead of using the highlight query we need to make one like it that uses the all_near_match field.
33        $allQuery = new MultiMatch();
34        $allQuery->setQuery( $term );
35        $allQuery->setFields( [ 'all_near_match', 'all_near_match.asciifolding' ] );
36        $searchContext->addFilter( $allQuery );
37        $searchContext->setHighlightQuery( $highlightQuery );
38        $searchContext->addSyntaxUsed( 'near_match' );
39    }
40}