From 2c566a4e400c11cd76240d66c65be3b5426edd0c Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Thu, 8 Nov 2018 11:01:56 +0100 Subject: [PATCH 1/3] bypass ep mr --- src/Core.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Core.php b/src/Core.php index efbce59..a2eb9ae 100644 --- a/src/Core.php +++ b/src/Core.php @@ -38,6 +38,11 @@ public function add_actions() add_filter('ep_formatted_args', [$this, 'ep_formatted_args'], 1, 1); add_filter('ep_index_post_request_args', [$this, 'ep_index_post_request_args'], 1, 2); add_filter('ep_index_post_request_path', [$this, 'ep_index_post_request_path'], 1, 2); + + //workaround: https://github.com/10up/ElasticPress/pull/1158 + add_filter('ep_post_sync_args', [$this, 'ep_post_sync_args'], 10, 2); + + /// ELASITC PRESS add_filter('acp/filtering/cache/seconds', function ($seconds) { @@ -97,6 +102,12 @@ public function add_actions() }); } + public function ep_post_sync_args($args, $post_id) + { + $args['comment_status'] = absint($args['comment_status']); + $args['ping_status'] = absint($args['ping_status']); + return $args; + } //ACF querie disable // public function debug_enabled() @@ -177,10 +188,10 @@ public function ep_formatted_args($args) $new['query_string'] = $new['multi_match']; $new['query_string']['query'] = $this->wildCardIt($new['query_string']['query']); $new['query_string']['query'] = str_replace( - ['\\', '+', '-', '&', '|', '!', '(', ')', '{', '}', '[', ']', '^', '~', '?', ':'], - ['\\\\', "\+", "\-", "\&", "\|", "\!", "\(", "\)", "\{", "\}", "\[", "\]", "\^", "\~", "\?", "\:"], - $new['query_string']['query'] - ); + ['\\', '+', '-', '&', '|', '!', '(', ')', '{', '}', '[', ']', '^', '~', '?', ':'], + ['\\\\', "\+", "\-", "\&", "\|", "\!", "\(", "\)", "\{", "\}", "\[", "\]", "\^", "\~", "\?", "\:"], + $new['query_string']['query'] + ); $new['query_string']['analyze_wildcard'] = true; unset($new['query_string']['type']); $new['query_string']['fields'] = ['post_title']; From 5f31059f043e023c02b2f94dbf1e31fe32f9ab1b Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Mon, 28 Jan 2019 08:30:36 +0100 Subject: [PATCH 2/3] ela1 --- src/Core.php | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/src/Core.php b/src/Core.php index e4d6475..e051ae7 100644 --- a/src/Core.php +++ b/src/Core.php @@ -54,6 +54,8 @@ public function add_actions() add_filter('ep_index_post_request_path', [$this, 'ep_index_post_request_path'], 1, 2); add_filter('ep_bulk_index_posts_request_args', [$this, 'ep_bulk_index_posts_request_args'], 10, 2); + add_filter('ep_config_mapping', [$this, 'ep_config_mapping']); + //workaround: https://github.com/10up/ElasticPress/pull/1158 add_filter('ep_post_sync_args', [$this, 'ep_post_sync_args'], 10, 2); @@ -193,6 +195,43 @@ public function wildCardIt($s) return join($fin, ' '); } + public function ep_config_mapping($mapping) { + +$mapping['settings']['analysis']['analyzer']['default']['filter'] = [ 'standard','lowercase', 'edge_ngram']; +$mapping['settings']['analysis']['filter']['edge_ngram']['min_gram'] = 3; +$mapping['settings']['analysis']['filter']['edge_ngram']['max_gram'] = 128; //(quite bit but we're happy with this) +return $mapping; + //Create custom ngram index + $mapping['settings']['analysis']['analyzer']['my_index_analyzer'] = array( + 'type' => 'custom', + 'tokenizer' => 'standard', + 'filter' => array( + 'lowercase', + 'mynGram', + ), + ); + + $mapping['settings']['analysis']['analyzer']['my_search_analyzer'] = array( + 'type' => 'custom', + 'tokenizer' => 'standard', + 'filter' => array( + 'standard', + 'lowercase', + 'mynGram', + ), + ); + + $mapping['settings']['analysis']['filter']['mynGram'] = array( + 'type' => 'nGram', + 'min_gram' => 1, + 'max_gram'=> 3 + ); + + $mapping['settings']['analysis']['analyzer']['default']['filter'][] = 'mynGram'; + + + return $mapping; + } public function ep_formatted_args($args) { if (! array_key_exists('bool', $args['query'])) { @@ -202,16 +241,19 @@ public function ep_formatted_args($args) unset($args['query']['bool']['should']); $new = $args['query']['bool']['must'][0]; $new['query_string'] = $new['multi_match']; - $new['query_string']['query'] = $this->wildCardIt($new['query_string']['query']); +// $new['query_string']['query'] = $this->wildCardIt($new['query_string']['query']); $new['query_string']['query'] = str_replace( ['\\', '+', '-', '&', '|', '!', '(', ')', '{', '}', '[', ']', '^', '~', '?', ':'], ['\\\\', "\+", "\-", "\&", "\|", "\!", "\(", "\)", "\{", "\}", "\[", "\]", "\^", "\~", "\?", "\:"], $new['query_string']['query'] ); $new['query_string']['analyze_wildcard'] = true; + $new['query_string']['fuzziness'] = 5; + //$new['query_string']['analyzer'] = 'edge_ngram_analyzer'; + //$new['query_string']['analyzer'] = 'edge_ngram'; unset($new['query_string']['type']); $new['query_string']['fields'] = ['post_title']; - $new['query_string']['boost'] = 10; + //$new['query_string']['boost'] = 10; $new['query_string']['default_operator'] = 'AND'; unset($new['multi_match']); unset($new['type']); @@ -224,6 +266,25 @@ public function ep_formatted_args($args) } $args['sort'] = ['post_date' => ['order' => 'desc']]; + //Simplifie as fuck + // + $nq = (object) [ + 'query_string' => (object)[ + 'default_field' => 'post_title.post_title', + "query" => $new['query_string']['query'], + 'default_operator' => 'AND', + "analyze_wildcard" => true, + "fuzziness" => 5 + + ] + ]; + //Reset + unset($args['query']); + $args['query'] = $nq; + + + //echo "
";
+        //echo json_encode($args, JSON_PRETTY_PRINT);
         return $args;
     }
 

From a2dac782d8399eaf7e39c77a52376b7aa14142a3 Mon Sep 17 00:00:00 2001
From: Helmut Januschka 
Date: Wed, 30 Jan 2019 08:52:14 +0100
Subject: [PATCH 3/3] tweak elasticsearch

---
 src/Core.php | 99 +++++++++++++++++-----------------------------------
 1 file changed, 32 insertions(+), 67 deletions(-)

diff --git a/src/Core.php b/src/Core.php
index e051ae7..fc65f75 100644
--- a/src/Core.php
+++ b/src/Core.php
@@ -185,93 +185,44 @@ public function ep_index_post_request_args($args, $post)
 
     public function wildCardIt($s)
     {
-        return $s;
+        if($this->isExtenendEPQuery($s)) {
+          return $s;
+        }
         $w = explode(' ', $s);
         $fin = [];
         foreach ($w as $word) {
-            $fin[] = '*' . $word . '*';
+          $fin[] = '/.*' . $word . '.*/';
         }
 
         return join($fin, ' ');
     }
 
-    public function ep_config_mapping($mapping) {
+    public function ep_config_mapping($mapping)
+    {
 
+/*
+ * 
 $mapping['settings']['analysis']['analyzer']['default']['filter'] = [  'standard','lowercase', 'edge_ngram'];
 $mapping['settings']['analysis']['filter']['edge_ngram']['min_gram'] =  3;
 $mapping['settings']['analysis']['filter']['edge_ngram']['max_gram'] =  128; //(quite bit but we're happy with this)
-return $mapping;
-      //Create custom ngram index
-      $mapping['settings']['analysis']['analyzer']['my_index_analyzer'] = array(
-		'type' => 'custom',
-		'tokenizer' => 'standard',
-		'filter' => array(
-			'lowercase',
-			'mynGram',
-		),
-	);
-
-      $mapping['settings']['analysis']['analyzer']['my_search_analyzer'] = array(
-		'type' => 'custom',
-		'tokenizer' => 'standard',
-		'filter' => array(
-      'standard',
-			'lowercase',
-			'mynGram',
-		),
-	);
-
-      $mapping['settings']['analysis']['filter']['mynGram'] = array(
-		'type' => 'nGram',
-    'min_gram' => 1,
-    'max_gram'=> 3
-	);
-
- $mapping['settings']['analysis']['analyzer']['default']['filter'][] = 'mynGram';
-
-
-      return $mapping;
+ */
+        return $mapping;
     }
     public function ep_formatted_args($args)
     {
         if (! array_key_exists('bool', $args['query'])) {
             return $args;
         }
-        $args['query']['bool']['must'] = $args['query']['bool']['should'];
-        unset($args['query']['bool']['should']);
-        $new = $args['query']['bool']['must'][0];
-        $new['query_string'] = $new['multi_match'];
-//        $new['query_string']['query'] = $this->wildCardIt($new['query_string']['query']);
-        $new['query_string']['query'] = str_replace(
-            ['\\',    '+',  '-',  '&',  '|',  '!',  '(',  ')',  '{',  '}',  '[',  ']',  '^',  '~',  '?',  ':'],
-            ['\\\\', "\+", "\-", "\&", "\|", "\!", "\(", "\)", "\{", "\}", "\[", "\]", "\^", "\~", "\?", "\:"],
-            $new['query_string']['query']
-        );
-        $new['query_string']['analyze_wildcard'] = true;
-        $new['query_string']['fuzziness'] = 5;
-        //$new['query_string']['analyzer'] = 'edge_ngram_analyzer';
-        //$new['query_string']['analyzer'] = 'edge_ngram';
-        unset($new['query_string']['type']);
-        $new['query_string']['fields'] = ['post_title'];
-        //$new['query_string']['boost'] = 10;
-        $new['query_string']['default_operator'] = 'AND';
-        unset($new['multi_match']);
-        unset($new['type']);
-        array_unshift($args['query']['bool']['must'], $new);
-
-        foreach ($args['query']['bool']['must'] as $idx => &$r) {
-            if (isset($r['multi_match'])) {
-                unset($args['query']['bool']['must'][$idx]);
-            }
-        }
-        $args['sort'] = ['post_date' => ['order' => 'desc']];
 
         //Simplifie as fuck
         //
-        $nq = (object) [
-          'query_string' => (object)[
+        $qs = $args['query']['bool']['should'][0]['multi_match']['query'];
+        $qs = $this->wildCardIt($qs);
+        $qs = $this->sanitizeEPQuery($qs);
+        $nq =  [
+          'query_string' => [
             'default_field' => 'post_title.post_title',
-            "query" => $new['query_string']['query'],
+            "query" => $qs,  
             'default_operator' => 'AND',
             "analyze_wildcard" => true,
             "fuzziness" => 5
@@ -282,11 +233,25 @@ public function ep_formatted_args($args)
         unset($args['query']);
         $args['query'] = $nq;
 
-
         //echo "
";
-        //echo json_encode($args, JSON_PRETTY_PRINT);
+        //echo json_encode($args);
+        //exit;
         return $args;
     }
+    public function sanitizeEPQuery($q) {
+      if($this->isExtenendEPQuery($q)) {
+        return preg_replace("#^\!#", "", $q);
+      }
+       return str_replace(
+            ['\\',    '+',  '-',  '&',  '|',  '!',  '(',  ')',  '{',  '}',  '[',  ']',  '^',  '~',  '?',  ':'],
+            ['\\\\', "\+", "\-", "\&", "\|", "\!", "\(", "\)", "\{", "\}", "\[", "\]", "\^", "\~", "\?", "\:"],
+            $q
+        );
+
+    }
+    public function isExtenendEPQuery($q) {
+      return preg_match("#^\!#", $q);
+    }
 
     /*
      * /Elasticpress