2009年3月29日 星期日

ultrasphinx memo(1)

Ultrasphinx File: README
Class: ActiveRecord :: Base
Class: Ultrasphinx :: Search
Delta indexing
Delta index 讓你不需要每次都對重新整個做index

First, in your .base file, set the indexer option delta to your maximum interval between full reindexes. A day or a week is good, depending. Add a little bit to account for the time it takes the actual index to run:
Now, configure your models for delta indexing in the is_indexed call:
Now you can run rake ultrasphinx:index:delta frequently, and only records that were changed within 1 day will be reindexed. You will need to run rake ultrasphinx:index:main once a day to move the delta contents into the main index.



Association scoping
A common use case is to only search records that belong to a particular parent model. Ultrasphinx configures Sphinx to support a :filters element on any date or numeric field, so any *_id fields you have will be filterable.

For example, say a Company has_many :users and each User has_many :articles. If you want to to filter Articles by Company, add company_id to the Article‘s is_indexed method. The best way is to grab it from the User association:
Now you can run:
If the associations weren‘t just has_many and belongs_to, you would need to use the :association_sql key to set up a custom JOIN.



Query Defaults
Note that you can set up your own query defaults in environment.rb:


Geographic distance
If you pass a :location Hash, distance from the location in meters will be available in your result records via the distance accessor:
Note that Sphinx expects lat/long to be indexed as radians. If you have degrees in your database, do the conversion in the is_indexed as so:
Then, set Ultrasphinx::Search.client_options[:location][:units] = ‘degrees‘.

The MySQL :double column type is recommended for storing location data. For Postgres, use float


Excerpt mode
就是想讓search 的關鍵字高亮的模式
You can have Sphinx excerpt and highlight the matched sections in the associated fields. Instead of calling run, call excerpt.

@search.excerpt

The returned models will be frozen and have their field contents temporarily changed to the excerpted and highlighted results.

There are some other keys you can set, such as excerpt size, HTML tags to highlight with, and number of words on either side of each excerpt chunk. Example (in environment.rb):

沒有留言: