search_index_backend_test.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. require 'test_helper'
  2. class SearchIndexBackendTest < ActiveSupport::TestCase
  3. test 'simple_query_append_wildcard correctly modifies simple queries' do
  4. def clean_queries(query_string)
  5. query_string.each_line
  6. .map(&:strip)
  7. .reject(&:empty?)
  8. .map { |x| x.split('#')[0] }
  9. end
  10. # Examples of complex queries from https://docs.zammad.org/en/latest/general-search.html
  11. complex_queries = clean_queries %(
  12. title:”some words with spaces” # exact phrase / without quotation marks ” an AND search for the words will be performed (in Zammad 1.5 and lower an OR search will be performed)
  13. title:”some wor*” # exact phrase beginning with “some wor*” will be searched
  14. created_at:[2017-01-01 TO 2017-12-31] # a time range
  15. created_at:>now-1h # created within last hour
  16. state:new OR state:open
  17. (state:new OR state:open) OR priority:”3 normal”
  18. (state:new OR state:open) AND customer.lastname:smith
  19. state:(new OR open) AND title:(full text search) # state: new OR open & title: full OR text OR search
  20. tags: “some tag”
  21. owner.email: “bod@example.com” AND state: (new OR open OR pending*) # show all open tickets of a certain agent
  22. state:closed AND _missing_:tag # all closed objects without tags
  23. article_count: [1 TO 5] # tickets with 1 to 5 articles
  24. article_count: [10 TO *] # tickets with 10 or more articles
  25. article.from: bob # also article.from can be used
  26. article.body: heat~ # using the fuzzy operator will also find terms that are similar, in this case also “head”
  27. article.body: /joh?n(ath[oa]n)/ # using regular expressions
  28. user:M
  29. user:Max
  30. user:Max.
  31. user:Max*
  32. organization:A_B
  33. organization:A_B*
  34. user: M
  35. user: Max
  36. user: Max.
  37. user: Max*
  38. organization: A_B
  39. organization: A_B*
  40. id:123
  41. number:123
  42. id:"123"
  43. number:"123"
  44. )
  45. simple_queries = clean_queries %(
  46. M
  47. Max
  48. Max. # dot and underscore are acceptable characters in simple queries
  49. A_
  50. A_B
  51. äöü
  52. 123
  53. *ax # wildcards are allowed in simple queries
  54. Max*
  55. M*x
  56. M?x
  57. test@example.com
  58. test@example.
  59. test@example
  60. test@
  61. )
  62. complex_queries.each do |query|
  63. result_query = SearchIndexBackend.append_wildcard_to_simple_query(query)
  64. # Verify that the result query is still the same as the input query
  65. assert_equal(query, result_query)
  66. end
  67. simple_queries.each do |query|
  68. result_query = SearchIndexBackend.append_wildcard_to_simple_query(query)
  69. # Verify that * is correctly appended to simple queries
  70. assert_equal(query + '*', result_query)
  71. end
  72. end
  73. end