Use elasticsearch for post history

This commit is contained in:
Kira 2019-09-13 19:50:26 -07:00
parent ed89eef80d
commit 4da805a4fa
3 changed files with 25 additions and 8 deletions

View File

@ -3,7 +3,7 @@ class PostVersionsController < ApplicationController
respond_to :html, :xml, :json
def index
@post_versions = PostArchive.includes(:updater, post: [:versions]).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@post_versions = PostArchive.search(PostArchive.build_query(search_params)).paginate(params[:page], :limit => params[:limit], :search_count => params[:search], includes: [:updater, post: [:versions]])
respond_with(@post_versions) do |format|
format.xml do
render :xml => @post_versions.to_xml(:root => "post-versions")

View File

@ -5,6 +5,7 @@ module PostVersionIndex
base.settings index: {number_of_shards: 8, number_of_replicas: 1} do
mappings dynamic: false do
indexes :id, type: 'integer'
indexes :post_id, type: 'integer'
indexes :version, type: 'integer'
indexes :updater_id, type: 'integer'
indexes :parent_id, type: 'integer'
@ -61,6 +62,7 @@ module PostVersionIndex
def as_indexed_json(options = {})
{
id: id,
post_id: post_id,
updated_at: updated_at,
version: version,
updater_id: updater_id,

View File

@ -31,26 +31,41 @@ class PostArchive < ApplicationRecord
for_user(user_id)
end
def search(params)
q = super
def build_query(params)
must = []
def should(*args)
{bool: {should: args}}
end
def split_to_terms(field, input)
input.split(',').map(&:to_i).map {|x| {term: {field => x}}}
end
if params[:updater_name].present?
q = q.for_user_name(params[:updater_name])
must << {term: {updater_id: User.name_to_id(params[:updater_name])}}
end
if params[:updater_id].present?
q = q.where(updater_id: params[:updater_id].split(",").map(&:to_i))
must << should(*split_to_terms(:updater_id, params[:updater_id]))
end
if params[:post_id].present?
q = q.where(post_id: params[:post_id].split(",").map(&:to_i))
must << should(*split_to_terms(:post_id, params[:post_id]))
end
if params[:start_id].present?
q = q.where("id <= ?", params[:start_id].to_i)
must << {range: {id: {gte: params[:start_id].to_i}}}
end
q.apply_default_order(params)
if must.empty?
must.push({match_all: {}})
end
{
query: {bool: {must: must}},
sort: {id: :desc},
_source: false,
timeout: "#{CurrentUser.user.try(:statement_timeout) || 3_000}ms"
}
end
end