How to enable :tsearch dictionary for pg_search mu

2019-05-08 16:43发布

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?

1条回答
劫难
2楼-- · 2019-05-08 17: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.

查看更多
登录 后发表回答