gzmo
gzmo

Reputation: 173

Laravel TNTsearch custom index creation and usage for large SQL database table

Here is my situation, context, and dilemma.

Situation

I'm fairly new to Laravel and still learning the ropes. I recently installed TNTSearch and Laravel Scout and was able to create a model index using the below config. I created the index using the console command php artisan tntsearch:import "App\Models\Product" and can fuzzy search successfully with App\Models\Product::search($keyword)->get().

config/scout.php

'tntsearch' => [
    'storage' => storage_path() . '/index',
    'fuzziness' => 'auto',
    'fuzzy' => [
        'prefix_length' => 2, 
        'max_expansions' => 50, 
        'distance' => 4,
    ],
    'asYouType' => true
],

Context

I have an SQL database table with over 30k+ product records segmented per province (Canadian project), and instead of searching the whole index and later filter by market, I’d like to create one index per market and launch a search for a given market. I believe it will speed up the search and avoid returning results which will later be discarded! So basically having one product index file per province (i.e. products_on.index, products_qc.index, ...)

Dilemma/Issue

I am unable to find how to create such an index, have it update automatically and also how to use it. I scoured the Internet for tutorial/guidance and could only find scarce information I can hardly put together. I’d appreciate if someone could point me in the right direction or guide me on how to implement such a thing.

No answer is wrong, and any bits and pieces of information can help me greatly to “get up to speed.”

EDIT (July 30th, 2018):

I still haven't found the answer to my request but the more I search, the more I'm concluding search indexes are "tied" to a model, and it is not possible to have more than one index per model. So I would have to create one model extension per market from the original Listings model (Listings_QC, Listings_ON, ...). Then create an index per markets and search from those (Listings_QC::search(...)).

I'm not keen to create models based on data! Is this a good approach/practice?

Upvotes: 1

Views: 1260

Answers (1)

gzmo
gzmo

Reputation: 173

RESOLVED !

My inexperience with Laravel search index in general lead me in the wrong direction! I finally found a document explaining how to use searchBoolean() to search using "and". Modified my config as below to add the searchBoolean:

'tntsearch' => [
    'storage' => storage_path() . '/index',
    'fuzziness' => 'auto',
    'fuzzy' => [
        'prefix_length' => 2, 
        'max_expansions' => 50, 
        'distance' => 4,
    ],
    'asYouType' => true,
    'searchBoolean' => true
],

Then specify the market using the model's method toSearchableArray(), and add the market to any requested seach keyword.

For example, listing search with 'Alsace' for a QC market, I launch the search as Listings::search('Alsace QC')->get().

Voilà! May help others hitting the same "wall"!

Upvotes: 2

Related Questions