Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
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 / 14
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 build
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
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        if ( $searchContext->getConfig()->getElement( 'CirrusSearchAllFields', 'use' ) ) {
33            // Instead of using the highlight query we need to make one like it that uses the all_near_match field.
34            $allQuery = new MultiMatch();
35            $allQuery->setQuery( $term );
36            $allQuery->setFields( [ 'all_near_match', 'all_near_match.asciifolding' ] );
37            $searchContext->addFilter( $allQuery );
38        } else {
39            $searchContext->addFilter( $highlightQuery );
40        }
41        $searchContext->setHighlightQuery( $highlightQuery );
42        $searchContext->addSyntaxUsed( 'near_match' );
43    }
44}