| 1 |
<?php |
| 2 |
/** |
| 3 |
* EP version 3.5.0 search algorithm |
| 4 |
* |
| 5 |
* @since 4.3.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\SearchAlgorithm; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
// @codeCoverageIgnoreStart |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
// @codeCoverageIgnoreEnd |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* EP version 3.5.0 search algorithm class. |
| 19 |
*/ |
| 20 |
class Version_350 extends \ElasticPress\SearchAlgorithm { |
| 21 |
/** |
| 22 |
* Search algorithm slug. |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public function get_slug() : string { |
| 27 |
return '3.5'; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Search algorithm name. |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public function get_name() : string { |
| 36 |
return esc_html__( 'Version 3.5', 'elasticpress' ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Search algorithm description. |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function get_description() : string { |
| 45 |
return esc_html__( 'Search for the existence of all words in the search first, then return results based on how closely those words appear.', 'elasticpress' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Return the Elasticsearch `query` clause. |
| 50 |
* |
| 51 |
* @param string $indexable_slug Indexable slug |
| 52 |
* @param string $search_term Search term(s) |
| 53 |
* @param array $search_fields Search fields |
| 54 |
* @param array $query_vars Query vars |
| 55 |
* @return array ES `query` |
| 56 |
*/ |
| 57 |
protected function get_raw_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ) : array { |
| 58 |
$query = [ |
| 59 |
'bool' => [ |
| 60 |
'should' => [ |
| 61 |
[ |
| 62 |
'multi_match' => [ |
| 63 |
'query' => $search_term, |
| 64 |
'type' => 'phrase', |
| 65 |
'fields' => $search_fields, |
| 66 |
/** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ |
| 67 |
'boost' => apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 3, $search_fields, $query_vars ), |
| 68 |
], |
| 69 |
], |
| 70 |
[ |
| 71 |
'multi_match' => [ |
| 72 |
'query' => $search_term, |
| 73 |
'fields' => $search_fields, |
| 74 |
'type' => 'phrase', |
| 75 |
'slop' => 5, |
| 76 |
], |
| 77 |
], |
| 78 |
], |
| 79 |
], |
| 80 |
]; |
| 81 |
|
| 82 |
$query = $this->apply_legacy_filters( $query, $indexable_slug, $search_fields, $query_vars ); |
| 83 |
|
| 84 |
return $query; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Apply legacy filters. |
| 89 |
* |
| 90 |
* @param array $query ES `query` |
| 91 |
* @param string $indexable_slug Indexable slug |
| 92 |
* @param array $search_fields Search term(s) |
| 93 |
* @param array $query_vars Query vars |
| 94 |
* @return array ES `query` |
| 95 |
*/ |
| 96 |
protected function apply_legacy_filters( array $query, string $indexable_slug, array $search_fields, array $query_vars ) : array { |
| 97 |
if ( 'post' !== $indexable_slug ) { |
| 98 |
return $query; |
| 99 |
} |
| 100 |
|
| 101 |
/** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ |
| 102 |
$query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( |
| 103 |
'ep_match_phrase_boost', |
| 104 |
[ $query['bool']['should'][0]['multi_match']['boost'], $search_fields, $query_vars ], |
| 105 |
'4.3.0', |
| 106 |
'ep_post_match_phrase_boost' |
| 107 |
); |
| 108 |
|
| 109 |
return $query; |
| 110 |
} |
| 111 |
} |
| 112 |
|