Improved ticket search.

This commit is contained in:
Martin Edenhofer 2016-09-11 10:46:01 +02:00
parent 2bb247f6e9
commit c28c8514c4
4 changed files with 86 additions and 30 deletions

View file

@ -393,7 +393,9 @@ class TicketsController < ApplicationController
def search def search
# permit nested conditions # permit nested conditions
if params[:condition]
params.require(:condition).permit! params.require(:condition).permit!
end
# build result list # build result list
tickets = Ticket.search( tickets = Ticket.search(

View file

@ -421,6 +421,10 @@ generate condition query to search for tickets based on condition
condition example condition example
{ {
'ticket.title' => {
operator: 'contains', # contains not
value: 'some value',
},
'ticket.state_id' => { 'ticket.state_id' => {
operator: 'is', operator: 'is',
value: [1,2,5] value: [1,2,5]

View file

@ -58,6 +58,7 @@ search tickets via database
result = Ticket.search( result = Ticket.search(
current_user: User.find(123), current_user: User.find(123),
query: 'some query', # query or condition is required
condition: { condition: {
'tickets.owner_id' => { 'tickets.owner_id' => {
operator: 'is', operator: 'is',
@ -89,6 +90,7 @@ returns
# get params # get params
query = params[:query] query = params[:query]
condition = params[:condition]
limit = params[:limit] || 12 limit = params[:limit] || 12
current_user = params[:current_user] current_user = params[:current_user]
full = false full = false
@ -97,7 +99,7 @@ returns
end end
# try search index backend # try search index backend
if !params[:condition] && SearchIndexBackend.enabled? if !condition && SearchIndexBackend.enabled?
query_extention = {} query_extention = {}
query_extention['bool'] = {} query_extention['bool'] = {}
query_extention['bool']['must'] = [] query_extention['bool']['must'] = []
@ -160,7 +162,7 @@ returns
.order('tickets.created_at DESC') .order('tickets.created_at DESC')
.limit(limit) .limit(limit)
else else
query_condition, bind_condition = selector2sql(params[:condition]) query_condition, bind_condition = selector2sql(condition)
tickets_all = Ticket.select('DISTINCT(tickets.id), tickets.created_at') tickets_all = Ticket.select('DISTINCT(tickets.id), tickets.created_at')
.where(access_condition) .where(access_condition)
.where(query_condition, *bind_condition) .where(query_condition, *bind_condition)

View file

@ -225,8 +225,9 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
end end
test '02.04 ticket with correct ticket id' do test '02.04 ticket with correct ticket id' do
title = "ticket with corret ticket id testagent#{rand(999_999_999)}"
ticket = Ticket.create!( ticket = Ticket.create!(
title: 'ticket with corret ticket id', title: title,
group: Group.lookup(name: 'Users'), group: Group.lookup(name: 'Users'),
customer_id: @customer_without_org.id, customer_id: @customer_without_org.id,
state: Ticket::State.lookup(name: 'new'), state: Ticket::State.lookup(name: 'new'),
@ -240,13 +241,13 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(Hash, result.class) assert_equal(Hash, result.class)
assert_equal(ticket.id, result['id']) assert_equal(ticket.id, result['id'])
assert_equal('ticket with corret ticket id', result['title']) assert_equal(title, result['title'])
assert_equal(ticket.customer_id, result['customer_id']) assert_equal(ticket.customer_id, result['customer_id'])
assert_equal(1, result['updated_by_id']) assert_equal(1, result['updated_by_id'])
assert_equal(1, result['created_by_id']) assert_equal(1, result['created_by_id'])
params = { params = {
title: 'ticket with corret ticket id - 2', title: "#{title} - 2",
customer_id: @agent.id, customer_id: @agent.id,
} }
put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials) put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
@ -254,7 +255,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(Hash, result.class) assert_equal(Hash, result.class)
assert_equal(ticket.id, result['id']) assert_equal(ticket.id, result['id'])
assert_equal('ticket with corret ticket id - 2', result['title']) assert_equal("#{title} - 2", result['title'])
assert_equal(@agent.id, result['customer_id']) assert_equal(@agent.id, result['customer_id'])
assert_equal(@agent.id, result['updated_by_id']) assert_equal(@agent.id, result['updated_by_id'])
assert_equal(1, result['created_by_id']) assert_equal(1, result['created_by_id'])
@ -266,19 +267,42 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
} }
post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials) post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
assert_response(201) assert_response(201)
article_result = JSON.parse(@response.body)
assert_equal(Hash, article_result.class)
assert_equal(ticket.id, article_result['ticket_id'])
assert_equal('Tickets Agent', article_result['from'])
assert_equal('some subject', article_result['subject'])
assert_equal('some body', article_result['body'])
assert_equal('text/plain', article_result['content_type'])
assert_equal(false, article_result['internal'])
assert_equal(@agent.id, article_result['created_by_id'])
assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, article_result['sender_id'])
assert_equal(Ticket::Article::Type.lookup(name: 'note').id, article_result['type_id'])
Scheduler.worker(true)
get "/api/v1/tickets/search?query=\"#{CGI.escape(title)}\"", {}, @headers.merge('Authorization' => credentials)
assert_response(200)
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(Hash, result.class) assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id']) assert_equal(ticket.id, result['tickets'][0])
assert_equal('Tickets Agent', result['from']) assert_equal(1, result['tickets_count'])
assert_equal('some subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])
assert_equal(false, result['internal'])
assert_equal(@agent.id, result['created_by_id'])
assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
assert_equal(Ticket::Article::Type.lookup(name: 'note').id, result['type_id'])
delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials) params = {
condition: {
'ticket.title' => {
operator: 'contains',
value: title,
},
},
}
post '/api/v1/tickets/search', params.to_json, @headers.merge('Authorization' => credentials)
assert_response(200)
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['tickets'][0])
assert_equal(1, result['tickets_count'])
delete "/api/v1/ticket_articles/#{article_result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
assert_response(200) assert_response(200)
params = { params = {
@ -517,8 +541,9 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
end end
test '03.04 ticket with correct ticket id' do test '03.04 ticket with correct ticket id' do
title = "ticket with corret ticket id testme#{rand(999_999_999)}"
ticket = Ticket.create!( ticket = Ticket.create!(
title: 'ticket with corret ticket id', title: title,
group: Group.lookup(name: 'Users'), group: Group.lookup(name: 'Users'),
customer_id: @customer_without_org.id, customer_id: @customer_without_org.id,
state: Ticket::State.lookup(name: 'new'), state: Ticket::State.lookup(name: 'new'),
@ -532,13 +557,13 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(Hash, result.class) assert_equal(Hash, result.class)
assert_equal(ticket.id, result['id']) assert_equal(ticket.id, result['id'])
assert_equal('ticket with corret ticket id', result['title']) assert_equal(title, result['title'])
assert_equal(ticket.customer_id, result['customer_id']) assert_equal(ticket.customer_id, result['customer_id'])
assert_equal(1, result['updated_by_id']) assert_equal(1, result['updated_by_id'])
assert_equal(1, result['created_by_id']) assert_equal(1, result['created_by_id'])
params = { params = {
title: 'ticket with corret ticket id - 2', title: "#{title} - 2",
customer_id: @agent.id, customer_id: @agent.id,
} }
put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials) put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
@ -546,7 +571,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(Hash, result.class) assert_equal(Hash, result.class)
assert_equal(ticket.id, result['id']) assert_equal(ticket.id, result['id'])
assert_equal('ticket with corret ticket id - 2', result['title']) assert_equal("#{title} - 2", result['title'])
assert_equal(ticket.customer_id, result['customer_id']) assert_equal(ticket.customer_id, result['customer_id'])
assert_equal(@customer_without_org.id, result['updated_by_id']) assert_equal(@customer_without_org.id, result['updated_by_id'])
assert_equal(1, result['created_by_id']) assert_equal(1, result['created_by_id'])
@ -558,18 +583,41 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
} }
post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials) post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
assert_response(201) assert_response(201)
article_result = JSON.parse(@response.body)
assert_equal(Hash, article_result.class)
assert_equal(ticket.id, article_result['ticket_id'])
assert_equal('Tickets Customer1', article_result['from'])
assert_equal('some subject', article_result['subject'])
assert_equal('some body', article_result['body'])
assert_equal('text/plain', article_result['content_type'])
assert_equal(@customer_without_org.id, article_result['created_by_id'])
assert_equal(Ticket::Article::Sender.lookup(name: 'Customer').id, article_result['sender_id'])
assert_equal(Ticket::Article::Type.lookup(name: 'note').id, article_result['type_id'])
Scheduler.worker(true)
get "/api/v1/tickets/search?query=\"#{CGI.escape(title)}\"", {}, @headers.merge('Authorization' => credentials)
assert_response(200)
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(Hash, result.class) assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id']) assert_equal(ticket.id, result['tickets'][0])
assert_equal('Tickets Customer1', result['from']) assert_equal(1, result['tickets_count'])
assert_equal('some subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])
assert_equal(@customer_without_org.id, result['created_by_id'])
assert_equal(Ticket::Article::Sender.lookup(name: 'Customer').id, result['sender_id'])
assert_equal(Ticket::Article::Type.lookup(name: 'note').id, result['type_id'])
delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials) params = {
condition: {
'ticket.title' => {
operator: 'contains',
value: title,
},
},
}
post '/api/v1/tickets/search', params.to_json, @headers.merge('Authorization' => credentials)
assert_response(200)
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['tickets'][0])
assert_equal(1, result['tickets_count'])
delete "/api/v1/ticket_articles/#{article_result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
assert_response(401) assert_response(401)
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(Hash, result.class) assert_equal(Hash, result.class)