From 8a6ffc23cba04eb0d582fab22693dcd90c78d99f Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 17 Apr 2019 08:26:26 +0200 Subject: [PATCH] Fixed issue #2059 - Incorrect display count of a report profile if tree_select is used. --- .gitlab-ci.yml | 2 +- lib/search_index_backend.rb | 42 ++++++++++++++++++++++++++---- test/integration/report_test.rb | 45 ++++++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bf1efac88..7d35803a7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -232,8 +232,8 @@ test:integration:es: - bundle exec rake zammad:db:unseeded - bundle exec rails test test/integration/elasticsearch_active_test.rb - bundle exec rails test test/integration/elasticsearch_test.rb - - bundle exec rails test test/integration/report_test.rb - bundle exec rspec --tag searchindex + - bundle exec rails test test/integration/report_test.rb ### Zendesk diff --git a/lib/search_index_backend.rb b/lib/search_index_backend.rb index be4fd2ab6..9a6f385e0 100644 --- a/lib/search_index_backend.rb +++ b/lib/search_index_backend.rb @@ -333,7 +333,7 @@ remove whole data from index return if url.blank? url += if index - if index.class == Array + if index.is_a?(Array) "/#{index.join(',')}/_search" else "/#{index}/_search" @@ -399,6 +399,7 @@ remove whole data from index next if value.blank? next if order_by&.at(index).blank? + # for sorting values use .raw values (no analyzer is used - plain values) if value !~ /\./ && value !~ /_(time|date|till|id|ids|at)$/ value += '.raw' end @@ -435,6 +436,24 @@ remove whole data from index get count of tickets and tickets which match on selector + result = SearchIndexBackend.selectors(index, selector) + +example with a simple search: + + result = SearchIndexBackend.selectors('Ticket', { category: { operator: 'is', value: 'aa::ab' } }) + + result = [ + { id: 1, type: 'Ticket' }, + { id: 2, type: 'Ticket' }, + { id: 3, type: 'Ticket' }, + ] + +you also can get aggregations + + result = SearchIndexBackend.selectors(index, selector, options, aggs_interval) + +example for aggregations within one year + aggs_interval = { from: '2015-01-01', to: '2015-12-31', @@ -447,9 +466,8 @@ get count of tickets and tickets which match on selector current_user: User.find(123), } - result = SearchIndexBackend.selectors(index, selector, options, aggs_interval) + result = SearchIndexBackend.selectors('Ticket', { category: { operator: 'is', value: 'aa::ab' } }, options, aggs_interval) - # for aggregations result = { hits:{ total:4819, @@ -482,7 +500,7 @@ get count of tickets and tickets which match on selector return if url.blank? url += if index - if index.class == Array + if index.is_a?(Array) "/#{index.join(',')}/_search" else "/#{index}/_search" @@ -553,9 +571,23 @@ get count of tickets and tickets which match on selector key_tmp = key.sub(/^.+?\./, '') t = {} + # use .raw in cases where query contains :: + if data['value'].is_a?(Array) + data['value'].each do |value| + if value.is_a?(String) && value =~ /::/ + key_tmp += '.raw' + break + end + end + elsif data['value'].is_a?(String) + if /::/.match?(data['value']) + key_tmp += '.raw' + end + end + # is/is not/contains/contains not if data['operator'] == 'is' || data['operator'] == 'is not' || data['operator'] == 'contains' || data['operator'] == 'contains not' - if data['value'].class == Array + if data['value'].is_a?(Array) t[:terms] = {} t[:terms][key_tmp] = data['value'] else diff --git a/test/integration/report_test.rb b/test/integration/report_test.rb index 246914105..0938b894a 100644 --- a/test/integration/report_test.rb +++ b/test/integration/report_test.rb @@ -1,10 +1,36 @@ -require 'test_helper' +require 'integration_test_helper' class ReportTest < ActiveSupport::TestCase include SearchindexHelper setup do + # create attribute + attribute1 = ObjectManager::Attribute.add( + object: 'Ticket', + name: 'test_category', + display: 'Test 1', + data_type: 'tree_select', + data_option: { + maxlength: 200, + null: false, + default: '', + options: [ + { 'name' => 'aa', 'value' => 'aa', 'children' => [{ 'name' => 'aa', 'value' => 'aa::aa' }, { 'name' => 'bb', 'value' => 'aa::bb' }, { 'name' => 'cc', 'value' => 'aa::cc' }] }, + { 'name' => 'bb', 'value' => 'bb', 'children' => [{ 'name' => 'aa', 'value' => 'bb::aa' }, { 'name' => 'bb', 'value' => 'bb::bb' }, { 'name' => 'cc', 'value' => 'bb::cc' }] }, + { 'name' => 'cc', 'value' => 'cc', 'children' => [{ 'name' => 'aa', 'value' => 'cc::aa' }, { 'name' => 'bb', 'value' => 'cc::bb' }, { 'name' => 'cc', 'value' => 'cc::cc' }] }, + ] + }, + active: true, + screens: {}, + position: 20, + created_by_id: 1, + updated_by_id: 1, + editable: false, + to_migrate: false, + ) + ObjectManager::Attribute.migration_execute + configure_elasticsearch(required: true) Ticket.destroy_all @@ -26,6 +52,7 @@ class ReportTest < ActiveSupport::TestCase priority: Ticket::Priority.lookup(name: '2 normal'), created_at: '2015-10-28 09:30:00 UTC', updated_at: '2015-10-28 09:30:00 UTC', + test_category: 'cc::bb', updated_by_id: 1, created_by_id: 1, ) @@ -1353,6 +1380,22 @@ class ReportTest < ActiveSupport::TestCase assert(result) assert_nil(result[:ticket_ids][0]) + # search for test_category.raw to find values with :: in query + result = Report::TicketGenericTime.items( + range_start: Time.zone.parse('2015-01-01T00:00:00Z'), + range_end: Time.zone.parse('2015-12-31T23:59:59Z'), + selector: { + 'test_category' => { + 'operator' => 'is', + 'value' => 'cc::bb' + }, + }, # ticket selector to get only a collection of tickets + params: { field: 'created_at' }, + ) + + assert(result) + assert_equal(@ticket1.id, result[:ticket_ids][0].to_i) + assert_nil(result[:ticket_ids][1]) end end