meilisearch / meilisearch-php

PHP wrapper for the Meilisearch API

Home Page:https://meilisearch.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Replace character with spoofing char before being imported

warmwhisky opened this issue · comments

Do you know of a way that I can replace a character in my index before it gets indexed.

I need to replace # with either a spoofing char or a unique key combination so that my searches don't cause idle timeouts in Meilisearch.

E.g. When my users searches for D#G#C#F#A#D# meilisearch searches instead for D G C F A D I believe. This causes huge response times on some of my pages as they include this search just by opening a page.

So until you can change the soft space char or disable some for Meilisearch I need to replace all # chars with a unique char or key combination so my meili index no longer contains #

Maybe I could replace all # with zxzxzxz or something else and then replace zxzxzxz with # before the search takes place. I know it sounds clunky but I believe its my only option to stop the slow response times I have experienced since going live with Meilisearch.

More info about my issue can be found here meilisearch/product#160 (comment)

Hi @warmwhisky, sorry for the delay.

I'm unsure how I can help you with your issue (I also read the discussion thread). Because if you use addDocuments to insert your data, it is just up to you to change the data before calling the method (unless you're using an abstraction layer on top of the meilisearch-php like laravel scout/symfony)...

📡 to @oluademola if you know a way to help @warmwhisky ?

Hi @brunoocasali

Sorry I did not mention that I am using Laravel with Scout.

Thank you you for the pointer with Scout. I changed the toSearchableArray to manage the replacements. Once I re-indexed I could see the replacements inside of Meilisearch mini dashboard.

Now with the search query C#G#C#F#A#D# C#G#C#F#A#D# the load time has gone down from Load: 22.21 s to Load: 905 ms which is a fantastic improvement.

I am not using As you type search yet, so I have no issue with these adjustments. Also I am stuck on Centos 7 which does not allow me to use Meilisearch above 0.27.2. Until my host upgrades to AlmaLinux I will stick with Meilisearch 0.27.2

Here is my toSearchableArray within my guitar tunings model

public function toSearchableArray() {

    $array = $this->toArray();

    $data = [
        'tuning'            => str_replace('#', 'zxzxzxzx', $array['tuning']),
        'name'              => str_replace('#', 'zxzxzxzx', $array['name']),
        'description'       => str_replace('#', 'zxzxzxzx', $array['description']),
        'perma_name_flats'  => str_replace('♭', 'qzqzqzqz', $array['perma_name_flats']),
    ];

    return $data;
}

And just before I query Meilisearch I replace '#' with 'zxzxzxzx' and '♭' with 'qzqzqzqz'

public function replaceSharp($search_query) {
    return str_replace('#','zxzxzxzx',$search_query);
}

public function replaceFlat($search_query) {
    return str_replace('♭','qzqzqzqz',$search_query);
}

As Meili just returns the actual data from the tables I do not need to do any more conversions.

Very pleased with the results.

Thanks for all of the attention and help