ElasticSearch

本文介绍如何使用mongoid-elasticsearch gem实现MongoDB与Elasticsearch的集成,包括索引创建、搜索操作及批量重新索引的方法。此外还提供了自定义搜索及排序的相关资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mongoid-elasticsearch

https://github.com/rs-pro/mongoid-elasticsearch

gem 'mongoid-elasticsearch'
$ bundle

class Post
  include Mongoid::Document
  include Mongoid::Elasticsearch
  elasticsearch!
end

Post.es.search 'test text' # shortcut for Post.es.search({q: 'test text'})
result = Post.es.search body: {query: {...}, facets: {...}} etc
result.raw_response
result.results # by default returns an Enumerable with Post instances exactly
               # like they were loaded from MongoDB
Post.es.index.create(Post.es.index_all) # create index (done automatically on app boot)
Post.es.index.destroy(Post.es.index.delete) # drop index
Post.es.index.reset # recreate index
Post.es.index.refresh # force index update (useful for specs)
Post.es.client # Elasticsearch::Client instance

response = Mongoid::Elasticsearch.search 'test'

Reindexing

All models

rake es:reindex will reindex all indices managed by Mongoid::Elasticsearch

One model - Simple bulk

This is the preferred (fastest) method to reindex everything

Music::Video.es.index_all
One model - Simple
Communities::Thread.es.index.reset
Communities::Thread.enabled.each do |ingr|
  ingr.es_update
end
 

自定义elasticsearch! : http://stackoverflow.com/questions/21430601/sorting-search-results-with-mongoid-elasticsearch-gem

ElasticSearch搜索: http://www.qwolf.com/?p=1387

转载于:https://www.cnblogs.com/qinyan20/p/3858644.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值