PluginProbe
ElasticPress / 5.3.5
ElasticPress v5.3.5
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / SearchAlgorithm.php

SearchAlgorithm.php in ElasticPress 5.3.5, at includes/classes/SearchAlgorithm.php

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