我有一个Ruby on rails 3.2应用程序.我想在已经填充了大量数据的模型上启用基于文本的搜索.假设模型类的名称是Post.我正在计划使用elasticsearch,因为我听说它是最好的实时搜索引擎之一,我正在使用轮胎gem,以便我的应用程序可以与elasticsearch进行交互.
由于我是elasticsearch的新手,因此无法为模型的现有数据创建索引.我使用mongodb作为后端数据库.谁能告诉我如何导入索引.
我已经尝试过了
Tire.index "posts" do
import Post.all
end
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
BSON::InvalidObjectId: illegal ObjectId format: Career Guidance
from /Users/anirvan/.rvm/gems/ruby-1.9.3-p125/gems/bson-1.5.1/lib/bson/types/object_id.rb:126:in `from_string'
Run Code Online (Sandbox Code Playgroud)
有人可以帮我从这里出去吗 ?
我有一个Resource对象,它包含以下字段:title,description和body.
使用轮胎宝石我想使用标准分析仪来标题和描述,但对于身体领域我想使用雪球分析仪.
所以在我的resource.rb文件中,我有以下代码:
mapping do
indexes :title, type: 'string', :index => :not_analyzed, :store => true
indexes :description, type: 'string'
indexes :body, type: 'string', :analyzer => 'snowball'
end
def self.search(params)
search_query = params[:query]
tire.search(page: params[:page], per_page: 15) do |s|
s.query { string params[:query], default_operator: "AND"} if params[:query].present?
# do i need another line here specifically for description?
#s.query { string "body:#{params[:query]}", analyzer: 'snowball'} if params[:query].present?
end
end …Run Code Online (Sandbox Code Playgroud) 使用Elasticsearch与Rails 3和轮胎宝石.
我有一些方面可以在几个领域工作,但我现在有一个特殊的要求,并不确定它是否可行.我的模型Project上有两个字段,它们都存储相同的值:Country1和Country2
允许用户为项目存储最多两个国家/地区.两者的下拉菜单都是相同的.这两个领域都不需要.
我想要的是一个单独的方面,它"合并"来自Country1和Country2的值,并且可以智能地处理这些方面的点击(即无论是1还是2都会找到它)
这是我到目前为止的模型:(注意Country1/2可以是多个单词)
class Project < ActiveRecord::Base
mapping do
indexes :id
indexes :title, :boost => 100
indexes :subtitle
indexes :country1, :type => 'string', :index => 'not_analyzed'
indexes :country2, :type => 'string', :index => 'not_analyzed'
end
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 10) do
query do
boolean do
must { string params[:query], default_operator: "AND" } if params[:query].present?
must { term :country1, params[:country] } if params[:country].present?
end
end
sort { by :display_type, "desc" }
facet "country" do
terms …Run Code Online (Sandbox Code Playgroud) 您有什么建议如何使用cancan执行弹性搜索结果(使用轮胎)的授权?
我的方法是让cancan加载并授权模型并将所有授权的id传递给轮胎搜索,或者反之亦然.
你怎么看?
我有玩家和玩家有来自Profil的全名.我在Player上使用elasticsearch.我需要按姓名默认排序.我怎么设置它?来自Player类文件的代码:
......
mapping do
indexes :id, type: 'integer'
indexes :fullname, boost: 10
indexes :name
indexes :surname
indexes :position_name
indexes :info
indexes :club_id, type: 'integer'
indexes :club_name
end
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 20) do
query do
boolean do
if params[:query].present?
must { string params[:query], default_operator: "AND" }
else
must {string '*'}
end
must { term :position_id, params[:position_id]} if params[:position_id].present?
must { term :club_id, params[:club_id]} if params[:club_id].present?
end
end
# filter :term, user_id: params[:user_id] if params[:user_id].present?
# sort { …Run Code Online (Sandbox Code Playgroud) 我已将相关字段映射为多字段.我已经设置了'名称'属性,使用雪球分析仪进行分析,并将'精确'设置为未分析.我可以在这个字段上搜索并过滤.我无法对这个领域进行排序.尝试排序时,弹性返回错误"无法对每个文档具有多个值的字符串类型进行排序,或者每个字段使用多个标记".
我试图创建一个名为'raw'的附加字段,类似于exact,这也不起作用.下面是我的映射以及我如何尝试通过轮胎gem进行排序:
mapping do
indexes :sectors, :type => 'object',
:properties => { :name => { :type => 'multi_field',
:fields => {
:name => { :type => 'string', :analyzer => 'snowball' },
:exact => { :type => 'string', :index => 'not_analyzed' , :include_in_all => false }
}
}
}
end
def to_indexed_json
to_json( :include => {
...
:sectors => { :only => ["name"] },
...
})
end
def self.search(params)
tire.search(:load => true, :page=>params[:page], :per_page => 12) do
if params[:query].present?
query { …Run Code Online (Sandbox Code Playgroud) 我正在使用弹性搜索来增强我的应用中的搜索功能.搜索工作正常,但排序不适用于包含多个单词的字段.
当我尝试按日志'消息'对搜索进行排序时,我收到错误:
"无法对每个文档具有多个值的字符串类型进行排序,或者每个字段使用多个标记"
我搜索了错误并发现我可以在:message字段上使用多字段映射(一个分析而另一个不分析)来对它们进行排序.所以我这样做了:
class Log < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
tire.mapping do
indexes :id, index: :not_analyzed
indexes :source, type: 'string'
indexes :level, type: 'string'
indexes :created_at, :type => 'date', :include_in_all => false
indexes :updated_at, :type => 'date', :include_in_all => false
indexes :message, type: 'multi_field', fields: {
analyzed: {type: 'string', index: 'analyzed'},
message: {type: 'string', index: :not_analyzed}
}
indexes :domain, type: 'keyword'
end
end
Run Code Online (Sandbox Code Playgroud)
但是,由于某种原因,没有将此映射传递给ES.
rails console
Log.index.delete #=> true
Log.index.create #=> 200 : {"ok":true,"acknowledged":true}
Log.index.import Log.all #=> 200 …Run Code Online (Sandbox Code Playgroud) 初学者使用Elasticsearch.我觉得这应该很简单,但我被困在这里.我有一个Posts的映射,看起来像这样:
[ post1: {
title: 'asdfasd',
comments: [commment1, comment2, comment3]
},
post2: {
title: 'asdf',
comments: [comment1, comment2]
}
.
.
.]
Run Code Online (Sandbox Code Playgroud)
我正在尝试按标题搜索它们,然后按评论数量排序.我可以通过标题搜索就好了,但是我对如何通过评论计数来排序结果感到有点困惑.这样做最好的方法是什么?
我是弹性搜索的新手.我在我的系统中安装了弹性搜索.在localhost:9200它正在显示
{
"ok" : true,
"status" : 200,
"name" : "Unseen",
"version" : {
"number" : "0.90.10",
"build_hash" : "0a5781f44876e8d1c30b6360628d59cb2a7a2bbb",
"build_timestamp" : "2014-01-10T10:18:37Z",
"build_snapshot" : false,
"lucene_version" : "4.6"
},
"tagline" : "You Know, for Search"
}
Run Code Online (Sandbox Code Playgroud)
我在我的宝石文件中包含了这两个宝石.
gem 'tire'
gem 'elasticsearch'
Run Code Online (Sandbox Code Playgroud)
我有一个电影模型和一个名为search_controller的控制器.
movie.rb
class Movie < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :title
end
def self.search(params)
binding.pry
tire.search(load: true, page: 1, per_page: 10) do
query { string params[:query]} if params[:query].present?
end
Run Code Online (Sandbox Code Playgroud)
迁移电影模型
class CreateMovies < ActiveRecord::Migration
def …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Elasticsearch和Tire来索引一些数据.我希望能够在部分匹配上搜索它,而不仅仅是完整的单词.在下面的示例模型上运行查询时,它只会匹配"notes"字段中完整单词匹配的单词.我无法弄清楚为什么.
class Thingy
include Tire::Model::Search
include Tire::Model::Callbacks
# has some attributes
tire do
settings analysis: {
filter: {
ngram_filter: {
type: 'nGram',
min_gram: 2,
max_gram: 12
}
},
analyzer: {
index_ngram_analyzer: {
type: 'custom',
tokenizer: 'standard',
filter: ['lowercase']
},
search_ngram_analyzer: {
type: 'custom',
tokenizer: 'standard',
filter: ['lowercase', 'ngram_filter']
}
}
} do
mapping do
indexes :notes, :type => "string", boost: 10, index_analyzer: "index_ngram_analyzer", search_analyzer: "search_ngram_analyzer"
end
end
end
def to_indexed_json
{
id: self.id,
account_id: self.account_id,
created_at: self.created_at,
test: self.test,
notes: …Run Code Online (Sandbox Code Playgroud)