]> _ Git - cubist_cms-back.git/commitdiff
wip #3305 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 3 Jan 2020 16:51:12 +0000 (17:51 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 3 Jan 2020 16:51:12 +0000 (17:51 +0100)
src/app/Magic/Search.php

index da79f7725a3f57bbcba9a84c5d0460ac9d1d6ca7..aa0f32054359b2ce532cf94f819395d227b0106f 100644 (file)
 
 namespace Cubist\Backpack\app\Magic;
 
+use Cubist\Backpack\app\Magic\Models\Locale;
 use Cubist\Backpack\app\Magic\Menu\Menu;
 use Cubist\Util\XML\DOMSelector;
 use Cviebrock\LaravelElasticsearch\Facade as Elasticsearch;
 
 class Search
 {
+    protected static $_mappings = ['fr' =>
+        [
+            'type' => 'text',
+            'analyzer' => 'french_light',
+            'fields' => [
+                'stemmed' => [
+                    'type' => 'text',
+                    'analyzer' => 'french_heavy'
+                ]
+            ]
+        ],
+        'en' => [
+            'type' => 'text',
+            'analyser' => 'english',
+        ],
+        'de' => [
+            'type' => 'text',
+            'analyser' => 'german'
+        ],
+        'es' => [
+            'type' => 'text',
+            'analyser' => 'spanish'
+        ],
+        'it' => [
+            'type' => 'text',
+            'analyser' => 'italian'
+        ],
+        'pt' => [
+            'type' => 'text',
+            'analyser' => 'portuguese',
+        ]
+    ];
+
+    protected static $_analysis = ['fr' =>
+        [
+            "filter" => [
+                "french_elision" => [
+                    "type" => "elision",
+                    "articles_case" => true,
+                    "articles" => ["l", "m", "t", "qu", "n", "s", "j", "d", "c", "jusqu", "quoiqu", "lorsqu", "puisqu"]
+                ],
+                "french_synonym" => [
+                    "type" => "synonym",
+                    "ignore_case" => true,
+                    "expand" => true,
+                    "synonyms" => [
+                        "salade, laitue",
+                        "mayo, mayonnaise",
+                        "grille, toaste",
+                        'pmi, pm instrumentation',
+                    ]
+                ],
+                "french_stemmer" => [
+                    "type" => "stemmer",
+                    "language" => "light_french"
+                ]
+            ],
+            "analyzer" => [
+                "french_heavy" => [
+                    "tokenizer" => "icu_tokenizer",
+                    "char_filter" => ["html_strip"],
+                    "filter" => [
+                        "french_elision",
+                        "icu_folding",
+                        "french_synonym",
+                        "french_stemmer"
+                    ]
+                ],
+                "french_light" => [
+                    "tokenizer" => "icu_tokenizer",
+                    "char_filter" => ["html_strip"],
+                    "filter" => [
+                        "french_elision",
+                        "icu_folding"
+                    ]
+                ]
+            ]
+        ]
+    ];
+
     public static function index()
     {
-        $index = config('cubist.internal_search_index');
+        foreach (Locale::getLocales() as $locale) {
+            self::indexLocale($locale);
+        }
+    }
+
+    public static function indexLocale($locale)
+    {
+        $index = config('cubist.internal_search_index') . '_' . $locale;
 
         try {
             Elasticsearch::indices()->delete(['index' => $index]);
@@ -18,15 +106,18 @@ class Search
             echo $e->getMessage();
         }
 
+        $analysis = self::_analysis($locale);
+        $settings = [];
+        if (null !== $analysis) {
+            $settings['analysis'] = $analysis;
+        }
+
         Elasticsearch::indices()->create(
             [
                 'index' => $index,
                 'body' => [
-                    'settings' =>
-                        [
-                            'analysis' => self::_french(),
-                        ],
-                    'mappings' => self::_typeMapping(true),
+                    'settings' => $settings,
+                    'mappings' => self::_typeMapping($locale, true),
                 ]
             ]);
 
@@ -79,94 +170,48 @@ class Search
     }
 
 
-    protected static function _typeMapping($source = true)
+    protected static function _typeMapping($locale, $source = true)
     {
         $res = [];
         if ($source) {
-            $res = ['_source' => [
-                'enabled' => true,
-            ]
+            $res = [
+                '_source' => [
+                    'enabled' => true,
+                ]
             ];
         }
 
+        $mapping = self::_mapping($locale);
+
 
         $res['properties'] = [
-            'short_title' => self::_frenchMapping(),
-            'long_title' => self::_frenchMapping(),
-            'description' => self::_frenchMapping(),
-            'keywords' => self::_frenchMapping(),
-            'main' => self::_frenchMapping(),
+            'short_title' => $mapping,
+            'long_title' => $mapping,
+            'description' => $mapping,
+            'keywords' => $mapping,
+            'main' => $mapping,
         ];
 
         return $res;
 
     }
 
-    protected static function _frenchMapping()
+    protected static function _mapping($locale)
     {
-        return [
-            'type' => 'text',
-            'analyzer' => 'french_light',
-            'fields' => [
-                'stemmed' => [
-                    'type' => 'text',
-                    'analyzer' => 'french_heavy'
-                ]
-            ]
-        ];
+        return self::$_mappings[$locale];
     }
 
-    protected static function _french()
+    protected static function _analysis($locale)
     {
-        return [
-            "filter" => [
-                "french_elision" => [
-                    "type" => "elision",
-                    "articles_case" => true,
-                    "articles" => ["l", "m", "t", "qu", "n", "s", "j", "d", "c", "jusqu", "quoiqu", "lorsqu", "puisqu"]
-                ],
-                "french_synonym" => [
-                    "type" => "synonym",
-                    "ignore_case" => true,
-                    "expand" => true,
-                    "synonyms" => [
-                        "salade, laitue",
-                        "mayo, mayonnaise",
-                        "grille, toaste",
-                        'pmi, pm instrumentation',
-                    ]
-                ],
-                "french_stemmer" => [
-                    "type" => "stemmer",
-                    "language" => "light_french"
-                ]
-            ],
-            "analyzer" => [
-                "french_heavy" => [
-                    "tokenizer" => "icu_tokenizer",
-                    "char_filter" => ["html_strip"],
-                    "filter" => [
-                        "french_elision",
-                        "icu_folding",
-                        "french_synonym",
-                        "french_stemmer"
-                    ]
-                ],
-                "french_light" => [
-                    "tokenizer" => "icu_tokenizer",
-                    "char_filter" => ["html_strip"],
-                    "filter" => [
-                        "french_elision",
-                        "icu_folding"
-                    ]
-                ]
-            ]
-        ];
+        if (!isset(self::$_analysis[$locale])) {
+            return null;
+        }
+        return self::$_analysis[$locale];
     }
 
-    public static function query($term, $type = null, $limit = null)
+    public static function query($term, $locale, $type = null, $limit = null)
     {
-        $index = config('cubist.internal_search_index');
+        $index = config('cubist.internal_search_index') . '_' . $locale;
         if (null === $limit) {
             $limit = 50;
         }