Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable :tsearch dictionary for pg_search multi-search?

I'm adding pg_search into a Rails app. I'm following the instructions on github and this railscast, but I've run into a problem.

I'm setting up a multi model search, and I have a basic implementation working. But I want to extend pg_seach to use its English dictionary.

I already have an initializer:

PgSearch.multisearch_options = {
  :using => [:tsearch,:trigram],
  :ignoring => :accents
}

So, from what I've read, it looks like adding the dictioary should be as simple as

PgSearch.multisearch_options = {
  :using => [:tsearch => [:dictionary => "english"],:trigram],
  :ignoring => :accents
}

But when I start my server

...config/initializers/pg_search.rb:2: syntax error, unexpected ']', expecting tASSOC (SyntaxError)
  :using => [:tsearch => [:dictionary => "english"],:trigram],

I've tried swapping square for curly brackets, and all the other syntax permutations I can think of, but no luck.

What is the correct syntax here? And why aren't my attempts valid, as I've followed the syntax for scoped searches?

like image 365
Andy Harvey Avatar asked Dec 21 '22 22:12

Andy Harvey


1 Answers

What you posted is not valid Ruby syntax.

You want something like this:

PgSearch.multisearch_options = {
  :using => {
    :tsearch => {
      :dictionary => "english"
    },
    :trigram => {}
  },
  :ignoring => :accents
}

The reason is that you must use a Hash if you want to have key-value pairs. So essentially, pg_search allows 2 syntaxes:

:using => someArray # such as [:tsearch, :trigram]

which means "use tsearch and trigram, both with the default options"

or

:using => someHash # such as {:tsearch => optionsHash1, :trigram => optionsHash2}

which means "use tsearch with some options from optionsHash1, and use trigram with some options from OptionsHash2"

Let me know if there's anything I can do to clarify. It's pretty basic Ruby syntax, but I understand that the fact that pg_search accepts both formats can be confusing to those who aren't as familiar.

like image 76
Grant Hutchins Avatar answered Jan 30 '23 01:01

Grant Hutchins