My Rails项目使用Tire gem实现ElasticSearch.我想在名为description的字段中查找具有类似文本的记录.我想根据记录的相似程度(%)来订购结果.
一个类似的tekst的例子:1.我的名字是汤姆,我喜欢汽车.嘿,我是汤姆.我喜欢汽车.汤米喜欢玩车.
请帮我配置模型.我该怎么办?谢谢.
有任何方法可以将新的Elasticsearch宝石与红宝石整合到轨道上,轮胎非常棒,但是从两个月开始退役并被新宝石取代,但是,还没有与轨道集成的功能.
所有教程都使用轮胎,但是现在,我们如何在Elasticsearch中使用rails?
我正在使用active_admin,这会将meta_search引入我的项目.(我不想用于其他任何事情).
它似乎是在我的所有模型上定义搜索方法,这意味着当我包括轮胎时,我不能使用它的搜索方法.
它如何定义方法似乎有些奇怪 - method_defined?说没有定义搜索方法,但是当我调用它时,我得到了meta_search.即使我在类中定义自己的搜索方法,当我调用Document.search时,我仍然会得到meta_search.
编辑:我对处理这类事情的一般方法感兴趣 - 我已经通过使用Model.tire.search解决了这个特殊问题(因为轮胎也可以通过这种方式访问),但我仍然讨厌那个宝石我甚至不使用可以强迫我在我的项目的其余部分使用解决方法.
编辑:我不知道在答案的答案中包含代码块的好方法,所以我会把它放在这里.
# Meta_search loaded, tire is not
1.9.3p125 :001 > require "tire" #=> true
1.9.3p125 :002 > Document.send(:include, Tire::Model::Search)
=> Document(...)
1.9.3p125 :003 > Document.search
Document Load (2.1ms) SELECT "documents".* FROM "documents"
# I get meta_search, as I should
# Tire loaded (and the include Tire::Model::Search is inside the class definition), meta_search is not loaded
1.9.3p125 :001 > Document.search
# I get tire, as I should
1.9.3p125 :002 > require "meta_search" #=> …Run Code Online (Sandbox Code Playgroud) 我不确定如何将我的产品编入索引,因为我认为这就是为什么我会收到此错误:
Tire::Search::SearchRequestFailed in SearchController#results
404 : {"error":"IndexMissingException[[products] missing]","status":404}
Run Code Online (Sandbox Code Playgroud)
这是我的产品型号:
class Product < ActiveRecord::Base
attr_accessible :name, :description
belongs_to :user
include Tire::Model::Search
include Tire::Model::Callbacks
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 10) do
query { string params[:query]} if params[:query].present?
end
end
end
Run Code Online (Sandbox Code Playgroud)
如果出现问题,如何为我的产品编制索引?
谢谢.
我正在使用ElasticSearch和轮胎宝石来支持我网站的搜索功能.我无法弄清楚如何映射和查询数据以获得我需要的结果.
相关代码如下.我将在下面解释所需的内容.
# models/product.rb
class Product < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
has_many :categorizations
has_many :categories, :through => :categorizations
has_many :product_traits
has_many :traits, :through => :product_traits
mapping do
indexes :id, type: 'integer'
indexes :name, boost: 10
indexes :description, analyzer: 'snowball'
indexes :categories do
indexes :id, type: 'integer'
indexes :name, type: 'string', index: 'not_analyzed'
end
indexes :product_traits, type: 'string', index: 'not_analyzed'
end
def self.search(params={})
out = tire.search(page: params[:page], per_page: 12, load: true) do
query do
boolean do
must { string params[:query], …Run Code Online (Sandbox Code Playgroud) 我有一个ElasticSearch模型(由Tire持有,没有ActiveRecord).如果我查询它,我得到一些方面的结果(如预期的那样).
The format is:
class Mention
include Tire::Model::Persistence
index_name 'mentions'
# basic display attributes
property :created_at, :type => 'date'
end
Run Code Online (Sandbox Code Playgroud)
查询返回结果很好.当我检查返回的日期时,我收到一个字符串.
1.9.2p320 :046 > a.results[0].created_at
=> "2012-12-26T02:55:50+01:00"
1.9.2p320 :047 > a.results[0].created_at.class
=> String
1.9.2p320 :048 > a.results[1].created_at
=> "2012-12-26T02:55:50+01:00"
1.9.2p320 :049 > a.results[2].created_at
=> "2012-12-26T02:56:33+01:00"
1.9.2p320 :050 > a.results[2].created_at.class
=> String
Run Code Online (Sandbox Code Playgroud)
我有一个简单的facet返回date_histogram,只是为了检索与查询匹配的日期结果量.格式如下:
facet 'volumes' do
date :created_at
end
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎回归了一个疯狂的Unix时间戳.
1.9.2p320 :069 > d = a.helper.facets["volumes"]["entries"][0]["time"].to_s
=> "1356307200000"
1.9.2p320 :070 > d.class
=> String
1.9.2p320 :071 > Date.strptime(d, "%s")
=> Sun, …Run Code Online (Sandbox Code Playgroud) 如何将多个滤镜应用于刻面(带轮胎宝石)?我有一个代码:
facet "packages" do
terms :package
facet_filter :terms, producer: [*params[:producer]] if params[:producer].present?
facet_filter :terms, category_id: [*params[:category_id]] if params[:category_id].present?
end
Run Code Online (Sandbox Code Playgroud)
但它似乎只适用于最后一个过滤器.但我需要他们两个.
我已经查看了Tire文档,我知道可以使用Tire创建索引并将对象导入Elasticsearch,而无需使用ActiveModel包含.
我想保持我的模型文件完全清除任何轮胎映射,而不是单独实现它们.
到目前为止,我已经为我的Member模型创建了一个索引对象,如下所示:
class MemberIndexer
def initialize(member)
@member = member
end
def attributes
{
id: @member.id,
name: @member.full_name,
age: @member.age,
birthday: @member.birthday,
type: 'member'
}.to_json
end
def self.refresh_index
Tire.index 'members' do
delete
create mappings: {
member: {
properties: {
id: { type: 'integer' },
name: { type: 'string', boost: 5 },
age: { type: 'integer' },
birthday: { type: 'date' }
}
}
}
Member.find_in_batches do |members|
import members.map { |m| MemberIndexer.new(m).attributes }
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
这工作正常,索引已创建,我可以正常搜索.我缺少的是可以通过使用包含的回调Tire::Model::Callbacks.是否可以使用轮胎更新索引中的单个记录而无需包含 …
如何在不删除索引映射的情况下从 Elasticsearch 数据库中删除数据?
我是 Tyre gem,使用 delete 命令删除我的所有映射并再次运行 create 命令。我想避免一次又一次地运行 create 命令。
这个你能帮我吗。
我有 2 个型号,Player并且Item. Aplayer有很多items.
模型的映射Item:
tire do
mapping(
_parent: { type: 'player' },
_routing: { required: true, path: :user }
) do
indexes :user, type: "string", index: :not_analyzed
end
Run Code Online (Sandbox Code Playgroud)
(不确定该path选项是什么,除了它可能用于将项目存储在不同的分片上?
我不明白的是如何player在保存item. 在我在网上找到的所有父/子关系示例中,他们只是使用 CURL:
curl -XPOST localhost:9200/authors/book/1?parent=2 -d '{
"name": "Revelation Space",
"genre": "scifi",
"publisher": "penguin"
}'
Run Code Online (Sandbox Code Playgroud)