elastic_searchable 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/elastic_searchable/version.rb +1 -1
- data/lib/elastic_searchable.rb +4 -0
- data/test/test_elastic_searchable.rb +86 -0
- metadata +4 -4
data/lib/elastic_searchable.rb
CHANGED
@@ -42,6 +42,10 @@ module ElasticSearchable
|
|
42
42
|
response
|
43
43
|
end
|
44
44
|
|
45
|
+
def escape_query(string)
|
46
|
+
string.to_s.gsub(/([\(\)\[\]\{\}\?\\\"!\^\+\-\*:~])/,'\\\\\1')
|
47
|
+
end
|
48
|
+
|
45
49
|
private
|
46
50
|
# all elasticsearch rest calls return a json response when an error occurs. ex:
|
47
51
|
# {error: 'an error occurred' }
|
@@ -468,6 +468,92 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
468
468
|
assert_equal 2, @results.num_pages
|
469
469
|
end
|
470
470
|
end
|
471
|
+
|
472
|
+
context "escape_query" do
|
473
|
+
should "escape exclamation marks" do
|
474
|
+
queryString = '!'
|
475
|
+
result = ElasticSearchable.escape_query(queryString)
|
476
|
+
assert_equal '\!', result
|
477
|
+
end
|
478
|
+
|
479
|
+
should "escape ^" do
|
480
|
+
queryString = '^'
|
481
|
+
result = ElasticSearchable.escape_query(queryString)
|
482
|
+
assert_equal '\^', result
|
483
|
+
end
|
484
|
+
|
485
|
+
should "escape +" do
|
486
|
+
queryString = '+'
|
487
|
+
result = ElasticSearchable.escape_query(queryString)
|
488
|
+
assert_equal '\+', result
|
489
|
+
end
|
490
|
+
|
491
|
+
should "escape -" do
|
492
|
+
queryString = '-'
|
493
|
+
result = ElasticSearchable.escape_query(queryString)
|
494
|
+
assert_equal '\-', result
|
495
|
+
end
|
496
|
+
|
497
|
+
should "escape (" do
|
498
|
+
queryString = '('
|
499
|
+
result = ElasticSearchable.escape_query(queryString)
|
500
|
+
assert_equal '\(', result
|
501
|
+
end
|
502
|
+
|
503
|
+
should "escape )" do
|
504
|
+
queryString = ')'
|
505
|
+
result = ElasticSearchable.escape_query(queryString)
|
506
|
+
assert_equal '\)', result
|
507
|
+
end
|
508
|
+
|
509
|
+
should "escape {" do
|
510
|
+
queryString = '}'
|
511
|
+
result = ElasticSearchable.escape_query(queryString)
|
512
|
+
assert_equal '\}', result
|
513
|
+
end
|
514
|
+
|
515
|
+
should "escape [" do
|
516
|
+
queryString = '['
|
517
|
+
result = ElasticSearchable.escape_query(queryString)
|
518
|
+
assert_equal '\[', result
|
519
|
+
end
|
520
|
+
|
521
|
+
should "escape ]" do
|
522
|
+
queryString = ']'
|
523
|
+
result = ElasticSearchable.escape_query(queryString)
|
524
|
+
assert_equal '\]', result
|
525
|
+
end
|
526
|
+
|
527
|
+
should 'escape "' do
|
528
|
+
queryString = '"'
|
529
|
+
result = ElasticSearchable.escape_query(queryString)
|
530
|
+
assert_equal '\"', result
|
531
|
+
end
|
532
|
+
|
533
|
+
should "escape ~" do
|
534
|
+
queryString = '~'
|
535
|
+
result = ElasticSearchable.escape_query(queryString)
|
536
|
+
assert_equal '\~', result
|
537
|
+
end
|
538
|
+
|
539
|
+
should "escape *" do
|
540
|
+
queryString = '*'
|
541
|
+
result = ElasticSearchable.escape_query(queryString)
|
542
|
+
assert_equal '\*', result
|
543
|
+
end
|
544
|
+
|
545
|
+
should "escape :" do
|
546
|
+
queryString = ':'
|
547
|
+
result = ElasticSearchable.escape_query(queryString)
|
548
|
+
assert_equal '\:', result
|
549
|
+
end
|
550
|
+
|
551
|
+
should "escape ?" do
|
552
|
+
queryString = '?'
|
553
|
+
result = ElasticSearchable.escape_query(queryString)
|
554
|
+
assert_equal '\?', result
|
555
|
+
end
|
556
|
+
end
|
471
557
|
end
|
472
558
|
end
|
473
559
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elastic_searchable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 2
|
10
|
+
version: 1.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ryan Sonnek
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-11-14 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: activerecord
|