小木木 发表于 2017-5-21 09:24:19

rails中Elasticsearch的客户端Tire配置

$ gem install tire   ||  https://github.com/karmi/retire
#加载

#model中加载tire模块 
class Article < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
end
#索引的名字
 index_name "#{Tire::Model::Search.index_prefix}<模型名字>"
 
#建立索引

class Article < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :id,         :index    => :not_analyzed
indexes :title,      :analyzer => 'snowball', :boost => 100
indexes :content,      :analyzer => 'snowball'
indexes :content_size, :as       => 'content.size'
indexes :author,       :analyzer => 'keyword'
indexes :published_on, :type => 'date', :include_in_all => false
end
end
# analyzer:分词【keyword,snowball…】type:类型【string, integer, date ...】 boost:权重
 
# 创建json

class Article < ActiveRecord::Base
# 可以添加一些方法
def to_indexed_json
names      = author.split(/\W/)
last_name= names.pop
first_name = names.join
{
:title   => title,
:content => content,
:author=> {
:first_name => first_name,
:last_name=> last_name
}
}.to_json
end
end
#... 添加方法
def is_admin? do
...
end
def to_indexed_json
to_json(
include: {:user{methods: [:is_admin?]}})
end
 
# 更新索引
 

    class Article < ActiveRecord::Base
include Tire::Model::Search
after_save do
update_index if state == 'published'
end
end
 
# 构建搜索的json数据

……
# 搜索方法,静态方法
DEFAULT_QUERY_FIELDS = ['title','des']
def self.search(params)
# page:分页页数,per_page:每页的数量, highlight:高亮
# load:不加这个以为着要直接用elasticsearch搜索的json,通过 _source 或fields 取JSON属性,如果加上true 或 {include: user},返回的结果有对象
tire.search(:page => (params[:page] || 1), per_page: 10, highlight: :name, load: true) do
# 表示查询项
query do
# 表示对比值
boolean do
term单一属性
must { term 'is_admin?', true }      
must { string (params[:q] || '*'), default_operator: "AND", fields: DEFAULT_QUERY_FIELDS }
end
end
# terms表述属性是[...], 与后面的交集
filter :terms, 'tags' => ['ruby', 'java']
## range 表示范围
filter :range, 'price' => {gte: 1}
filter :range, 'price' => {lte: 2}
# 表示被包含
facet 'global-tags', :global => true do
terms :tags
end
# 排序(添加时间索引 type:date)
sort { by 'created_at', "desc" }
end
end
……
 

简单类型:
String:字符型最常用的
Integer:整型
Long:长整型
Float:浮点型
Double:双字节型
Boolean:布尔型
复杂类型:
Array:数组型
Object:对象类型
Nested: 嵌入类型用的还是比较多的
multi_field允许为一个字段设置多个数据类型。应用multi_field的一个最典型的场景是:"properties": { "created": { "type":"multi_field", "fields": { "created": { "type": "string" }, "date": { "type": "date"}}}}

   filter :nested, {path: 'cars'}.merge({query: (
Tire::Search::Query.new do
filtered do
query { all }
filter :range, 'cars.price' => {gte: params[:price_from].to_i} if params[:price_from].present?
filter :range, 'cars.price' => {lte: params[:price_to].to_i} if params[:price_to].present?
end
end).to_hash}) if params[:price_from].blank? && params[:price_to]
  tire 删除索引(这个索引的数据已经被删除了)

def self.date_authen!(klass, result)
begin
result.results
rescue ActiveRecord::RecordNotFound => ex
index_ids = ex.message.split('(').to_s.split(')').to_s.split(',').map(&:to_i)
kass_ids = klass.where(id: index_ids).select(:id).map(&:id)
delete_ids = index_ids - kass_ids
delete_ids.each do |id|
klass.tire.index.remove(klass.name.tableize.singularize, id)
end
klass.tire.index.refresh
end
result
end
 
 
 Nested http://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/
 
 查看index:sudo ./bin/plugin -install mobz/elasticsearch-head
页: [1]
查看完整版本: rails中Elasticsearch的客户端Tire配置