Question

I have a Rails4 app with Postgresql(PG_search) on a MAC. I created the synonym dictionary through rails migration. The synonym dictionary is somewhat working. Setup below

Migrations

CREATE TEXT SEARCH DICTIONARY custom_synonyms_for_keywords ( TEMPLATE = synonym, SYNONYMS = synonyms_for_keywords );
CREATE TEXT SEARCH CONFIGURATION simple_syns_keywords (copy=simple);
ALTER TEXT SEARCH CONFIGURATION simple_syns_keywords ALTER MAPPING FOR asciiword WITH custom_synonyms_for_keywords;

my_synonyms file:
fshk bim24os
fridge refrigerator
postgres pgsql
postgresql pgsql
indices index*

Controller

class Product < ActiveRecord::Base
   pg_search_scope :pg_search, against: :sku,
         using: {
           tsearch: { prefix: true, dictionary: "simple_syns_keywords" }
         }
end

When i run

Product.pg_search("fshk") # returns record of "bim24os"
Product.pg_search("fridge") # returns no record of "refrigerator"

But when i run this, it returns the correct value

SELECT ts_lexize('custom_synonyms_for_keywords', 'fshk');  # returns "bim24os"
SELECT ts_lexize('custom_synonyms_for_keywords', 'fridge');    # returns "refrigerator"

RESULTS

Product.pg_search("fshk")
   Product Load (3.0ms)  SELECT "products".*, ((ts_rank((to_tsvector('simple_syns_keywords', coalesce("products"."sku"::text, ''))), (to_tsquery('simple_syns_keywords', ''' ' || 'fshk' || ' ''' || ':*')), 0))) AS pg_search_rank FROM "products" WHERE (((to_tsvector('simple_syns_keywords', coalesce("products"."sku"::text, ''))) @@ (to_tsquery('simple_syns_keywords', ''' ' || 'fshk' || ' ''' || ':*')))) ORDER BY pg_search_rank DESC, "products"."id" ASC
=> #<ActiveRecord::Relation [#Product id: 1, sku: "BIM24OS"]

Product.pg_search("fridge")
   Product Load (1.8ms)  SELECT "products".*, ((ts_rank((to_tsvector('simple_syns_keywords', coalesce("products"."sku"::text, ''))), (to_tsquery('simple_syns_keywords', ''' ' || 'fridge' || ' ''' || ':*')), 0))) AS pg_search_rank FROM "products" WHERE (((to_tsvector('simple_syns_keywords', coalesce("products"."sku"::text, ''))) @@ (to_tsquery('simple_syns_keywords', ''' ' || 'fridge' || ' ''' || ':*')))) ORDER BY pg_search_rank DESC, "products"."id" ASC
=> ActiveRecord::Relation []

I'm i missing a step because i'm not sure why it's not working. Any help is appreciated.

Thanks

Was it helpful?

Solution

Got it working by adding "simple" in the third line of my SQL migration.

From

ALTER TEXT SEARCH CONFIGURATION simple_syns_keywords 
  ALTER MAPPING FOR asciiword 
  WITH custom_synonyms_for_keywords;

To

ALTER TEXT SEARCH CONFIGURATION simple_syns_keywords 
  ALTER MAPPING FOR asciiword 
  WITH custom_synonyms_for_keywords, simple;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top