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

允我心安 提交于 2019-12-07 07:49:42

问题


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:


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.



来源:https://stackoverflow.com/questions/10512642/how-to-enable-tsearch-dictionary-for-pg-search-multi-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!