我有几个使用elasticsearch索引的模型,其中一个模型有一个需要过滤的关联,但如果我在search方法中添加这样的东西
search.filter :range, 'sessions.starts_on' => {:gte => start_date,
:lte => ends_on}
Run Code Online (Sandbox Code Playgroud)
然后过滤掉所有模型,因为它们没有这种关联sessions.
那么有没有办法将过滤器应用于一个模型?
任何想法都非常感谢!
我正在使用Elasticsearch来获取ID到某些值的映射,但是至关重要的是,保持ID顺序的结果顺序。
例:
def term_mapping(ids)
ids = ids.split(',')
self.search do |s|
s.filter :terms, id: ids
end
end
res = term_mapping("4,2,3,1")
Run Code Online (Sandbox Code Playgroud)
结果集合应包含ID顺序为4,2,3,1的对象...
你有什么想法我能做到吗?
我希望查询返回一个计算得分,如:
标题中每个查询词的出现+描述/查询词的数量
例如
EbSearch.add [
new_job( id: 1, title: "Java Programmierer",
description: "Java Programmierer")
]
res = EbSearch.search("Java Programmierer").results.first.score.should == 4
Run Code Online (Sandbox Code Playgroud)
目前它输出8,因为它对每个术语进行查询并总结它.之后我可以分开,但我没有分析的查询条款,因此化合物可能会弄乱分数.
查询结构如下:
search = Tire.search index_name do
query do
dis_max do
query { string query, fields: ['title^3', 'description.with_synonyms^0.5'], use_dis_max: false, default_operator: "OR" }
query { string query, fields: ['title^3', 'description.without_synonyms'], use_dis_max: false, default_operator: "OR"}
end
end
end
Run Code Online (Sandbox Code Playgroud)
任何想法如何解决这个问题非常感谢.
编辑
我意识到我提供的背景不够.
以下是我已经制定的其他一些片段.我写了一个自定义的SimilarityProvider来禁用idf和规范化.https://gist.github.com/outsmartin/6114175
完整的轮胎代码可在https://gist.github.com/6114186找到.它比示例稍微复杂一点,但它应该是可以理解的.
现在我有以下代码:
search = Tire.search ['object1_index', 'object2_index'] do |search|
search.query { string params[:q] }
end
@results = search.results
Run Code Online (Sandbox Code Playgroud)
现在的结果都来自object1,但是,如果我使用相同的查询删除'object1_index',我会得到object2结果.如何让我的搜索同时与两个索引进行交互?
编辑:
我现在有以下(使用我在模型上定义的搜索),但由于我最终将添加分页,我不确定这是否是最好的解决方法:
object1_results = Object1.search(params).results
object2_results = Object2.search(params).results
@results = object1_results + object2_results
@results.sort!(&:_score)
Run Code Online (Sandbox Code Playgroud) 我最近一直在尝试使用ruby on rails.我无法将数据编入索引,因此我可以搜索包含复数和非复数关键字的项目.
轮胎将允许我为每个映射属性分配一个分析器:
mapping do
indexes title, analyzer: 'snowball'
indexes body, analyzer: 'snowball'
end
Run Code Online (Sandbox Code Playgroud)
现在,假设我在"测试"标题中有一个关键字
如果我使用查询中的属性进行搜索: http:// localhost:9200/myindex/mymapping/_search?q = title:test 它将起作用.
但是,如果我进行常规搜索而不指定如下属性:
http:// localhost:9200/myindex/mymapping/_search?q = test
它找不到该文件.
如何指定我希望默认分析器为"雪球",因此我不必指定要搜索的属性?
ps我正在使用轮胎宝石.所以请尽可能地回答这个问题.
我正在使用Tire进行弹性搜索.在我的应用程序中,我有2个模型; 价格和产品.
我正在尝试搜索我的Price类并使用它所属的产品属于:name搜索字段的属性.现在,如果我有一个名为Product 1"pro","prod"或"duct"的产品,则不会出现任何结果.但输入"product"或"Product"会显示结果.我相信问题在于我的映射.我查看了查询及其:
...localhost:3000/search/results?utf8=%E2%9C%93&query=product
Run Code Online (Sandbox Code Playgroud)
当我认为它应该是:
...localhost:3000/search/results?utf8=%E2%9C%93&query:product=product
Run Code Online (Sandbox Code Playgroud)
从这个问题判断:ElasticSearch映射不起作用
我不知道如何让我params[:query]采取product.name唯一的.我试图使用:string params[:query], default_field: "product.name"但这没用.
我不想使用该_all字段.
这是我的代码:
Price.rb
include Tire::Model::Search
include Tire::Model::Callbacks
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 20) do
query do
boolean do
must { string params[:query] } if params[:query].present?
must { term :private, false }
end
end
sort do
by :date, "desc"
by :amount, "asc"
end
end
end
def to_indexed_json
to_json( include: { product: { …Run Code Online (Sandbox Code Playgroud) 我在 EC2 单服务器设置上使用 Rails、Tire 和 Elasticsearch,没有分片或复制(这是 Jenkins CI 服务器)。使用自定义初始值设定项:
analysis:
filter:
name_synonyms:
type: synonym
synonyms_path: <%= Rails.root.join("config", "synonyms", "name_synonyms.txt") %>
Run Code Online (Sandbox Code Playgroud)
该文件通过 Erubis 运行,同义词路径转换为如下所示:
/root/workspace/project-project-0f317744a1870b4baf61bbaeb390ebe1/config/synonyms/term_synonyms.txt
Run Code Online (Sandbox Code Playgroud)
当我列出服务器中的文件时,我看到以下内容:
root@ip-XX-XXX-XX-XXX:~/workspace/project-project-0f317744a1870b4baf61bbaeb390ebe1/config/synonyms# ls -la
total 20
drwxr-xr-x 2 root root 4096 Feb 11 18:25 .
drwxr-xr-x 7 root root 4096 Feb 11 18:25 ..
-rw-r--r-- 1 root root 3117 Feb 11 18:25 location_synonyms.txt
-rw-r--r-- 1 root root 3999 Feb 11 18:25 name_synonyms.txt
-rw-r--r-- 1 root root 2144 Feb 11 18:25 term_synonyms.txt
Run Code Online (Sandbox Code Playgroud)
这正是我所期待的,但是我在运行时看到以下错误 rake spec
500 : …Run Code Online (Sandbox Code Playgroud) Item我的Rails应用程序中有一个可搜索的模型.
我想索引ElasticSearch中的大多数,除了满足某些条件的那些.
我怎样才能做到这一点?
update_index在模型中重写可接受的解决方案?
我知道我也可以将它们全部索引,并在搜索中过滤掉它们.但它们永远不会被搜索,所以我根本没有看到索引它们的理由.
我正在尝试在建立has_many, :through关联时索引模型,但是没有显示任何结果.
class Business < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
def self.search(params)
tire.search(load: true) do
query { string params[:q]} if params[:q].present?
end
end
mapping do
indexes :service_name
indexes :service_description
indexes :latitude
indexes :longitude
indexes :services do
indexes :service
indexes :description
end
end
def to_indexed_json #returns json data that should index (the model that should be searched)
to_json(methods: [:service_name, :service_description], include: { services: [:service, :description]})
end
def service_name
services.map(&:service)
end
def service_description
services.map(&:description)
end
has_many :professionals
has_many :services, :through …Run Code Online (Sandbox Code Playgroud) ruby-on-rails associations elasticsearch tire ruby-on-rails-3.2
我在 Heroku 上部署了 Ruby On Rails 应用程序,我使用 ElasticSearch 插件,我的弹性搜索集群工作正常,但最近 ElasticSearch 插件要求所有集群强制执行身份验证,以保护托管集群内部的数据,现在,对于我们尝试提出的每个请求,我们都会收到以下异常:
Tire::Search::SearchRequestFailed (401 : {"error":{"root_cause":[{"type":"security_exception","reason":"action [indices:data/read/search] requires authentication","header":{"WWW-Authenticate":"Basic realm=\"shield\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"action [indices:data/read/search] requires authentication","header":{"WWW-Authenticate":"Basic realm=\"shield\" charset=\"UTF-8\""}},"status":401}):
Run Code Online (Sandbox Code Playgroud)
我们从Elasticsearch 附带的Shield插件配置了一些用户,但是我不确定如何从我的 ruby On Rails 应用程序输入此身份验证,我使用 Tire 在我的 Ruby On Rails 应用程序中集成弹性搜索,但我找不到我可以输入这些信息来验证我的应用程序并解决此问题吗?
我们想知道如何使用这两个 gem 进行身份验证: ElasticSearch-rails和Tire