Implemented issue #1951 - Allow pagination in search result with more then 100 results.
This commit is contained in:
parent
9791c6b72c
commit
cf16264940
8 changed files with 66 additions and 48 deletions
|
@ -229,14 +229,14 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co
|
||||||
raise Exceptions::NotAuthorized
|
raise Exceptions::NotAuthorized
|
||||||
end
|
end
|
||||||
|
|
||||||
# set limit for pagination if needed
|
per_page = params[:per_page] || params[:limit] || 100
|
||||||
if params[:page] && params[:per_page]
|
per_page = per_page.to_i
|
||||||
params[:limit] = params[:page].to_i * params[:per_page].to_i
|
if per_page > 500
|
||||||
end
|
per_page = 500
|
||||||
|
|
||||||
if params[:limit] && params[:limit].to_i > 500
|
|
||||||
params[:limit] = 500
|
|
||||||
end
|
end
|
||||||
|
page = params[:page] || 1
|
||||||
|
page = page.to_i
|
||||||
|
offset = (page - 1) * per_page
|
||||||
|
|
||||||
query = params[:query]
|
query = params[:query]
|
||||||
if query.respond_to?(:permit!)
|
if query.respond_to?(:permit!)
|
||||||
|
@ -244,7 +244,8 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co
|
||||||
end
|
end
|
||||||
query_params = {
|
query_params = {
|
||||||
query: query,
|
query: query,
|
||||||
limit: params[:limit],
|
limit: per_page,
|
||||||
|
offset: offset,
|
||||||
current_user: current_user,
|
current_user: current_user,
|
||||||
}
|
}
|
||||||
if params[:role_ids].present?
|
if params[:role_ids].present?
|
||||||
|
@ -254,12 +255,6 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co
|
||||||
# do query
|
# do query
|
||||||
organization_all = Organization.search(query_params)
|
organization_all = Organization.search(query_params)
|
||||||
|
|
||||||
# do pagination if needed
|
|
||||||
if params[:page] && params[:per_page]
|
|
||||||
offset = (params[:page].to_i - 1) * params[:per_page].to_i
|
|
||||||
organization_all = organization_all[offset, params[:per_page].to_i] || []
|
|
||||||
end
|
|
||||||
|
|
||||||
if response_expand?
|
if response_expand?
|
||||||
list = []
|
list = []
|
||||||
organization_all.each do |organization|
|
organization_all.each do |organization|
|
||||||
|
|
|
@ -425,14 +425,14 @@ class TicketsController < ApplicationController
|
||||||
params.require(:condition).permit!
|
params.require(:condition).permit!
|
||||||
end
|
end
|
||||||
|
|
||||||
# set limit for pagination if needed
|
per_page = params[:per_page] || params[:limit] || 50
|
||||||
if params[:page] && params[:per_page]
|
per_page = per_page.to_i
|
||||||
params[:limit] = params[:page].to_i * params[:per_page].to_i
|
if per_page > 200
|
||||||
end
|
per_page = 200
|
||||||
|
|
||||||
if params[:limit] && params[:limit].to_i > 100
|
|
||||||
params[:limit] = 100
|
|
||||||
end
|
end
|
||||||
|
page = params[:page] || 1
|
||||||
|
page = page.to_i
|
||||||
|
offset = (page - 1) * per_page
|
||||||
|
|
||||||
query = params[:query]
|
query = params[:query]
|
||||||
if query.respond_to?(:permit!)
|
if query.respond_to?(:permit!)
|
||||||
|
@ -443,16 +443,11 @@ class TicketsController < ApplicationController
|
||||||
tickets = Ticket.search(
|
tickets = Ticket.search(
|
||||||
query: query,
|
query: query,
|
||||||
condition: params[:condition].to_h,
|
condition: params[:condition].to_h,
|
||||||
limit: params[:limit],
|
limit: per_page,
|
||||||
|
offset: offset,
|
||||||
current_user: current_user,
|
current_user: current_user,
|
||||||
)
|
)
|
||||||
|
|
||||||
# do pagination if needed
|
|
||||||
if params[:page] && params[:per_page]
|
|
||||||
offset = (params[:page].to_i - 1) * params[:per_page].to_i
|
|
||||||
tickets = tickets[offset, params[:per_page].to_i] || []
|
|
||||||
end
|
|
||||||
|
|
||||||
if response_expand?
|
if response_expand?
|
||||||
list = []
|
list = []
|
||||||
tickets.each do |ticket|
|
tickets.each do |ticket|
|
||||||
|
|
|
@ -373,14 +373,28 @@ class UsersController < ApplicationController
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
# set limit for pagination if needed
|
per_page = params[:per_page] || params[:limit] || 100
|
||||||
if params[:page] && params[:per_page]
|
per_page = per_page.to_i
|
||||||
params[:limit] = params[:page].to_i * params[:per_page].to_i
|
if per_page > 500
|
||||||
|
per_page = 500
|
||||||
|
end
|
||||||
|
page = params[:page] || 1
|
||||||
|
page = page.to_i
|
||||||
|
offset = (page - 1) * per_page
|
||||||
|
|
||||||
|
query = params[:query]
|
||||||
|
if query.respond_to?(:permit!)
|
||||||
|
query = query.permit!.to_h
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:limit] && params[:limit].to_i > 500
|
# build result list
|
||||||
params[:limit] = 500
|
tickets = Ticket.search(
|
||||||
end
|
query: query,
|
||||||
|
condition: params[:condition].to_h,
|
||||||
|
limit: per_page,
|
||||||
|
offset: offset,
|
||||||
|
current_user: current_user,
|
||||||
|
)
|
||||||
|
|
||||||
query = params[:query] || params[:term]
|
query = params[:query] || params[:term]
|
||||||
if query.respond_to?(:permit!)
|
if query.respond_to?(:permit!)
|
||||||
|
@ -389,7 +403,8 @@ class UsersController < ApplicationController
|
||||||
|
|
||||||
query_params = {
|
query_params = {
|
||||||
query: query,
|
query: query,
|
||||||
limit: params[:limit],
|
limit: per_page,
|
||||||
|
offset: offset,
|
||||||
current_user: current_user,
|
current_user: current_user,
|
||||||
}
|
}
|
||||||
%i[role_ids permissions].each do |key|
|
%i[role_ids permissions].each do |key|
|
||||||
|
|
|
@ -38,6 +38,7 @@ search organizations
|
||||||
current_user: User.find(123),
|
current_user: User.find(123),
|
||||||
query: 'search something',
|
query: 'search something',
|
||||||
limit: 15,
|
limit: 15,
|
||||||
|
offset: 100,
|
||||||
)
|
)
|
||||||
|
|
||||||
returns
|
returns
|
||||||
|
@ -51,6 +52,7 @@ returns
|
||||||
# get params
|
# get params
|
||||||
query = params[:query]
|
query = params[:query]
|
||||||
limit = params[:limit] || 10
|
limit = params[:limit] || 10
|
||||||
|
offset = params[:offset] || 0
|
||||||
current_user = params[:current_user]
|
current_user = params[:current_user]
|
||||||
|
|
||||||
# enable search only for agents and admins
|
# enable search only for agents and admins
|
||||||
|
@ -58,7 +60,7 @@ returns
|
||||||
|
|
||||||
# try search index backend
|
# try search index backend
|
||||||
if SearchIndexBackend.enabled?
|
if SearchIndexBackend.enabled?
|
||||||
items = SearchIndexBackend.search(query, limit, 'Chat::Session')
|
items = SearchIndexBackend.search(query, limit, 'Chat::Session', {}, offset)
|
||||||
chat_sessions = []
|
chat_sessions = []
|
||||||
items.each do |item|
|
items.each do |item|
|
||||||
chat_session = Chat::Session.lookup(id: item[:id])
|
chat_session = Chat::Session.lookup(id: item[:id])
|
||||||
|
@ -73,7 +75,7 @@ returns
|
||||||
query.delete! '*'
|
query.delete! '*'
|
||||||
chat_sessions = Chat::Session.where(
|
chat_sessions = Chat::Session.where(
|
||||||
'name LIKE ?', "%#{query}%"
|
'name LIKE ?', "%#{query}%"
|
||||||
).order('name').limit(limit).to_a
|
).order('name').offset(offset).limit(limit).to_a
|
||||||
chat_sessions
|
chat_sessions
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,6 +38,7 @@ search organizations
|
||||||
current_user: User.find(123),
|
current_user: User.find(123),
|
||||||
query: 'search something',
|
query: 'search something',
|
||||||
limit: 15,
|
limit: 15,
|
||||||
|
offset: 100,
|
||||||
)
|
)
|
||||||
|
|
||||||
returns
|
returns
|
||||||
|
@ -51,6 +52,7 @@ returns
|
||||||
# get params
|
# get params
|
||||||
query = params[:query]
|
query = params[:query]
|
||||||
limit = params[:limit] || 10
|
limit = params[:limit] || 10
|
||||||
|
offset = params[:offset] || 0
|
||||||
current_user = params[:current_user]
|
current_user = params[:current_user]
|
||||||
|
|
||||||
# enable search only for agents and admins
|
# enable search only for agents and admins
|
||||||
|
@ -58,7 +60,7 @@ returns
|
||||||
|
|
||||||
# try search index backend
|
# try search index backend
|
||||||
if SearchIndexBackend.enabled?
|
if SearchIndexBackend.enabled?
|
||||||
items = SearchIndexBackend.search(query, limit, 'Organization')
|
items = SearchIndexBackend.search(query, limit, 'Organization', {}, offset)
|
||||||
organizations = []
|
organizations = []
|
||||||
items.each do |item|
|
items.each do |item|
|
||||||
organization = Organization.lookup(id: item[:id])
|
organization = Organization.lookup(id: item[:id])
|
||||||
|
@ -73,7 +75,7 @@ returns
|
||||||
query.delete! '*'
|
query.delete! '*'
|
||||||
organizations = Organization.where(
|
organizations = Organization.where(
|
||||||
'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"
|
'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"
|
||||||
).order('name').limit(limit).to_a
|
).order('name').offset(offset).limit(limit).to_a
|
||||||
|
|
||||||
# if only a few organizations are found, search for names of users
|
# if only a few organizations are found, search for names of users
|
||||||
if organizations.length <= 3
|
if organizations.length <= 3
|
||||||
|
|
|
@ -35,6 +35,7 @@ search tickets via search index
|
||||||
current_user: User.find(123),
|
current_user: User.find(123),
|
||||||
query: 'search something',
|
query: 'search something',
|
||||||
limit: 15,
|
limit: 15,
|
||||||
|
offset: 100,
|
||||||
)
|
)
|
||||||
|
|
||||||
returns
|
returns
|
||||||
|
@ -47,6 +48,7 @@ search tickets via search index
|
||||||
current_user: User.find(123),
|
current_user: User.find(123),
|
||||||
query: 'search something',
|
query: 'search something',
|
||||||
limit: 15,
|
limit: 15,
|
||||||
|
offset: 100,
|
||||||
full: false,
|
full: false,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -77,6 +79,7 @@ search tickets via database
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
limit: 15,
|
limit: 15,
|
||||||
|
offset: 100,
|
||||||
full: false,
|
full: false,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -92,6 +95,7 @@ returns
|
||||||
query = params[:query]
|
query = params[:query]
|
||||||
condition = params[:condition]
|
condition = params[:condition]
|
||||||
limit = params[:limit] || 12
|
limit = params[:limit] || 12
|
||||||
|
offset = params[:offset] || 0
|
||||||
current_user = params[:current_user]
|
current_user = params[:current_user]
|
||||||
full = false
|
full = false
|
||||||
if params[:full] == true || params[:full] == 'true' || !params.key?(:full)
|
if params[:full] == true || params[:full] == 'true' || !params.key?(:full)
|
||||||
|
@ -127,7 +131,7 @@ returns
|
||||||
|
|
||||||
query_extention['bool']['must'].push access_condition
|
query_extention['bool']['must'].push access_condition
|
||||||
|
|
||||||
items = SearchIndexBackend.search(query, limit, 'Ticket', query_extention)
|
items = SearchIndexBackend.search(query, limit, 'Ticket', query_extention, offset)
|
||||||
if !full
|
if !full
|
||||||
ids = []
|
ids = []
|
||||||
items.each do |item|
|
items.each do |item|
|
||||||
|
@ -156,6 +160,7 @@ returns
|
||||||
.where('(tickets.title LIKE ? OR tickets.number LIKE ? OR ticket_articles.body LIKE ? OR ticket_articles.from LIKE ? OR ticket_articles.to LIKE ? OR ticket_articles.subject LIKE ?)', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%" )
|
.where('(tickets.title LIKE ? OR tickets.number LIKE ? OR ticket_articles.body LIKE ? OR ticket_articles.from LIKE ? OR ticket_articles.to LIKE ? OR ticket_articles.subject LIKE ?)', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%" )
|
||||||
.joins(:articles)
|
.joins(:articles)
|
||||||
.order('tickets.created_at DESC')
|
.order('tickets.created_at DESC')
|
||||||
|
.offset(offset)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
else
|
else
|
||||||
query_condition, bind_condition, tables = selector2sql(condition)
|
query_condition, bind_condition, tables = selector2sql(condition)
|
||||||
|
@ -164,6 +169,7 @@ returns
|
||||||
.where(access_condition)
|
.where(access_condition)
|
||||||
.where(query_condition, *bind_condition)
|
.where(query_condition, *bind_condition)
|
||||||
.order('tickets.created_at DESC')
|
.order('tickets.created_at DESC')
|
||||||
|
.offset(offset)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,7 @@ search user
|
||||||
result = User.search(
|
result = User.search(
|
||||||
query: 'some search term',
|
query: 'some search term',
|
||||||
limit: 15,
|
limit: 15,
|
||||||
|
offset: 100,
|
||||||
current_user: user_model,
|
current_user: user_model,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -45,6 +46,7 @@ or with certain role_ids | permissions
|
||||||
result = User.search(
|
result = User.search(
|
||||||
query: 'some search term',
|
query: 'some search term',
|
||||||
limit: 15,
|
limit: 15,
|
||||||
|
offset: 100,
|
||||||
current_user: user_model,
|
current_user: user_model,
|
||||||
role_ids: [1,2,3],
|
role_ids: [1,2,3],
|
||||||
permissions: ['ticket.agent']
|
permissions: ['ticket.agent']
|
||||||
|
@ -61,6 +63,7 @@ returns
|
||||||
# get params
|
# get params
|
||||||
query = params[:query]
|
query = params[:query]
|
||||||
limit = params[:limit] || 10
|
limit = params[:limit] || 10
|
||||||
|
offset = params[:offset] || 0
|
||||||
current_user = params[:current_user]
|
current_user = params[:current_user]
|
||||||
|
|
||||||
# enable search only for agents and admins
|
# enable search only for agents and admins
|
||||||
|
@ -87,7 +90,7 @@ returns
|
||||||
}
|
}
|
||||||
query_extention['bool']['must'].push access_condition
|
query_extention['bool']['must'].push access_condition
|
||||||
end
|
end
|
||||||
items = SearchIndexBackend.search(query, limit, 'User', query_extention)
|
items = SearchIndexBackend.search(query, limit, 'User', query_extention, offset)
|
||||||
users = []
|
users = []
|
||||||
items.each do |item|
|
items.each do |item|
|
||||||
user = User.lookup(id: item[:id])
|
user = User.lookup(id: item[:id])
|
||||||
|
@ -103,11 +106,11 @@ returns
|
||||||
users = if params[:role_ids]
|
users = if params[:role_ids]
|
||||||
User.joins(:roles).where('roles.id' => params[:role_ids]).where(
|
User.joins(:roles).where('roles.id' => params[:role_ids]).where(
|
||||||
'(users.firstname LIKE ? OR users.lastname LIKE ? OR users.email LIKE ? OR users.login LIKE ?) AND users.id != 1', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%"
|
'(users.firstname LIKE ? OR users.lastname LIKE ? OR users.email LIKE ? OR users.login LIKE ?) AND users.id != 1', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%"
|
||||||
).order('updated_at DESC').limit(limit)
|
).order('updated_at DESC').offset(offset).limit(limit)
|
||||||
else
|
else
|
||||||
User.where(
|
User.where(
|
||||||
'(firstname LIKE ? OR lastname LIKE ? OR email LIKE ? OR login LIKE ?) AND id != 1', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%"
|
'(firstname LIKE ? OR lastname LIKE ? OR email LIKE ? OR login LIKE ?) AND id != 1', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%"
|
||||||
).order('updated_at DESC').limit(limit)
|
).order('updated_at DESC').offset(offset).limit(limit)
|
||||||
end
|
end
|
||||||
users
|
users
|
||||||
end
|
end
|
||||||
|
|
|
@ -288,20 +288,20 @@ return search result
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def self.search(query, limit = 10, index = nil, query_extention = {})
|
def self.search(query, limit = 10, index = nil, query_extention = {}, from = 0)
|
||||||
return [] if query.blank?
|
return [] if query.blank?
|
||||||
if index.class == Array
|
if index.class == Array
|
||||||
ids = []
|
ids = []
|
||||||
index.each do |local_index|
|
index.each do |local_index|
|
||||||
local_ids = search_by_index(query, limit, local_index, query_extention)
|
local_ids = search_by_index(query, limit, local_index, query_extention, from)
|
||||||
ids = ids.concat(local_ids)
|
ids = ids.concat(local_ids)
|
||||||
end
|
end
|
||||||
return ids
|
return ids
|
||||||
end
|
end
|
||||||
search_by_index(query, limit, index, query_extention)
|
search_by_index(query, limit, index, query_extention, from)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.search_by_index(query, limit = 10, index = nil, query_extention = {})
|
def self.search_by_index(query, limit = 10, index = nil, query_extention = {}, from)
|
||||||
return [] if query.blank?
|
return [] if query.blank?
|
||||||
|
|
||||||
url = build_url
|
url = build_url
|
||||||
|
@ -316,7 +316,7 @@ return search result
|
||||||
'/_search'
|
'/_search'
|
||||||
end
|
end
|
||||||
data = {}
|
data = {}
|
||||||
data['from'] = 0
|
data['from'] = from
|
||||||
data['size'] = limit
|
data['size'] = limit
|
||||||
data['sort'] =
|
data['sort'] =
|
||||||
[
|
[
|
||||||
|
|
Loading…
Reference in a new issue