From b7745c95f7ade7eec59892a7225ff68fe589ba76 Mon Sep 17 00:00:00 2001 From: Scott Buckel Date: Wed, 22 Apr 2020 22:31:12 -0400 Subject: [PATCH 1/7] Added a custom search parameter to allow for Painless queries --- docs/indexables.md | 4 +++- includes/classes/Indexable/Post/Post.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/indexables.md b/docs/indexables.md index 8382d9d477..38a4944935 100644 --- a/docs/indexables.md +++ b/docs/indexables.md @@ -93,7 +93,9 @@ ElasticPress integrates with `WP_Query` if the `ep_integrate` parameter is passe 'day' => 1, ) ); ``` - +* ```custom_query``` (*string*) + ```custom_query``` allows the user to write a custom query in the painless scripting language (https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html)[https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html]. For example `doc['meta.birthday'].value.getMonthValue() == 3` + * ```date_query``` (*array*) ```date_query``` accepts an array of keys and values (array|string|int) to find posts created on diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index d23c81e11c..e474a75180 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1227,6 +1227,25 @@ function( $tax_query ) use ( $args ) { $use_filters = true; } + if ( ! empty( $args['custom_query'] ) ) { + foreach($args['custom_query'] as $customQuery){ + $filter['bool']['must'][] = array( + "bool" => array( + "filter" => array( + 'script' => array( + 'script' => array( + "source" => $customQuery, + "lang" => "painless" + ) + ), + ), + ), + ); + } + } + + + /** * Like WP_Query in search context, if no post_status is specified we default to "any". To * be safe you should ALWAYS specify the post_status parameter UNLIKE with WP_Query. From 707afcdb73e7c3ffb7bca21d282ac2341cbf76b2 Mon Sep 17 00:00:00 2001 From: Scott Buckel Date: Wed, 22 Apr 2020 22:32:32 -0400 Subject: [PATCH 2/7] Fixing md format --- docs/indexables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/indexables.md b/docs/indexables.md index 38a4944935..9c8b0e3b24 100644 --- a/docs/indexables.md +++ b/docs/indexables.md @@ -94,7 +94,7 @@ ElasticPress integrates with `WP_Query` if the `ep_integrate` parameter is passe ) ); ``` * ```custom_query``` (*string*) - ```custom_query``` allows the user to write a custom query in the painless scripting language (https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html)[https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html]. For example `doc['meta.birthday'].value.getMonthValue() == 3` + ```custom_query``` allows the user to write a custom query in the painless scripting language [https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html](https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html). For example `doc['meta.birthday'].value.getMonthValue() == 3` * ```date_query``` (*array*) From d7f8df0e0c7573ce418926b3e5ccd80f7c502e43 Mon Sep 17 00:00:00 2001 From: Scott Buckel Date: Wed, 22 Apr 2020 22:33:52 -0400 Subject: [PATCH 3/7] fixing markdown format --- docs/indexables.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/indexables.md b/docs/indexables.md index 9c8b0e3b24..3b15166363 100644 --- a/docs/indexables.md +++ b/docs/indexables.md @@ -94,7 +94,10 @@ ElasticPress integrates with `WP_Query` if the `ep_integrate` parameter is passe ) ); ``` * ```custom_query``` (*string*) - ```custom_query``` allows the user to write a custom query in the painless scripting language [https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html](https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html). For example `doc['meta.birthday'].value.getMonthValue() == 3` + ```custom_query``` allows the user to write a custom query in the painless scripting language [https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html](https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html). + ```php + $args['custom_query'][] = "doc['meta.birthday'].value.getMonthValue() == 3";` + ``` * ```date_query``` (*array*) From ff8a61e138d186073d06a42cc585aefbe23088a8 Mon Sep 17 00:00:00 2001 From: Scott Buckel Date: Wed, 22 Apr 2020 22:34:09 -0400 Subject: [PATCH 4/7] fixing markdown format --- docs/indexables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/indexables.md b/docs/indexables.md index 3b15166363..21c3200041 100644 --- a/docs/indexables.md +++ b/docs/indexables.md @@ -96,7 +96,7 @@ ElasticPress integrates with `WP_Query` if the `ep_integrate` parameter is passe * ```custom_query``` (*string*) ```custom_query``` allows the user to write a custom query in the painless scripting language [https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html](https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html). ```php - $args['custom_query'][] = "doc['meta.birthday'].value.getMonthValue() == 3";` + $args['custom_query'][] = "doc['meta.birthday'].value.getMonthValue() == 3"; ``` * ```date_query``` (*array*) From 3cfda3978a492e4de074568c9f540e2213eb89d9 Mon Sep 17 00:00:00 2001 From: Scott Buckel Date: Wed, 22 Apr 2020 22:34:46 -0400 Subject: [PATCH 5/7] fixing markdown format --- docs/indexables.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/indexables.md b/docs/indexables.md index 21c3200041..35a5afddaa 100644 --- a/docs/indexables.md +++ b/docs/indexables.md @@ -94,6 +94,7 @@ ElasticPress integrates with `WP_Query` if the `ep_integrate` parameter is passe ) ); ``` * ```custom_query``` (*string*) + ```custom_query``` allows the user to write a custom query in the painless scripting language [https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html](https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html). ```php $args['custom_query'][] = "doc['meta.birthday'].value.getMonthValue() == 3"; From 7a74b08452054fc9dd4498d15f8ed2017d808406 Mon Sep 17 00:00:00 2001 From: Scott Buckel Date: Tue, 28 Apr 2020 12:19:07 -0400 Subject: [PATCH 6/7] Fixing code lint issues --- includes/classes/Indexable/Post/Post.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index e474a75180..a5b7fbae79 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1228,14 +1228,14 @@ function( $tax_query ) use ( $args ) { } if ( ! empty( $args['custom_query'] ) ) { - foreach($args['custom_query'] as $customQuery){ + foreach ( $args['custom_query'] as $custom_query ) { $filter['bool']['must'][] = array( - "bool" => array( - "filter" => array( + 'bool' => array( + 'filter' => array( 'script' => array( 'script' => array( - "source" => $customQuery, - "lang" => "painless" + 'source' => $custom_query, + 'lang' => 'painless', ) ), ), @@ -1244,8 +1244,6 @@ function( $tax_query ) use ( $args ) { } } - - /** * Like WP_Query in search context, if no post_status is specified we default to "any". To * be safe you should ALWAYS specify the post_status parameter UNLIKE with WP_Query. From c34d4a43b1b32cb19fe3cdae49413f2f86cc0ac3 Mon Sep 17 00:00:00 2001 From: Scott Buckel Date: Tue, 28 Apr 2020 12:25:42 -0400 Subject: [PATCH 7/7] Finishing up PHPCS errors --- includes/classes/Indexable/Post/Post.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index a5b7fbae79..a9192057b4 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1233,10 +1233,10 @@ function( $tax_query ) use ( $args ) { 'bool' => array( 'filter' => array( 'script' => array( - 'script' => array( + 'script' => array( 'source' => $custom_query, - 'lang' => 'painless', - ) + 'lang' => 'painless', + ), ), ), ),