Project Icon

acts_as_indexed

为Ruby on Rails应用添加简单高效的全文搜索功能

acts_as_indexed是一个轻量级的Ruby on Rails全文搜索插件。它无需额外依赖,配置简单,支持布尔查询和相关性排序。开发者可以轻松为任意模型添加搜索功能,并根据需求进行灵活配置。该插件还提供分页支持和Heroku兼容性,适用于各种规模的Rails项目。

= acts_as_indexed

If you find this plugin useful, please consider a donation to show your support!

http://www.paypal.com/cgi-bin/webscr?cmd=_send-money

Paypal address: mailto:dougal.s@gmail.com

== Instructions

This plugin allows boolean-queried fulltext search to be added to any Rails app with no dependencies and minimal setup.

== Resources

=== Installation

==== Add to your Gemfile

gem 'acts_as_indexed'

Run bundle install. Done.

==== Still on Rails 2.x.x without Bundler?

./script/plugin install git://github.com/dougal/acts_as_indexed.git

If you don't have git installed, but still want the plugin, you can download the plugin from the GitHub page (http://github.com/dougal/acts_as_indexed) and unpack it into the vendor/plugins directory of your rails app.

=== Upgrading

When upgrading to a new version of acts_as_indexed it is recommended you delete the index directory and allow it to be rebuilt.

== Usage

=== Setup

Add +acts_as_indexed+ to the top of any models you want to index, along with a list of the fields you wish to be indexed.

class Post < ActiveRecord::Base acts_as_indexed :fields => [:title, :body]

...

end

The fields are not limited to model fields, but can be any instance method of the current model.

class User < ActiveRecord::Base acts_as_indexed :fields => [:address, :fullname]

def fullname
  self.firstname + ' ' + self.lastname
end

...

end

Any of the configuration options in the Further Configuration section can be added as to the acts_as_indexed method call. These will override any defaults or global configuration.

You can specify proc that needs to evaluate to true before the item gets indexed. This is useful if you only want items with a certain state to be included. The Proc is passed the current object's instance so you are able to test against that.

For example, if you have a visible column that is false if the post is hidden, or true if it is visible, you can filter the index by doing:

class Post < ActiveRecord::Base acts_as_indexed :fields => [:title, :body], :if => Proc.new { |post| post.visible? } ... end

=== Searching

==== With Relevance

To search with the most relevant matches appearing first, call the +find_with_index+ method on your model, passing a query as the first argument. The optional +ids_only+ parameter, when set to true, will return only the IDs of any matching records.

Returns array of Post objects ordered by relevance.

my_search_results = Post.find_with_index('my search query')

Pass any of the ActiveRecord find options to the search.

my_search_results = Post.find_with_index('my search query',{:limit => 10}) # return the first 10 matches.

Returns array of IDs ordered by relevance.

my_search_results = Post.find_with_index('my search query',{},{:ids_only => true}) # => [12,19,33...

==== Without Relevance (Scope)

If the relevance of the results is not important, call the +with_query+ named scope on your model, passing a query as an argument.

Returns array of Post objects.

my_search_results = Post.with_query('my search query')

Chain it with any number of ActiveRecord methods and named_scopes.

my_search_results = Post.public.with_query('my search query').find(:all, :limit => 10) # return the first 10 matches which are public.

=== Query Options

The following query operators are supported:

AND :: This is the default option. 'cat dog' will find records matching 'cat' AND 'dog'. NOT :: 'cat -dog' will find records matching 'cat' AND NOT 'dog' INCLUDE :: 'cat +me' will find records matching 'cat' and 'me', even if 'me' is smaller than the +min_word_size+ "" :: Quoted terms are matched as phrases. '"cat dog"' will find records matching the whole phrase. Quoted terms can be preceded by the NOT operator; 'cat -"big dog"' etc. Quoted terms can include words shorter than the +min_word_size+. ^ :: Terms that begin with ^ will match records that contain a word starting with the term. '^cat' will find matches containing 'cat', 'catapult', 'caterpillar' etc. ^"" :: A quoted term that begins with ^ matches any phrase that begin with this phrase. '^"cat d"' will find records matching the whole phrases "cat dog" and "cat dinner". This type of search is useful for autocomplete inputs.

=== Pagination

==== With Relevance

Pagination is supported via the +paginate_search+ method whose first argument is the search query, followed by all the standard will_paginate arguments.

@images = Image.paginate_search('girl', :page => 1, :per_page => 5)

==== Without Relevance (Scope)

Since +with_query+ is a named scope, WillPaginate can be used in the normal fashion.

@images = Image.with_query('girl').paginate(:page => 1, :per_page => 5)

=== Further Configuration

A config block can be provided in your environment files or initializers. Example showing changing the min word size:

ActsAsIndexed.configure do |config| config.min_word_size = 3 # More config as required... end

A full rundown of the available configuration options can be found in lib/acts_as_indexed/configuration.rb

=== Heroku Support

Acts As Indexed supports Heroku out-of-the-box. The index is created in the tmp directory, which is the only writeable part of the Heroku dyno filesystem. Please read Heroku's documentation( https://devcenter.heroku.com/articles/read-only-filesystem) regarding their file-system.

== RDoc Documentation

View the rdoc documentation online[http://rdoc.info/projects/dougal/acts_as_indexed/].

== Problems, Comments, Suggestions?

All of the above are most welcome. mailto:dougal.s@gmail.com

== Contributors

A huge thanks to all the contributors to this library. Without them many bugfixes and features wouldn't have happened.

  • Douglas F Shearer - http://douglasfshearer.com
  • Thomas Pomfret
  • Philip Arndt
  • Fernanda Lopes
  • Alex Coles
  • Myles Eftos
  • Edward Anderson
  • Florent Guilleux
  • Ben Anderson
  • Theron Toomey
  • Uģis Ozols
  • Gabriel Namiman
  • Roman Samoilov
  • David Turner
  • Pascal Hurni
  • Ryan Kopf

== Unicode (UTF8) Support

At the moment acts_as_indexed only works with Unicode characters when used in the following way:

https://gist.github.com/193903bb4e0d6e5debe1

I have rewritten the tokenization process to allow easier handling of this in the future.

项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

白日梦AI

白日梦AI提供专注于AI视频生成的多样化功能,包括文生视频、动态画面和形象生成等,帮助用户快速上手,创造专业级内容。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

讯飞绘镜

讯飞绘镜是一个支持从创意到完整视频创作的智能平台,用户可以快速生成视频素材并创作独特的音乐视频和故事。平台提供多样化的主题和精选作品,帮助用户探索创意灵感。

Project Cover

讯飞文书

讯飞文书依托讯飞星火大模型,为文书写作者提供从素材筹备到稿件撰写及审稿的全程支持。通过录音智记和以稿写稿等功能,满足事务性工作的高频需求,帮助撰稿人节省精力,提高效率,优化工作与生活。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

AIWritePaper论文写作

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号