基于searchkick的样例
brew install elasticsearch
brew services start elasticsearch
gem 'searchkick', '2.5.0'
class SearchObj < ApplicationRecord
searchkick merge_mappings: true, mappings: {
search_obj:{
properties: {
name: {type: 'string', analyzer: "standard"},
cas: {type: 'string'}
}
}
}
def search_data
{
name: good_name,
cas: cas
}
end
# 手动调整搜索结构
# ops: {q: '12233', type: 1}
def self.es_search(opts)
bools = {
must: [], # a & b & c
should: [], # a || b || c
must_not: [] # !a & !b & !c
}
if opts[:q].present?
bools[:should] << {
match: {cas: { query: opts[:q], boost: 100 }}
}
bools[:should] << {
match: {name: {query: opts[:q], boost: 10}}
}
end
search_body = {
query: {
bool: bools
},
size: opts[:per_page] || 10,
from: ((opts[:page] || 1) - 1) * (opts[:per_page] || 10),
sort: "_score"
}
search body: search_body
end
end
一些参考链接
https://segmentfault.com/a/1190000011490134
基于elasticsearch-rails的样例
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
namespace :activities do
task refresh_es: :environment do
class VendorProductPromotionPriceEs
include ActiveModel::Model
include Elasticsearch::Model
index_name "mall-#{Rails.env}"
document_type "vendor_product_promotion_prices"
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
mappings do
indexes :cas, type: 'string', index: "not_analyzed", analyzer: 'snowball'
indexes :names, type: 'string', analyzer: 'snowball'
indexes :names_keywords, type: 'string', analyzer: 'keyword'
indexes :names_cn, type: 'string', analyzer: 'ik_max_word'
indexes :names_cn_keywords, type: 'string', analyzer: 'keyword'
indexes :status, type: 'integer'
indexes :vendor_ids, type: 'integer'
indexes :activity_ids, type: 'integer'
end
end
attr_accessor :chemi_id, :vendor_ids, :activity_ids
def chemical
@_chemical ||= Chemi.find_by_id(self.chemical_id)
end
def id
chemical_id
end
def as_indexed_json(options={})
h = chemical.as_indexed_json(options)
h.delete(:suggest)
h.delete(:suggest_cn)
h[:vendor_ids] = self.vendor_ids
h[:activity_ids] = self.activity_ids
h
end
end
results = ActiveRecord::Base.connection.select_all 'select distinct chemical_id, vendor_id,activity_id from vendor_product_promotion_prices'
cs = {}
results.each do |r|
cs[r['chemical_id']] ||= {}
cs[r['chemical_id']]['vendor_ids'] ||= []
cs[r['chemical_id']]['activity_ids'] ||= []
cs[r['chemical_id']]['vendor_ids'] << r['vendor_id']
cs[r['chemical_id']]['activity_ids'] << r['activity_id']
end
es = []
cs.each do |chemical_id, values|
es << VendorProductPromotionPriceEs.new(chemical_id: chemical_id, vendor_ids: values['vendor_ids'].to_a.uniq, activity_ids: values['activity_ids'].to_a.uniq)
end
index_name = "whmall-#{Rails.env}"
document_type = "vendor_product_promotion_prices"
es.in_groups_of(1000,false).each do |batch|
VendorProductPromotionPriceEs.__elasticsearch__.client.bulk( index: index_name, type: document_type, body: batch.map { |model| { index: { _id: model.id, data: model.as_indexed_json}}})
end
end
end
rake environment elasticsearch:import:model FORCE=y CLASS='Chemi' SCOPE='with_products'
更新产品在elasticsearch中的索引
chem = Chem.find(19385)
chem.__elasticsearch__.index_document(refresh: true) # index_document是创建索引, refresh: true 是强制ES刷新索引,刷新后才能search
# #需要注意,chem.danger 需要设置为0,不能是nil
获取产品在ES里的索引信息
chem.__elasticsearch__.as_indexed_json
删除产品在ES中的索引
chem.__elasticsearch__.delete_document(refresh: true)
重建所有的产品索引
Chemical.__elasticsearch__.reindex!
一些参考链接
https://www.rubydoc.info/gems/elasticsearch-api/
分词:
Elasticsearch 标准分词和ik分词的差别
https://blog.csdn.net/silent1/article/details/44590701
ElasticSearch最全分词器比较及使用方法 - 赵英超的博客 - CSDN博客:
https://blog.csdn.net/ZYC88888/article/details/83620572
另外的一些gem
sunspot/sunspot
https://blog.csdn.net/raosheng1993/article/details/45438573
https://ruby-china.org/topics/21473
搜索引擎选择: Elasticsearch与Solr
https://www.cnblogs.com/chowmin/articles/4629220.html
分享到:
相关推荐
(狂神)ElasticSearch快速入门笔记,ElasticSearch基本操作以及爬虫(Java-ES仿京东实战),包含了小狂神讲的东西,特别适合新手学习,笔记保存下来可以多看看。好记性不如烂笔头哦~,ElasticSearch,简称es,es是一个...
该自述文件通常会记录启动和运行应用程序所需的所有步骤。 您可能要讲的内容: Ruby版本 系统依赖 配置 数据库创建 数据库初始化 如何运行测试套件 服务(作业队列,缓存服务器,搜索引擎等) 部署说明 ...
Searchkick能够学习你的用户正在寻找什么。随着更多的人搜索,它变得更聪明并获得更好的结果。这对开发者友好,让你的用户感到神奇。
elasticsearch elasticsearch-6.2.2 elasticsearch-6.2.2.zip 下载
Logstash 和 Beats 有助于收集、聚合和丰富您的数据并将其存储在 Elasticsearch 中。Kibana 使您能够以交互方式探索、可视化和分享对数据的见解,并管理和监控堆栈。 Elasticsearch 为所有类型的数据提供近乎实时的...
一、概述 一般来说我们开发Elasticsearch会选择...2、elasticsearch-head (方便查看ES中的索引及数据) 3、Kibana(方便开发通过rest api 调试ES,有代码提示) 4、中文分词elasticsearch-analysis-ik (ik) 1、下载ela
elasticsearch-8.2.3 windows 版本。 Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的...
赠送jar包:elasticsearch-6.8.3.jar; 赠送原API文档:elasticsearch-6.8.3-javadoc.jar; 赠送源代码:elasticsearch-6.8.3-sources.jar; 赠送Maven依赖信息文件:elasticsearch-6.8.3.pom; 包含翻译后的API文档...
赠送jar包:elasticsearch-6.3.0.jar; 赠送原API文档:elasticsearch-6.3.0-javadoc.jar; 赠送源代码:elasticsearch-6.3.0-sources.jar; 赠送Maven依赖信息文件:elasticsearch-6.3.0.pom; 包含翻译后的API文档...
docker run --name elasticsearch7.16.3 -p 127.0.0.1:9200:9200 -p 127.0.0.1:9300:9300 -e "discovery.type=single-node" -v /Users/xingyue/Home/xingyue/学习/工程化/es/elasticsearch.yml:/usr/share/elastic...
elasticsearch-analysis-ik 是一个常用的中文分词器,在 Elasticsearch 中广泛应用于中文文本的分析和搜索。下面是 elasticsearch-analysis-ik 分词器的几个主要特点: 中文分词:elasticsearch-analysis-ik 是基于...
赠送jar包:elasticsearch-6.8.3.jar; 赠送原API文档:elasticsearch-6.8.3-javadoc.jar; 赠送源代码:elasticsearch-6.8.3-sources.jar; 赠送Maven依赖信息文件:elasticsearch-6.8.3.pom; 包含翻译后的API文档...
ElasticSearch实战 in Action(原著原版)压缩包,ElasticSearch文档,ES全文检索从入门到精通,高清PDF,是学习es和精通es必备手册,能快速达到精通ElasticSearch,一册在手,ElasticSearch无忧,开发、学习、调优...
赠送jar包:elasticsearch-6.2.3.jar; 赠送原API文档:elasticsearch-6.2.3-javadoc.jar; 赠送源代码:elasticsearch-6.2.3-sources.jar; 赠送Maven依赖信息文件:elasticsearch-6.2.3.pom; 包含翻译后的API文档...
分享一套完整版视频课程——分布式搜索引擎Elasticsearch...Elasticsearch安装和插件介绍 Elasticsearch基本使用和简单查询 Elasticsearch的Java客户端使用 Elasticsearch索引和Mapping Elasticsearch搜索深入 Elastic
1、进入到Elasticsearch的安装路径下,本文中以该路径为例子:/ultra/ES/elasticsearch-2.3.4。先安装license,执行以下命令: ./bin/plugin install license 2、再安装shield,执行以下命令: ./bin/plugin ...
讲述Elasticsearch的架构和Elasticsearch 的核心 概念 二、索引数据 单词 文档矩阵,倒排索引,倒排索引实例,单词词典 三、集群(Cluster) 发现机制、节点角色、脑裂现象 四、Elasticsearch 集群扩容和容灾 集群健康...
ElasticSearch数据导出 elasticsearch单文档数据导出 支持自定义查询 导出数据Json文件
Elasticsearch 是一个开源的高度可扩展的全文搜索和分析引擎,拥有查询近实时的超强性能。 大名鼎鼎的Lucene 搜索引擎被广泛用于搜索领域,但是操作复杂繁琐,总是让开发者敬而远之。而 Elasticsearch将 Lucene 作为...
消费kafka数据,然后批量导入到Elasticsearch,本例子使用的kafka版本0.10,es版本是6.4,使用bulk方式批量导入到es中,也可以一条一条的导入,不过比较慢。 <groupId>org.elasticsearch <artifactId>elastic...