5

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?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Andy Harvey
  • 12,333
  • 17
  • 93
  • 185

1 Answers1

11

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.

Grant Hutchins
  • 4,275
  • 1
  • 27
  • 32
  • Thanks @nertzy. I could have sworn I tried this syntax, even though I posted a different variation in my question in an attempt to follow the guidelines. Thinking about it, actually I may have omitted the empty array for `:trigram`. Anyway, it's now working, thanks for helping me track this down! – Andy Harvey May 11 '12 at 03:06