estella 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +7 -13
- data/CHANGELOG.md +2 -1
- data/README.md +42 -13
- data/lib/estella/query.rb +40 -35
- data/lib/estella/version.rb +1 -1
- data/spec/searchable_spec.rb +19 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d881bd248152b83758455a9116ea0c56e382d279
|
4
|
+
data.tar.gz: 31d2a0d826ab272ad92627ddd68b10b6ab74d07b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c60f3f41f1b4baae0fb677034f8edd524e5f14d40761da4f5484f51e9497ff304c6092e2c6d121c129c06462f48e6511ba35314380c21b6708a27fcdc4e6a88
|
7
|
+
data.tar.gz: 1389d0798d01f143d7a9640dc4eba8d367b2fe11e14329d1aa03b90127a2f697b69067c6bfaab946198e4f3a9fa1068100b1d5e2fd257496575a20d2e7f8246c
|
data/.rubocop_todo.yml
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2017-01-
|
3
|
+
# on 2017-01-27 12:52:56 -0500 using RuboCop version 0.47.1.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
|
-
# Offense count:
|
9
|
+
# Offense count: 1
|
10
10
|
Metrics/AbcSize:
|
11
|
-
Max:
|
11
|
+
Max: 18
|
12
12
|
|
13
13
|
# Offense count: 3
|
14
14
|
# Configuration parameters: CountComments, ExcludedMethods.
|
15
15
|
Metrics/BlockLength:
|
16
|
-
Max:
|
16
|
+
Max: 134
|
17
17
|
|
18
|
-
# Offense count:
|
18
|
+
# Offense count: 30
|
19
19
|
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
20
20
|
# URISchemes: http, https
|
21
21
|
Metrics/LineLength:
|
@@ -24,7 +24,7 @@ Metrics/LineLength:
|
|
24
24
|
# Offense count: 1
|
25
25
|
# Configuration parameters: CountComments.
|
26
26
|
Metrics/MethodLength:
|
27
|
-
Max:
|
27
|
+
Max: 13
|
28
28
|
|
29
29
|
# Offense count: 3
|
30
30
|
# Cop supports --auto-correct.
|
@@ -34,11 +34,6 @@ Performance/RedundantMerge:
|
|
34
34
|
- 'lib/estella/parser.rb'
|
35
35
|
- 'lib/estella/searchable.rb'
|
36
36
|
|
37
|
-
# Offense count: 1
|
38
|
-
Style/AccessorMethodName:
|
39
|
-
Exclude:
|
40
|
-
- 'lib/estella/helpers.rb'
|
41
|
-
|
42
37
|
# Offense count: 1
|
43
38
|
Style/ClassVars:
|
44
39
|
Exclude:
|
@@ -55,11 +50,10 @@ Style/Documentation:
|
|
55
50
|
- 'lib/estella/query.rb'
|
56
51
|
- 'lib/estella/searchable.rb'
|
57
52
|
|
58
|
-
# Offense count:
|
53
|
+
# Offense count: 1
|
59
54
|
# Configuration parameters: MinBodyLength.
|
60
55
|
Style/GuardClause:
|
61
56
|
Exclude:
|
62
|
-
- 'lib/estella/query.rb'
|
63
57
|
- 'lib/estella/searchable.rb'
|
64
58
|
|
65
59
|
# Offense count: 9
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
## Changelog
|
2
2
|
|
3
|
-
### 0.
|
3
|
+
### 0.3.0 (Next)
|
4
4
|
|
5
5
|
* [#15](https://github.com/artsy/estella/pull/15): Removed undocumented `set_index_alias!` - [@dblock](https://github.com/dblock).
|
6
6
|
* [#15](https://github.com/artsy/estella/pull/15): Added `delete_index!` and `create_index!` - [@dblock](https://github.com/dblock).
|
7
7
|
* [#14](https://github.com/artsy/estella/pull/14): Fix: destroying documents fails to remove them from the index - [@dblock](https://github.com/dblock).
|
8
|
+
* [#17](https://github.com/artsy/estella/pull/17): Added `exclude` to `estella_search` - [@dblock](https://github.com/dblock).
|
8
9
|
* [#13](https://github.com/artsy/estella/pull/13): Added code coverage w/ Coveralls - [@dblock](https://github.com/dblock).
|
9
10
|
* Your contribution here.
|
10
11
|
|
data/README.md
CHANGED
@@ -27,18 +27,17 @@ Just include the `Estella::Searchable` module and add a `searchable` block in yo
|
|
27
27
|
|
28
28
|
```ruby
|
29
29
|
class Artist < ActiveRecord::Base
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
...
|
30
|
+
include Estella::Searchable
|
31
|
+
|
32
|
+
searchable do
|
33
|
+
field :name, type: :string, analysis: Estella::Analysis::FULLTEXT_ANALYSIS, factor: 1.0
|
34
|
+
field :keywords, type: :string, analysis: ['snowball', 'shingle'], factor: 0.5
|
35
|
+
field :bio, using: :biography, type: :string, index: :not_analyzed
|
36
|
+
field :birth_date, type: :date
|
37
|
+
field :follows, type: :integer
|
38
|
+
field :published, type: :boolean, filter: true
|
39
|
+
boost :follows, modifier: 'log1p', factor: 1E-3
|
40
|
+
end
|
42
41
|
end
|
43
42
|
```
|
44
43
|
|
@@ -173,7 +172,13 @@ Artist.estella_search(term: 'frank', published: true)
|
|
173
172
|
Artist.estella_search(term: 'frank', size: 10, from: 5)
|
174
173
|
```
|
175
174
|
|
176
|
-
|
175
|
+
You can exclude records.
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
Artist.estella_search(term: 'frank', exclude: { keywords: 'sinatra' })
|
179
|
+
```
|
180
|
+
|
181
|
+
If you'd like to customize your query further, you can extend `Estella::Query` and override `query_definition` and `field_factors`:
|
177
182
|
|
178
183
|
```ruby
|
179
184
|
class MyQuery < Estella::Query
|
@@ -184,6 +189,30 @@ class MyQuery < Estella::Query
|
|
184
189
|
}
|
185
190
|
}
|
186
191
|
end
|
192
|
+
|
193
|
+
def field_factors
|
194
|
+
{
|
195
|
+
default: 5,
|
196
|
+
ngram: 5,
|
197
|
+
snowball: 2,
|
198
|
+
shingle: 1,
|
199
|
+
search: 1
|
200
|
+
}
|
201
|
+
end
|
202
|
+
end
|
203
|
+
```
|
204
|
+
|
205
|
+
Or manipulate the query in the initializer directly via `query` or using built-in helpers `must` and `exclude`.
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
class MyQuery < Estella::Query
|
209
|
+
def initialize(params)
|
210
|
+
super
|
211
|
+
# same as query[:filter][:bool][:must] = { keywords: 'frank' }
|
212
|
+
must term: { keywords: 'frank' }
|
213
|
+
# same as query[:filter][:bool][:must_not] = { keywords: 'sinatra' }
|
214
|
+
exclude term: { keywords: 'sinatra' }
|
215
|
+
end
|
187
216
|
end
|
188
217
|
```
|
189
218
|
|
data/lib/estella/query.rb
CHANGED
@@ -16,17 +16,10 @@ module Estella
|
|
16
16
|
}
|
17
17
|
add_query
|
18
18
|
add_filters
|
19
|
+
add_excludes
|
19
20
|
add_pagination
|
20
|
-
add_aggregations if params[:aggregations]
|
21
|
-
add_sort
|
22
21
|
end
|
23
22
|
|
24
|
-
# override if needed
|
25
|
-
def add_aggregations; end
|
26
|
-
|
27
|
-
# override if needed
|
28
|
-
def add_sort; end
|
29
|
-
|
30
23
|
def must(filter)
|
31
24
|
query[:filter][:bool][:must] << filter
|
32
25
|
end
|
@@ -35,6 +28,22 @@ module Estella
|
|
35
28
|
query[:filter][:bool][:must_not] << filter
|
36
29
|
end
|
37
30
|
|
31
|
+
def query_definition
|
32
|
+
{
|
33
|
+
multi_match: {
|
34
|
+
type: 'most_fields',
|
35
|
+
fields: term_search_fields,
|
36
|
+
query: params[:term]
|
37
|
+
}
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
def field_factors
|
42
|
+
Estella::Analysis::DEFAULT_FIELD_FACTORS
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
38
47
|
def add_pagination
|
39
48
|
query[:size] = params[:size] if params[:size]
|
40
49
|
query[:from] = params[:from] if params[:from]
|
@@ -59,32 +68,19 @@ module Estella
|
|
59
68
|
add_field_boost
|
60
69
|
end
|
61
70
|
|
62
|
-
def query_definition
|
63
|
-
{
|
64
|
-
multi_match: {
|
65
|
-
type: 'most_fields',
|
66
|
-
fields: term_search_fields,
|
67
|
-
query: params[:term]
|
68
|
-
}
|
69
|
-
}
|
70
|
-
end
|
71
|
-
|
72
71
|
def add_field_boost
|
73
|
-
|
74
|
-
|
75
|
-
field: params[:boost][:field],
|
76
|
-
modifier: params[:boost][:modifier],
|
77
|
-
factor: params[:boost][:factor]
|
78
|
-
}
|
72
|
+
boost = params[:boost]
|
73
|
+
return unless boost
|
79
74
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
75
|
+
query[:query][:function_score][:field_value_factor] = {
|
76
|
+
field: boost[:field],
|
77
|
+
modifier: boost[:modifier],
|
78
|
+
factor: boost[:factor]
|
79
|
+
}
|
85
80
|
|
86
|
-
|
87
|
-
|
81
|
+
max = boost[:max]
|
82
|
+
return unless max
|
83
|
+
query[:query][:function_score][:max_boost] = max
|
88
84
|
end
|
89
85
|
|
90
86
|
# search all analysed string fields by default
|
@@ -103,10 +99,19 @@ module Estella
|
|
103
99
|
end
|
104
100
|
|
105
101
|
def add_filters
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
102
|
+
indexed_fields = params[:indexed_fields]
|
103
|
+
return unless indexed_fields
|
104
|
+
indexed_fields.each do |field, opts|
|
105
|
+
next unless opts[:filter] && params[field]
|
106
|
+
must term: { field => params[field] }
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def add_excludes
|
111
|
+
exclude = params[:exclude]
|
112
|
+
return unless exclude
|
113
|
+
exclude.each do |k, v|
|
114
|
+
exclude(term: { k => v })
|
110
115
|
end
|
111
116
|
end
|
112
117
|
end
|
data/lib/estella/version.rb
CHANGED
data/spec/searchable_spec.rb
CHANGED
@@ -55,6 +55,9 @@ describe Estella::Searchable, type: :model do
|
|
55
55
|
it 'searches all text fields by default' do
|
56
56
|
expect(SearchableModel.estella_search(term: 'jez')).to eq([@jez])
|
57
57
|
end
|
58
|
+
it 'can exclude an instance' do
|
59
|
+
expect(SearchableModel.estella_search(term: 'jez tez fab', exclude: { keywords: 'jez' })).to eq([@fab, @tez])
|
60
|
+
end
|
58
61
|
it 'boosts on follows_count' do
|
59
62
|
popular_jeremy = SearchableModel.create(title: 'jeremy corban', follows_count: 20_000)
|
60
63
|
SearchableModel.refresh_index!
|
@@ -105,6 +108,22 @@ describe Estella::Searchable, type: :model do
|
|
105
108
|
expect(SearchableModel.estella_search(term: 'david')).to eq([@fab])
|
106
109
|
end
|
107
110
|
end
|
111
|
+
context 'with query customization' do
|
112
|
+
before do
|
113
|
+
class CustomQuery < Estella::Query
|
114
|
+
def initialize(params)
|
115
|
+
super
|
116
|
+
exclude term: { keywords: 'jez' }
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
allow(SearchableModel).to receive(:estella_search_query).and_return(CustomQuery)
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'uses the custom query' do
|
124
|
+
expect(SearchableModel.estella_search(term: 'jez tez fab')).to eq([@fab, @tez])
|
125
|
+
end
|
126
|
+
end
|
108
127
|
end
|
109
128
|
|
110
129
|
describe 'configuration errors' do
|