-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathSearchAlgorithm.php
More file actions
111 lines (101 loc) · 2.97 KB
/
Copy pathSearchAlgorithm.php
File metadata and controls
111 lines (101 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* SearchAlgorithm class.
*
* All search algorithms extend this class.
*
* @since 4.3.0
* @package elasticpress
*/
namespace ElasticPress;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* SearchAlgorithm abstract class
*/
abstract class SearchAlgorithm {
/**
* Return the Search Algorithm slug.
*
* @return string
*/
abstract public function get_slug(): string;
/**
* Return the Search Algorithm human readable name.
*
* @return string
*/
abstract public function get_name(): string;
/**
* Return the Search Algorithm description.
*
* @return string
*/
abstract public function get_description(): string;
/**
* Return the Elasticsearch `query` clause.
*
* @param string $indexable_slug Indexable slug
* @param string $search_term Search term(s)
* @param array $search_fields Search fields
* @param array $query_vars Query vars
* @return array ES `query`
*/
abstract protected function get_raw_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ): array;
/**
* Wrapper for the `get_raw_query`, making sure the `ep_{$indexable_slug}_formatted_args_query` filter is applied.
*
* @param string $indexable_slug Indexable slug
* @param string $search_term Search term(s)
* @param array $search_fields Search fields
* @param array $query_vars Query vars
* @return array ES `query`
*/
public function get_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ): array {
$query = $this->get_raw_query( $indexable_slug, $search_term, $search_fields, $query_vars );
/**
* Filter formatted Elasticsearch query (only contains query part)
*
* @hook ep_{$indexable_slug}_formatted_args_query
* @param {array} $query Current query
* @param {array} $query_vars Query variables
* @param {string} $search_text Search text
* @param {array} $search_fields Search fields
* @return {array} New query
*
* @since 4.3.0
*/
$query = apply_filters(
"ep_{$indexable_slug}_formatted_args_query",
$query,
$query_vars,
$search_term,
$search_fields
);
// Backwards-compatibility.
if ( 'post' === $indexable_slug ) {
/**
* Filter formatted Elasticsearch query for posts.
*
* This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_formatted_args_query`.
*
* @hook ep_formatted_args_query
* @param {array} $query Current query
* @param {array} $query_vars Query variables
* @param {string} $search_text Search text
* @param {array} $search_fields Search fields
* @return {array} New query
*
* @since 3.5.5 $search_text and $search_fields parameters added.
*/
$query = apply_filters_deprecated(
'ep_formatted_args_query',
[ $query, $query_vars, $search_term, $search_fields ],
'4.3.0',
'ep_post_formatted_args_query'
);
}
return $query;
}
}