dynamic_sunspot_search 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e2b8d46b882af75b7e5a0aa53143658cd7b7a317
4
- data.tar.gz: 234e61077f907f6ab306f752f3fa71356c61aa3f
3
+ metadata.gz: 057d51285b3fcce265bf19561a6d04cb41b63eef
4
+ data.tar.gz: d5cde668784329378f7e5f3c93d1d712d0cce417
5
5
  SHA512:
6
- metadata.gz: 19722ee4d4f3dbf96774e7645ffa2ddd423a6a59eca3d22d21b63672e446f15e7aaade5b6576317f10b810c8371a0b26d573cf1bf33a06e237c81aecccd8a1d2
7
- data.tar.gz: 6963b39e069378a04e96819bc736b31c9c7d242b6ab335530d667bdadff822880d60a50333730139f59b3de5ccc4316d6d1ca542b004e6fa108063b36dd19e0c
6
+ metadata.gz: 62524c3fe09f6b10b23fdbe455e9bd8951db44a0c850c96c0e28d3c3aa149a9a05f9979e565f166baa23989ed1a8101dded0a8bbaaa190b623981bc26a2801cc
7
+ data.tar.gz: 6856caf7f073660ef89d39e074711b5a88b3777c418009572c7ae4c9d6e0fa97dc24f42483a3d837626220d67457387776a382f528e89d22c8c057e7edc68916
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dynamic_sunspot_search (0.1.0)
4
+ dynamic_sunspot_search (0.1.3)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -188,4 +188,4 @@ DEPENDENCIES
188
188
  sunspot_solr
189
189
 
190
190
  BUNDLED WITH
191
- 1.16.0
191
+ 1.16.1
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
@@ -1,3 +1,3 @@
1
1
  module DynamicSunspotSearch
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  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.2
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-05 00:00:00.000000000 Z
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