| 1 |
<?php |
| 2 |
/** |
| 3 |
* Default 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 |
* Default search algorithm class. |
| 19 |
*/ |
| 20 |
class DefaultAlgorithm extends \ElasticPress\SearchAlgorithm { |
| 21 |
/** |
| 22 |
* Search algorithm slug. |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public function get_slug(): string { |
| 27 |
return 'default'; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Search algorithm name. |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public function get_name(): string { |
| 36 |
return esc_html__( 'Default', 'elasticpress' ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Search algorithm description. |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function get_description(): string { |
| 45 |
return esc_html__( 'Use a fuzzy match approach which includes results that have misspellings, and also includes matches on only some of the words in the search.', '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 |
/** |
| 67 |
* Filter match phrase boost amount |
| 68 |
* |
| 69 |
* @since 4.3.0 |
| 70 |
* @hook ep_{$indexable_slug}_match_phrase_boost |
| 71 |
* @param {int} $boost Phrase boost |
| 72 |
* @param {array} $search_fields Search fields |
| 73 |
* @param {array} $query_vars Query variables |
| 74 |
* @return {int} New boost amount |
| 75 |
*/ |
| 76 |
'boost' => apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 4, $search_fields, $query_vars ), |
| 77 |
], |
| 78 |
], |
| 79 |
[ |
| 80 |
'multi_match' => [ |
| 81 |
'query' => $search_term, |
| 82 |
'fields' => $search_fields, |
| 83 |
/** |
| 84 |
* Filter match boost amount |
| 85 |
* |
| 86 |
* @since 4.3.0 |
| 87 |
* @hook ep_{$indexable_slug}_match_boost |
| 88 |
* @param {int} $boost Boost |
| 89 |
* @param {array} $search_fields Search fields |
| 90 |
* @param {array} $query_vars Query variables |
| 91 |
* @return {int} New boost |
| 92 |
*/ |
| 93 |
'boost' => apply_filters( "ep_{$indexable_slug}_match_boost", 2, $search_fields, $query_vars ), |
| 94 |
'fuzziness' => 0, |
| 95 |
'operator' => 'and', |
| 96 |
], |
| 97 |
], |
| 98 |
[ |
| 99 |
'multi_match' => [ |
| 100 |
'fields' => $search_fields, |
| 101 |
'query' => $search_term, |
| 102 |
/** |
| 103 |
* Filter fuzziness amount |
| 104 |
* |
| 105 |
* @since 4.3.0 |
| 106 |
* @hook ep_{$indexable_slug}_fuzziness_arg |
| 107 |
* @param {int} $fuzziness Fuzziness |
| 108 |
* @param {array} $search_fields Search fields |
| 109 |
* @param {array} $query_vars Query variables |
| 110 |
* @return {int} New fuzziness |
| 111 |
*/ |
| 112 |
'fuzziness' => apply_filters( "ep_{$indexable_slug}_fuzziness_arg", 1, $search_fields, $query_vars ), |
| 113 |
], |
| 114 |
], |
| 115 |
], |
| 116 |
], |
| 117 |
]; |
| 118 |
|
| 119 |
$query = $this->apply_legacy_filters( $query, $indexable_slug, $search_fields, $query_vars ); |
| 120 |
|
| 121 |
return $query; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Apply legacy filters. |
| 126 |
* |
| 127 |
* @param array $query ES `query` |
| 128 |
* @param string $indexable_slug Indexable slug |
| 129 |
* @param array $search_fields Search term(s) |
| 130 |
* @param array $query_vars Query vars |
| 131 |
* @return array ES `query` |
| 132 |
*/ |
| 133 |
protected function apply_legacy_filters( array $query, string $indexable_slug, array $search_fields, array $query_vars ): array { |
| 134 |
if ( 'post' !== $indexable_slug ) { |
| 135 |
return $query; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Filter boost for post match phrase query. |
| 140 |
* |
| 141 |
* This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_phrase_boost`. |
| 142 |
* |
| 143 |
* @hook ep_match_phrase_boost |
| 144 |
* @param {int} $boost Phrase boost |
| 145 |
* @param {array} $search_fields Search fields |
| 146 |
* @param {array} $query_vars Query variables |
| 147 |
* @return {int} New boost amount |
| 148 |
*/ |
| 149 |
$query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( |
| 150 |
'ep_match_phrase_boost', |
| 151 |
[ $query['bool']['should'][0]['multi_match']['boost'], $search_fields, $query_vars ], |
| 152 |
'4.3.0', |
| 153 |
'ep_post_match_phrase_boost' |
| 154 |
); |
| 155 |
|
| 156 |
/** |
| 157 |
* Filter boost for post match query |
| 158 |
* |
| 159 |
* This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_boost`. |
| 160 |
* |
| 161 |
* @hook ep_match_boost |
| 162 |
* @param {int} $boost Boost |
| 163 |
* @param {array} $search_fields Search fields |
| 164 |
* @param {array} $query_vars Query variables |
| 165 |
* @return {int} New boost |
| 166 |
*/ |
| 167 |
$query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated( |
| 168 |
'ep_match_boost', |
| 169 |
[ $query['bool']['should'][1]['multi_match']['boost'], $search_fields, $query_vars ], |
| 170 |
'4.3.0', |
| 171 |
'ep_post_match_boost' |
| 172 |
); |
| 173 |
|
| 174 |
/** |
| 175 |
* Filter fuzziness for post query |
| 176 |
* |
| 177 |
* This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_fuzziness_arg`. |
| 178 |
* |
| 179 |
* @hook ep_fuzziness_arg |
| 180 |
* @param {int} $fuzziness Fuzziness |
| 181 |
* @param {array} $search_fields Search fields |
| 182 |
* @param {array} $query_vars Query variables |
| 183 |
* @return {int} New fuzziness |
| 184 |
*/ |
| 185 |
$query['bool']['should'][2]['multi_match']['fuzziness'] = apply_filters_deprecated( |
| 186 |
'ep_fuzziness_arg', |
| 187 |
[ $query['bool']['should'][2]['multi_match']['fuzziness'], $search_fields, $query_vars ], |
| 188 |
'4.3.0', |
| 189 |
'ep_post_fuzziness_arg' |
| 190 |
); |
| 191 |
|
| 192 |
return $query; |
| 193 |
} |
| 194 |
} |
| 195 |
|