dynamic_sunspot_search 0.1.2 → 0.1.3
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/Gemfile.lock +2 -2
- data/README.md +65 -0
- data/lib/dynamic_sunspot_search/translator.rb +2 -0
- data/lib/dynamic_sunspot_search/translator/boost_recency.rb +41 -0
- data/lib/dynamic_sunspot_search/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 057d51285b3fcce265bf19561a6d04cb41b63eef
|
4
|
+
data.tar.gz: d5cde668784329378f7e5f3c93d1d712d0cce417
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62524c3fe09f6b10b23fdbe455e9bd8951db44a0c850c96c0e28d3c3aa149a9a05f9979e565f166baa23989ed1a8101dded0a8bbaaa190b623981bc26a2801cc
|
7
|
+
data.tar.gz: 6856caf7f073660ef89d39e074711b5a88b3777c418009572c7ae4c9d6e0fa97dc24f42483a3d837626220d67457387776a382f528e89d22c8c057e7edc68916
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -650,6 +650,71 @@ Post.dynamic_search({
|
|
650
650
|
})
|
651
651
|
```
|
652
652
|
|
653
|
+
#### Boost By Recency
|
654
|
+
|
655
|
+
By default, Sunspot orders results by "score": the Solr-determined
|
656
|
+
relevancy metric. This score can be weighted based on the documents recency by
|
657
|
+
defining a field as type trie:
|
658
|
+
|
659
|
+
```ruby
|
660
|
+
class Post
|
661
|
+
searchable do
|
662
|
+
# ...
|
663
|
+
time :published_at, trie: true
|
664
|
+
# ...
|
665
|
+
end
|
666
|
+
```
|
667
|
+
|
668
|
+
Then by using the `boost_receny` method, where half_life defines the half life
|
669
|
+
period of the exponential decay function:
|
670
|
+
|
671
|
+
```ruby
|
672
|
+
# Posts that were published 5 day ago will have their scores cut in half
|
673
|
+
Post.dynamic_search({
|
674
|
+
fulltext: 'pizza',
|
675
|
+
boost_recency: {
|
676
|
+
field: :published_at,
|
677
|
+
half_life: {
|
678
|
+
days: 5,
|
679
|
+
},
|
680
|
+
},
|
681
|
+
})
|
682
|
+
|
683
|
+
# Posts that were published 1 year ago will have their scores cut in half
|
684
|
+
Post.dynamic_search({
|
685
|
+
fulltext: 'pizza',
|
686
|
+
boost_recency: {
|
687
|
+
field: :published_at,
|
688
|
+
half_life: {
|
689
|
+
year: 1,
|
690
|
+
},
|
691
|
+
},
|
692
|
+
})
|
693
|
+
|
694
|
+
# Posts that were published 37 minutes and 10 seconds ago will have their scores cut in half
|
695
|
+
Post.dynamic_search({
|
696
|
+
fulltext: 'pizza',
|
697
|
+
boost_recency: {
|
698
|
+
field: :published_at,
|
699
|
+
half_life: {
|
700
|
+
minutes: 37,
|
701
|
+
seconds: 10,
|
702
|
+
},
|
703
|
+
},
|
704
|
+
})
|
705
|
+
|
706
|
+
# Posts that were published 29.5 months ago will have their scores cut in half
|
707
|
+
Post.dynamic_search({
|
708
|
+
fulltext: 'pizza',
|
709
|
+
boost_recency: {
|
710
|
+
field: :published_at,
|
711
|
+
half_life: {
|
712
|
+
months: 29.5,
|
713
|
+
},
|
714
|
+
},
|
715
|
+
})
|
716
|
+
```
|
717
|
+
|
653
718
|
### Geospatial
|
654
719
|
|
655
720
|
**Sunspot 2.0 only**
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'dynamic_sunspot_search/translator/boost_recency'
|
1
2
|
require 'dynamic_sunspot_search/translator/facet'
|
2
3
|
require 'dynamic_sunspot_search/translator/field_list'
|
3
4
|
require 'dynamic_sunspot_search/translator/order_by'
|
@@ -18,6 +19,7 @@ module DynamicSunspotSearch
|
|
18
19
|
OrderByFunction.apply(search, query.delete(:order_by_function))
|
19
20
|
Paginate.apply(search, query.delete(:paginate))
|
20
21
|
Facet.apply(search, query.delete(:facet))
|
22
|
+
BoostRecency.apply(search, query.delete(:boost_recency))
|
21
23
|
raise ArgumentError.new("Unknown keys detected: #{query.keys}") unless query.blank?
|
22
24
|
end
|
23
25
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
module DynamicSunspotSearch
|
4
|
+
module Translator
|
5
|
+
module BoostRecency
|
6
|
+
# Boost by recency: https://wiki.apache.org/solr/FunctionQuery#Date_Boosting
|
7
|
+
def self.apply(query_object, options)
|
8
|
+
return unless options.present?
|
9
|
+
query_object.tap do |search|
|
10
|
+
Array.wrap(options).each do |recency_boost|
|
11
|
+
apply_recency_boost(query_object, recency_boost)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.apply_recency_boost(query_object, recency_boost)
|
17
|
+
return unless recency_boost.present?
|
18
|
+
field = recency_boost.delete(:field)
|
19
|
+
half_life = recency_boost.delete(:half_life)
|
20
|
+
query_object.tap do |search|
|
21
|
+
search.adjust_solr_params do |sunspot_params|
|
22
|
+
sunspot_params[:defType] = 'edismax'
|
23
|
+
sunspot_params[:boost] = build_boost_string(field, half_life)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.build_boost_string(field, half_life)
|
29
|
+
half_life_ms = calculate_half_life_ms(half_life)
|
30
|
+
half_life_recip = BigDecimal((1.0/(half_life_ms)).to_s).to_s('E').downcase
|
31
|
+
"recip(ms(NOW,#{field.to_s}_dt),#{half_life_recip},100,1)"
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.calculate_half_life_ms(options)
|
35
|
+
options.reduce(0) do |half_life_ms, (period, value)|
|
36
|
+
half_life_ms += value.to_f.send(period.to_sym).to_i*1000
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_sunspot_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Will Bryant
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01-
|
11
|
+
date: 2018-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -235,6 +235,7 @@ files:
|
|
235
235
|
- lib/dynamic_sunspot_search/translator/all_of.rb
|
236
236
|
- lib/dynamic_sunspot_search/translator/any.rb
|
237
237
|
- lib/dynamic_sunspot_search/translator/any_of.rb
|
238
|
+
- lib/dynamic_sunspot_search/translator/boost_recency.rb
|
238
239
|
- lib/dynamic_sunspot_search/translator/facet.rb
|
239
240
|
- lib/dynamic_sunspot_search/translator/field_list.rb
|
240
241
|
- lib/dynamic_sunspot_search/translator/fulltext.rb
|