elastic_record 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +2 -1
- data/elastic_record.gemspec +1 -1
- data/lib/elastic_record/lucene.rb +0 -54
- data/lib/elastic_record/relation/batches.rb +3 -3
- data/lib/elastic_record/relation/search_methods.rb +14 -18
- data/test/elastic_record/lucene_test.rb +0 -26
- data/test/elastic_record/relation/batches_test.rb +3 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2cbc5a9b19d4e4536a11ca0c7a9a871d94122454
|
4
|
+
data.tar.gz: d9a056220a3afdc7220fd38d416469406689ca22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 391e0c18ac97b92f0a9048aa1e2c158438a24cb984303b7ed96a642b49c39caa57905645695b8357548f233a21ea37b76f530156068b1cfd625139f2b17627cc
|
7
|
+
data.tar.gz: 7366f981a1d0e9d5c3f273f625f8a44c77dc7352a29ab19c729eb4d911982848370e05a7fe2ecaca88ebac7869deb84b04b3eec74cffec2965402f20cd751cf1
|
data/README.rdoc
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
= ElasticRecord
|
2
|
-
{<img src="https://secure.travis-ci.org/
|
2
|
+
{<img src="https://secure.travis-ci.org/data-axle/elastic_record.png?rvm=2.0.0" />}[http://travis-ci.org/data-axle/elastic_record]
|
3
|
+
{<img src="https://codeclimate.com/github/data-axle/elastic_record.png" />}[https://codeclimate.com/github/data-axle/elastic_record]
|
3
4
|
|
4
5
|
ElasticRecord is an elasticsearch ORM.
|
5
6
|
|
data/elastic_record.gemspec
CHANGED
@@ -10,60 +10,6 @@ module ElasticRecord
|
|
10
10
|
def escape(query)
|
11
11
|
query.gsub(ESCAPE_REGEX, "\\\\\\1")
|
12
12
|
end
|
13
|
-
|
14
|
-
# Returns a lucene query that works like GMail
|
15
|
-
def match_phrase(query, fields, &block)
|
16
|
-
return if query.blank?
|
17
|
-
|
18
|
-
words = split_phrase_into_words(query)
|
19
|
-
|
20
|
-
words.map do |word|
|
21
|
-
if word =~ /^(\w+):(.+)$/ && fields.include?($1)
|
22
|
-
match_word $2, [block_given? ? yield($1) : $1]
|
23
|
-
else
|
24
|
-
match_word word, (block_given? ? fields.map(&block) : fields)
|
25
|
-
end
|
26
|
-
end.join(' AND ')
|
27
|
-
end
|
28
|
-
|
29
|
-
# Performs a prefix match on the word:
|
30
|
-
#
|
31
|
-
# ElasticRecord::Lucene.match_word('blue', ['color', 'name'])
|
32
|
-
# => (color:blue* OR name:blue*)
|
33
|
-
#
|
34
|
-
# In the case that the word has special characters, it is wrapped in quotes:
|
35
|
-
#
|
36
|
-
# ElasticRecord::Lucene.match_word('A&M', ['name'])
|
37
|
-
# => (name:"A&M")
|
38
|
-
def match_word(word, fields)
|
39
|
-
if word =~ / / || word =~ ESCAPE_REGEX
|
40
|
-
word = "\"#{word.gsub('"', '')}\""
|
41
|
-
else
|
42
|
-
word = "#{word}*"
|
43
|
-
end
|
44
|
-
|
45
|
-
or_query = fields.map do |field|
|
46
|
-
"#{field}:#{word}"
|
47
|
-
end.join(' OR ')
|
48
|
-
|
49
|
-
"(#{or_query})"
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
# Converts a sentence into the words:
|
54
|
-
#
|
55
|
-
# split_phrase_into_words('his "blue fox"')
|
56
|
-
# => ['his', 'blue fox']
|
57
|
-
def split_phrase_into_words(phrase)
|
58
|
-
# If we have an odd number of double quotes,
|
59
|
-
# add a double quote to the end so that shellwords
|
60
|
-
# does not crap out.
|
61
|
-
if phrase.count('"') % 2 == 1
|
62
|
-
phrase = "#{phrase}\""
|
63
|
-
end
|
64
|
-
|
65
|
-
Shellwords::shellwords phrase.gsub("'", "\"'\"")
|
66
|
-
end
|
67
13
|
end
|
68
14
|
end
|
69
15
|
end
|
@@ -13,10 +13,10 @@ module ElasticRecord
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def find_ids_in_batches(options = {})
|
16
|
+
def find_ids_in_batches(options = {}, &block)
|
17
17
|
options.assert_valid_keys(:batch_size)
|
18
18
|
|
19
|
-
scroll_keep_alive = '
|
19
|
+
scroll_keep_alive = options[:keep_alive] || '5m'
|
20
20
|
size = options[:batch_size] || 100
|
21
21
|
|
22
22
|
options = {
|
@@ -28,7 +28,7 @@ module ElasticRecord
|
|
28
28
|
scroll_id = klass.elastic_index.search(as_elastic, options)['_scroll_id']
|
29
29
|
|
30
30
|
while (hit_ids = get_scroll_hit_ids(scroll_id, scroll_keep_alive)).any?
|
31
|
-
|
31
|
+
hit_ids.each_slice(size, &block)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -2,27 +2,23 @@ module ElasticRecord
|
|
2
2
|
class Relation
|
3
3
|
module SearchMethods
|
4
4
|
Relation::MULTI_VALUE_METHODS.each do |name|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end # end
|
13
|
-
CODE
|
5
|
+
define_method "#{name}_values" do
|
6
|
+
@values[name] || []
|
7
|
+
end
|
8
|
+
|
9
|
+
define_method "#{name}_values=" do |values|
|
10
|
+
@values[name] = values
|
11
|
+
end
|
14
12
|
end
|
15
13
|
|
16
14
|
Relation::SINGLE_VALUE_METHODS.each do |name|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end # end
|
25
|
-
CODE
|
15
|
+
define_method "#{name}_value" do
|
16
|
+
@values[name]
|
17
|
+
end
|
18
|
+
|
19
|
+
define_method "#{name}_value=" do |value|
|
20
|
+
@values[name] = value
|
21
|
+
end
|
26
22
|
end
|
27
23
|
|
28
24
|
def query!(value)
|
@@ -5,30 +5,4 @@ class ElasticRecord::LuceneTest < MiniTest::Spec
|
|
5
5
|
assert_equal "\\\\", ElasticRecord::Lucene.escape("\\")
|
6
6
|
assert_equal "Matt \\&& Joe", ElasticRecord::Lucene.escape("Matt && Joe")
|
7
7
|
end
|
8
|
-
|
9
|
-
def test_match_phrase
|
10
|
-
assert_match_phrase nil, '', ['name']
|
11
|
-
assert_match_phrase nil, nil, ['name']
|
12
|
-
|
13
|
-
assert_match_phrase '(name:foo*)', 'foo', ['name']
|
14
|
-
assert_match_phrase '(name:"foo-bar")', 'foo-bar', ['name']
|
15
|
-
assert_match_phrase "(name:bob's*)", "bob's", ['name']
|
16
|
-
assert_match_phrase '(name:foo* OR street:foo*)', 'foo', ['name', 'street']
|
17
|
-
assert_match_phrase '(name:"foo bar" OR street:"foo bar") AND (name:faz* OR street:faz*)', '"foo bar" faz', ['name', 'street']
|
18
|
-
assert_match_phrase '(street:"42 place") AND (name:bar*)', 'street:"42 place" name:bar', ['name', 'street']
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_match_phrase_with_unmatched_quotes
|
22
|
-
assert_match_phrase '(name:"foo bar")', '"foo bar', ['name']
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_match_phrase_with_block
|
26
|
-
assert_match_phrase '(name.analyzed:foo*)', 'foo', ['name'] { |f| "#{f}.analyzed" }
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def assert_match_phrase(expected, query, fields, &block)
|
32
|
-
assert_equal expected, ElasticRecord::Lucene.match_phrase(query, fields, &block)
|
33
|
-
end
|
34
8
|
end
|
@@ -24,11 +24,13 @@ class ElasticRecord::Relation::BatchesTest < MiniTest::Spec
|
|
24
24
|
|
25
25
|
def test_find_ids_in_batches_with_size
|
26
26
|
results = []
|
27
|
-
Widget.elastic_relation.find_ids_in_batches(batch_size:
|
27
|
+
Widget.elastic_relation.find_ids_in_batches(batch_size: 2) do |ids|
|
28
28
|
results << ids
|
29
29
|
end
|
30
30
|
|
31
31
|
assert_equal 2, results.size
|
32
|
+
assert_equal 2, results[0].size
|
33
|
+
assert_equal 1, results[1].size
|
32
34
|
assert_equal ['5', '10', '15'].to_set, results.flatten.to_set
|
33
35
|
end
|
34
36
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elastic_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Infogroup
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: arelastic
|