| 1 |
<?php |
| 2 |
/** |
| 3 |
* EP version 4.0.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 4.0.0 search algorithm class. |
| 19 |
*/ |
| 20 |
class Version_400 extends \ElasticPress\SearchAlgorithm { |
| 21 |
/** |
| 22 |
* Search algorithm slug. |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public function get_slug() : string { |
| 27 |
return '4.0'; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Search algorithm name. |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public function get_name() : string { |
| 36 |
return esc_html__( 'Version 4.0', '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 all search terms in one field first, then prioritize them over search terms matched in different fields.', '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 |
'operator' => 'and', |
| 75 |
/** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ |
| 76 |
'boost' => apply_filters( "ep_{$indexable_slug}_match_boost", 1, $search_fields, $query_vars ), |
| 77 |
/** |
| 78 |
* Filter fuzziness for match query |
| 79 |
* |
| 80 |
* @hook ep_{$indexable_slug}_match_fuzziness |
| 81 |
* @since 4.3.0 |
| 82 |
* @param {string|int} $fuzziness Fuzziness |
| 83 |
* @param {array} $search_fields Search fields |
| 84 |
* @param {array} $query_vars Query variables |
| 85 |
* @return {string} New fuzziness |
| 86 |
*/ |
| 87 |
'fuzziness' => apply_filters( "ep_{$indexable_slug}_match_fuzziness", 'auto', $search_fields, $query_vars ), |
| 88 |
], |
| 89 |
], |
| 90 |
[ |
| 91 |
'multi_match' => [ |
| 92 |
'query' => $search_term, |
| 93 |
'type' => 'cross_fields', |
| 94 |
'fields' => $search_fields, |
| 95 |
/** |
| 96 |
* Filter boost for match cross_fields query |
| 97 |
* |
| 98 |
* @hook ep_{$indexable_slug}_match_cross_fields_boost |
| 99 |
* @since 4.3.0 |
| 100 |
* @param {int} $boost Boost |
| 101 |
* @param {array} $search_fields Search fields |
| 102 |
* @param {array} $query_vars Query variables |
| 103 |
* @return {int} New boost |
| 104 |
*/ |
| 105 |
'boost' => apply_filters( "ep_{$indexable_slug}_match_cross_fields_boost", 1, $search_fields, $query_vars ), |
| 106 |
'analyzer' => 'standard', |
| 107 |
'tie_breaker' => 0.5, |
| 108 |
'operator' => 'and', |
| 109 |
], |
| 110 |
], |
| 111 |
], |
| 112 |
], |
| 113 |
]; |
| 114 |
|
| 115 |
$query = $this->apply_legacy_filters( $query, $indexable_slug, $search_fields, $query_vars ); |
| 116 |
|
| 117 |
return $query; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Apply legacy filters. |
| 122 |
* |
| 123 |
* @param array $query ES `query` |
| 124 |
* @param string $indexable_slug Indexable slug |
| 125 |
* @param array $search_fields Search term(s) |
| 126 |
* @param array $query_vars Query vars |
| 127 |
* @return array ES `query` |
| 128 |
*/ |
| 129 |
protected function apply_legacy_filters( array $query, string $indexable_slug, array $search_fields, array $query_vars ) : array { |
| 130 |
if ( 'post' !== $indexable_slug ) { |
| 131 |
return $query; |
| 132 |
} |
| 133 |
|
| 134 |
/** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ |
| 135 |
$query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( |
| 136 |
'ep_match_phrase_boost', |
| 137 |
[ $query['bool']['should'][0]['multi_match']['boost'], $search_fields, $query_vars ], |
| 138 |
'4.3.0', |
| 139 |
'ep_post_match_phrase_boost' |
| 140 |
); |
| 141 |
|
| 142 |
/** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ |
| 143 |
$query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated( |
| 144 |
'ep_match_boost', |
| 145 |
[ $query['bool']['should'][1]['multi_match']['boost'], $search_fields, $query_vars ], |
| 146 |
'4.3.0', |
| 147 |
'ep_post_match_boost' |
| 148 |
); |
| 149 |
|
| 150 |
/** |
| 151 |
* Filter fuzziness for post match query |
| 152 |
* |
| 153 |
* This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_fuzziness`. |
| 154 |
* |
| 155 |
* @hook ep_match_fuzziness |
| 156 |
* @since 4.0.0 |
| 157 |
* @param {string|int} $fuzziness Fuzziness |
| 158 |
* @param {array} $search_fields Search fields |
| 159 |
* @param {array} $query_vars Query variables |
| 160 |
* @return {string} New fuzziness |
| 161 |
*/ |
| 162 |
$query['bool']['should'][1]['multi_match']['fuzziness'] = apply_filters_deprecated( |
| 163 |
'ep_match_fuzziness', |
| 164 |
[ $query['bool']['should'][1]['multi_match']['fuzziness'], $search_fields, $query_vars ], |
| 165 |
'4.3.0', |
| 166 |
'ep_post_match_fuzziness' |
| 167 |
); |
| 168 |
|
| 169 |
/** |
| 170 |
* Filter boost for post match cross_fields query |
| 171 |
* |
| 172 |
* This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_cross_fields_boost`. |
| 173 |
* |
| 174 |
* @hook ep_{$indexable_slug}_match_cross_fields_boost |
| 175 |
* @since 4.0.0 |
| 176 |
* @param {int} $boost Boost |
| 177 |
* @param {array} $search_fields Search fields |
| 178 |
* @param {array} $query_vars Query variables |
| 179 |
* @return {int} New boost |
| 180 |
*/ |
| 181 |
$query['bool']['should'][2]['multi_match']['boost'] = apply_filters_deprecated( |
| 182 |
'ep_match_cross_fields_boost', |
| 183 |
[ $query['bool']['should'][2]['multi_match']['boost'], $search_fields, $query_vars ], |
| 184 |
'4.3.0', |
| 185 |
'ep_post_match_cross_fields_boost' |
| 186 |
); |
| 187 |
|
| 188 |
return $query; |
| 189 |
} |
| 190 |
} |
| 191 |
|