elastic_record 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- data/elastic_record.gemspec +1 -1
- data/lib/elastic_record/lucene.rb +8 -5
- data/test/elastic_record/lucene_test.rb +3 -2
- data/test/support/widget.rb +11 -3
- metadata +5 -5
data/elastic_record.gemspec
CHANGED
@@ -12,21 +12,21 @@ module ElasticRecord
|
|
12
12
|
end
|
13
13
|
|
14
14
|
# Returns a lucene query that works like G
|
15
|
-
def smart_query(query, fields)
|
15
|
+
def smart_query(query, fields, &block)
|
16
16
|
return if query.blank?
|
17
17
|
|
18
18
|
words = Shellwords::shellwords(query)
|
19
19
|
|
20
20
|
words.map do |word|
|
21
21
|
if word =~ /^(\w+):(.+)$/ && fields.include?($1)
|
22
|
-
match_word $2, [$1]
|
22
|
+
match_word $2, [$1], &block
|
23
23
|
else
|
24
|
-
match_word word, fields
|
24
|
+
match_word word, fields, &block
|
25
25
|
end
|
26
26
|
end.join(' AND ')
|
27
27
|
end
|
28
28
|
|
29
|
-
def match_word(word, fields)
|
29
|
+
def match_word(word, fields, &block)
|
30
30
|
word = escape(word)
|
31
31
|
if word =~ / /
|
32
32
|
word = "\"#{word}\""
|
@@ -34,7 +34,10 @@ module ElasticRecord
|
|
34
34
|
word = "#{word}*"
|
35
35
|
end
|
36
36
|
|
37
|
-
or_query = fields.map
|
37
|
+
or_query = fields.map do |field|
|
38
|
+
field = yield(field) if block_given?
|
39
|
+
"#{field}:#{word}"
|
40
|
+
end.join(' OR ')
|
38
41
|
|
39
42
|
"(#{or_query})"
|
40
43
|
end
|
@@ -11,13 +11,14 @@ class ElasticRecord::LuceneTest < MiniTest::Spec
|
|
11
11
|
assert_smart_escape nil, nil, ['name']
|
12
12
|
|
13
13
|
assert_smart_escape '(name:foo*)', 'foo', ['name']
|
14
|
+
assert_smart_escape '(name.analyzed:foo*)', 'foo', ['name'] { |f| "#{f}.analyzed" }
|
14
15
|
assert_smart_escape '(name:foo* OR street:foo*)', 'foo', ['name', 'street']
|
15
16
|
assert_smart_escape '(name:"foo bar" OR street:"foo bar") AND (name:faz* OR street:faz*)', '"foo bar" faz', ['name', 'street']
|
16
17
|
assert_smart_escape '(street:"42 place") AND (name:bar*)', 'street:"42 place" name:bar', ['name', 'street']
|
17
18
|
end
|
18
19
|
|
19
20
|
private
|
20
|
-
def assert_smart_escape(expected, query, fields)
|
21
|
-
assert_equal expected, ElasticRecord::Lucene.smart_query(query, fields)
|
21
|
+
def assert_smart_escape(expected, query, fields, &block)
|
22
|
+
assert_equal expected, ElasticRecord::Lucene.smart_query(query, fields, &block)
|
22
23
|
end
|
23
24
|
end
|
data/test/support/widget.rb
CHANGED
@@ -7,10 +7,18 @@ class Widget
|
|
7
7
|
include ElasticRecord::Callbacks
|
8
8
|
|
9
9
|
self.elastic_index.mapping[:properties].update(
|
10
|
-
name: {
|
11
|
-
|
10
|
+
name: {
|
11
|
+
type: 'multi_field',
|
12
|
+
fields: {
|
13
|
+
name: {type: 'string', index: 'not_analyzed'},
|
14
|
+
analyzed: {type: 'string', index: 'analyzed'}
|
15
|
+
}
|
16
|
+
},
|
17
|
+
color: {
|
18
|
+
type: 'string', index: 'not_analyzed'
|
19
|
+
}
|
12
20
|
)
|
13
|
-
|
21
|
+
|
14
22
|
class << self
|
15
23
|
def find(ids)
|
16
24
|
ids.map { |id| new(id: id, color: 'red') }
|
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: 0.7.
|
4
|
+
version: 0.7.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-10-05 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: arelastic
|
16
|
-
requirement: &
|
16
|
+
requirement: &70305939301120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70305939301120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activemodel
|
27
|
-
requirement: &
|
27
|
+
requirement: &70305939300660 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70305939300660
|
36
36
|
description: Find your records with elastic search
|
37
37
|
email: developer@matthewhiggins.com
|
38
38
|
executables: []
|