首页
/ Mongoid Search 技术文档

Mongoid Search 技术文档

2024-12-18 17:59:45作者:凌朦慧Richard

安装指南

1. 添加 Gem

在项目的 Gemfile 中添加以下内容:

gem 'mongoid_search'

2. 安装 Gem

运行以下命令来安装 Gem:

bundle install

项目的使用说明

1. 定义搜索模型

首先,在你的模型中引入 Mongoid::Search 模块,并定义需要搜索的字段。例如:

class Product
  include Mongoid::Document
  include Mongoid::Search
  field :brand
  field :name
  field :unit
  field :info, type: Hash

  has_many   :tags
  belongs_to :category

  search_in :brand, :name, tags: :name, category: :name, info: %i[summary description]
  search_in :unit, index: :_unit_keywords
end

class Tag
  include Mongoid::Document
  field :name

  belongs_to :product
end

class Category
  include Mongoid::Document
  field :name

  has_many :products
end

2. 保存数据并生成关键词

当你保存一个 Product 对象时,_keywords 字段会自动生成:

p = Product.new brand: 'Apple', name: 'iPhone', unit: 'kilogram', info: { summary: 'Info-summary', description: 'Info-description' }
p.tags << Tag.new(name: 'Amazing')
p.tags << Tag.new(name: 'Awesome')
p.tags << Tag.new(name: 'Superb')
p.save
# => true
p._keywords
# => ["amazing", "apple", "awesome", "iphone", "superb", "Info-summary", "Info-description"]
p._unit_keywords
# => ["kilogram"]

3. 执行搜索

你可以通过 full_text_search 方法进行搜索,搜索结果会返回匹配的记录:

Product.full_text_search("apple iphone").size
# => 1

4. 动态字段搜索

你还可以通过定义方法来实现动态字段的搜索:

class ModelWithDynamicFields
  
  ...
  
  search_in :search_data

  def search_data
    # 将所有字符串字段的值拼接起来
    self.attributes.select{|k,v| v.is_a?(String) }.values.join(' ')
  end
end

5. 多索引搜索

你可以为不同的搜索需求定义多个索引,并通过 index 参数指定使用哪个索引进行搜索:

Product.full_text_search("kilogram", index: :_unit_keywords).size
# => 1

项目API使用文档

1. full_text_search 方法

full_text_search 方法用于执行全文搜索,支持以下参数:

  • query: 搜索的关键词。
  • match: 匹配模式,可以是 :any:all,默认为 :any
  • allow_empty_search: 是否允许空搜索,默认为 false
  • relevant_search: 是否返回相关性信息,默认为 false
  • index: 指定使用的索引,默认为 _keywords

示例:

Product.full_text_search('apple motorola', match: :any).size
# => 1

Product.full_text_search('apple motorola', match: :all).size
# => 0

2. search_in 方法

search_in 方法用于定义需要搜索的字段,支持直接字段和关联字段:

search_in :brand, :name, tags: :name, category: :name, info: %i[summary description]

3. rake mongoid_search:index 任务

该任务用于重新索引所有现有记录:

$ rake mongoid_search:index

项目安装方式

1. 通过 Gemfile 安装

Gemfile 中添加:

gem 'mongoid_search'

然后运行:

bundle install

2. 初始化配置

你可以通过创建一个初始化文件来配置 Mongoid::Search 的选项:

Mongoid::Search.setup do |config|
  config.match = :any
  config.allow_empty_search = false
  config.relevant_search = false
  config.stem_keywords = false
  config.ignore_list = []
  config.regex_search = true
  config.regex = Proc.new { |query| /#{query}/ }
  config.ligatures = { "œ"=>"oe", "æ"=>"ae" }
  config.strip_symbols = /[._:;'\"`,?|+={}()!@#%^&*<>~\$\-\\\/\[\]]/
  config.strip_accents = /[^\s\p{Alnum}]/
  config.minimum_word_size = 2
end

通过以上步骤,你可以成功安装并使用 Mongoid Search 进行全文搜索。

热门项目推荐
相关项目推荐

项目优选

收起
Python-100-DaysPython-100-Days
Python - 100天从新手到大师
Python
610
115
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
286
79
mdmd
✍ WeChat Markdown Editor | 一款高度简洁的微信 Markdown 编辑器:支持 Markdown 语法、色盘取色、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性
Vue
111
25
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
60
48
RuoYi-Cloud-Vue3RuoYi-Cloud-Vue3
🎉 基于Spring Boot、Spring Cloud & Alibaba、Vue3 & Vite、Element Plus的分布式前后端分离微服务架构权限管理系统
Vue
45
29
go-stockgo-stock
🦄🦄🦄AI赋能股票分析:自选股行情获取,成本盈亏展示,涨跌报警推送,市场整体/个股情绪分析,K线技术指标分析等。数据全部保留在本地。支持DeepSeek,OpenAI, Ollama,LMStudio,AnythingLLM,硅基流动,火山方舟,阿里云百炼等平台或模型。
Go
1
0
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
205
57
MateChatMateChat
前端智能化场景解决方案UI库,轻松构建你的AI应用,我们将持续完善更新,欢迎你的使用与建议。 官网地址:https://matechat.gitcode.com
376
36
RuoYi-VueRuoYi-Vue
🎉 基于SpringBoot,Spring Security,JWT,Vue & Element 的前后端分离权限管理系统,同时提供了 Vue3 的版本
Java
182
44
frogfrog
这是一个人工生命试验项目,最终目标是创建“有自我意识表现”的模拟生命体。
Java
8
0