Mongoid Search 技术文档
2024-12-20 07:32:53作者:凌朦慧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 进行全文搜索。
登录后查看全文
热门项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00
MiniMax-M2.5MiniMax-M2.5开源模型,经数十万复杂环境强化训练,在代码生成、工具调用、办公自动化等经济价值任务中表现卓越。SWE-Bench Verified得分80.2%,Multi-SWE-Bench达51.3%,BrowseComp获76.3%。推理速度比M2.1快37%,与Claude Opus 4.6相当,每小时仅需0.3-1美元,成本仅为同类模型1/10-1/20,为智能应用开发提供高效经济选择。【此简介由AI生成】Python00
ruoyi-plus-soybeanRuoYi-Plus-Soybean 是一个现代化的企业级多租户管理系统,它结合了 RuoYi-Vue-Plus 的强大后端功能和 Soybean Admin 的现代化前端特性,为开发者提供了完整的企业管理解决方案。Vue06- RRing-2.5-1TRing-2.5-1T:全球首个基于混合线性注意力架构的开源万亿参数思考模型。Python00
Qwen3.5Qwen3.5 昇腾 vLLM 部署教程。Qwen3.5 是 Qwen 系列最新的旗舰多模态模型,采用 MoE(混合专家)架构,在保持强大模型能力的同时显著降低了推理成本。00
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
570
3.85 K
Ascend Extension for PyTorch
Python
386
458
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
894
680
暂无简介
Dart
805
198
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
354
211
昇腾LLM分布式训练框架
Python
120
146
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
1
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
68
20
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.37 K
781