[英]Rails and Elasticsearch - Filters, weights, and statistics
考慮以下模型
class MyModel # `Employee` used in concrete examples
field :field1 # company_name used in concrete examples
field :field2 # job_name used in concrete examples
scope: filter1 # non_retired
scope: filter2
end
我把所有這些放在一起有問題。 從Rails彈性搜索 - 命名范圍搜索我明白我需要直接向Elasticsearch提供ID。
def search_by_id(query, type, ids, options = {})
self.weighted_search(query, options.deep_merge({
query: {
multi_match: {
filter: {
ids: {
values: ids
}.tap do |filter|
filter[:type] = type if type
end
}
}
}
}))
end
def weighted_search(query, options = {})
self.__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: [
"company_name^3",
"job_name^2",
],
# strategy: 'leap_frog_filter_first'
# PROBLEM :cannot use this strategy on multi_match ?
}
}
}.deep_merge(options)
)
end
這會產生BadRequest錯誤,並提供以下說明
[400] {“error”:{“root_cause”:[{“type”:“query_parsing_exception”,“reason”:“[match] query不支持[$ oid]
我不明白這個錯誤......我不能用ID過濾?
然后,假設我有這個工作,我如何提取與ElasticSearch匹配的id的子集?
search = MyModel.search('query')
search.total_results # => 81
search.records.count # => 10
但我需要獲得所有81個ID以便我可以執行一些統計(即聚合公司名稱,我已經有一些代碼可以工作,但現在我只得到前10個結果匯總...)
好的,這是我目前的解決方案(我不喜歡,因為它擊中了數據庫很多)
僅使用MongoDB命名作用域來過濾記錄並拔出其ID
ids = @my_model_filtered.pluck(:id)
將生成的ID發送給ES,並使用特定字段的提升進行評分搜索
def search_by_id(query, type, ids, options = {}) self.weighted_search(query, options.deep_merge({ query: { filtered: { filter: { ids: { values: ids.map(&:to_s) }.tap do |filter| filter[:type] = type if type end } } } })) end def weighted_search(query, options = {}) self.__elasticsearch__.search( { query: { filtered: { query: { multi_match: { query: query, fields: [ "company_name^3", "job_name^2", "xxx" ], type: "most_fields" } } } } }.deep_merge(options) ) end
使用Elasticsearch提供的ID列表在MongoDB數據庫上進行其他信息提取(聚合等)
@response = @searching.search_by_id(@query, @searchable_type, ids).per(10000) # setting a high per number so ES returns all IDs @all_records = @response.records.records # Further processing @all_records.map_reduce(...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.