开发者

Indexing fields + custom text in with Thinking Sphinx

I've got indexes on a few different models, and sometimes the user might search for a value which exists in multiple models. Now, if the user is really only interested in data from one of the models I'd like the user to be able to pre/postfix the query with something to limit the scope.

For instance, if I only want to find a match in my Municipality model, I've set up an index in that model so that the user now can query "xyz municipality" (in quotes):

  define_index do
    indexes :name, :sortable => true
    indexes "name || ' municipality' name", :as => :extended_name, :type => :string
  end

This works just fine. Now I al开发者_开发知识库so have a Person model, with a relation to Municipality. I'd like, when searching only on the Person model, to have the same functionality available, so that I can say Person.search("xyz municipality") and get all people connected to that municipality. This is my current definition in the Person model:

  has_many :municipalities,  :through => :people_municipalities

  define_index do
    indexes [lastname, firstname], :as => :name, :sortable => true
    indexes municipalities.name, :as => :municipality_name, :sortable => true
  end

But is there any way I can create an index on this model, referencing municipalities, like the one I have on the Municipality model itself?


If you look at the generated SQL in the sql_query setting of config/development.sphinx.conf for source person_core_0, you'll see how municipalities.name is being concatenated together (I'd post an example, but it depends on your database - MySQL and PostgreSQL handle this completely differently).

I would recommend duplicating the field, and insert something like this (SQL is pseudo-code):

indexes "GROUP_CONCAT(' municipality ' + municipalities.name)",
  :as => :extended_municipality_names

Also: there's not much point adding :sortable true to either this nor the original field from the association - are you going to sort by all of the municipality names concat'd together? I'm guessing not :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜