![](/img/trans.png)
[英]How to remove _source from search results in Tire gem for elasticsearch
[英]How to perform ElasticSearch query on records from only a certain user (Rails Tire gem)
我有一個Mongoid
模型,我執行ElasticSearch
搜索查詢。 非常標准:
class ActivityLog
include Mongoid::Document
include Mongoid::Timestamps
include Tire::Model::Search
include Tire::Model::Callbacks
field :extra, type: Hash
belongs_to :user, :inverse_of => :activity_logs
def self.search(params, user)
tire.search(load: true, page: params[:page], per_page: 5) do
query { string params[:query], default_operator: "AND" } if params[:query].present?
sort { by :created_at, "desc" }
end
end
我很難理解有關如何更高級的東西的文檔 ,目前我陷入了如何處理我的搜索查詢,搜索應限制為僅屬於用戶的ActivityLog
對象。
有誰能告訴我如何將user._id匹配要求用於搜索查詢?
使用ElasticSearch,有兩個部分:映射和搜索
所以,如果你想通過一個關聯表(用戶表)來搜索,那就對了。 有很多方法,但這個我經常用於我的項目:
在ActivityLog
模型中
mapping do
indexes :id, type: 'integer'
indexes :name, type: 'string', analyzer: 'snowball', boost: 5
indexes :description, type: 'string', analyzer: 'snowball'
indexes :description_latin, as: 'description.sanitize', type: 'string', analyzer: 'snowball'
indexes :user_id, as: 'user.id', type: 'string', index: :not_analyzed
indexes :user_name, as: 'user.name', type: 'string'
indexes :created_at, type: 'date'
indexes :slug, index: :not_analyzed
indexes :publish, type: 'boolean', index: :not_analyzed
end
通知user_id
和user_name
,上面的映射方法定義將user.id
映射到user_id
,將user.name
映射到user_name
。
所以現在在搜索方法中,你可以做一些類似的代碼,比如filter :terms, user_id: params[:search][:user_id]
希望這有幫助
對上述內容進行了一處更正,ElasticSearch刪除了類型字符串,現在使用了文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.