acts_as_indexed

acts_as_indexed

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

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

acts_as_indexedRails全文搜索插件索引Github开源项目

= 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 <tt>bundle install</tt>. 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 <tt>vendor/plugins</tt> 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 <tt>lib/acts_as_indexed/configuration.rb</tt>

=== 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.

编辑推荐精选

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具酷表ChatExcelAI智能客服AI营销产品使用教程
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
AIWritePaper论文写作

AIWritePaper论文写作

AI论文写作指导平台

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

AI辅助写作AI工具AI论文工具论文写作智能生成大纲数据安全AI助手热门
博思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

AI办公办公工具AI工具博思AIPPTAI生成PPT智能排版海量精品模板AI创作热门
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

iTerms

iTerms

企业专属的AI法律顾问

iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。

SimilarWeb流量提升

SimilarWeb流量提升

稳定高效的流量提升解决方案,助力品牌曝光

稳定高效的流量提升解决方案,助力品牌曝光

Sora2视频免费生成

Sora2视频免费生成

最新版Sora2模型免费使用,一键生成无水印视频

最新版Sora2模型免费使用,一键生成无水印视频

Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

下拉加载更多