Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 21
NearMatchQueryBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 21
 build
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 21
<?php
namespace CirrusSearch\Query;
use CirrusSearch\Search\SearchContext;
use Elastica\Query\MultiMatch;
/**
 * Build a query suited for exact title/redirect match.
 */
class NearMatchQueryBuilder {
    use QueryBuilderTraits;
    /**
     * @param SearchContext $searchContext
     * @param string $term the original search term
     */
    public function build( SearchContext $searchContext, $term ) {
        if ( !$this->checkTitleSearchRequestLength( $term, $searchContext ) ) {
            return;
        }
        $searchContext->setOriginalSearchTerm( $term );
        // Elasticsearch seems to have trouble extracting the proper terms to highlight
        // from the default query we make so we feed it exactly the right query to highlight.
        $highlightQuery = new MultiMatch();
        $highlightQuery->setQuery( $term );
        $highlightQuery->setFields( [
            'title.near_match', 'redirect.title.near_match',
            'title.near_match_asciifolding', 'redirect.title.near_match_asciifolding',
        ] );
        if ( $searchContext->getConfig()->getElement( 'CirrusSearchAllFields', 'use' ) ) {
            // Instead of using the highlight query we need to make one like it that uses the all_near_match field.
            $allQuery = new MultiMatch();
            $allQuery->setQuery( $term );
            $allQuery->setFields( [ 'all_near_match', 'all_near_match.asciifolding' ] );
            $searchContext->addFilter( $allQuery );
        } else {
            $searchContext->addFilter( $highlightQuery );
        }
        $searchContext->setHighlightQuery( $highlightQuery );
        $searchContext->addSyntaxUsed( 'near_match' );
    }
}