elastic_record 4.1.3 → 4.1.4
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/README.md +22 -0
 - data/elastic_record.gemspec +1 -1
 - data/lib/elastic_record/index.rb +8 -0
 - data/lib/elastic_record/relation.rb +18 -9
 - data/lib/elastic_record/tasks/index.rake +7 -3
 - data/test/dummy/.env.test +1 -1
 - data/test/elastic_record/index_test.rb +7 -0
 - data/test/elastic_record/relation_test.rb +13 -0
 - 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: 421498813d046dccefd30ad42c6f838aa9f9eb68
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: a86f63e2eac01170619fe51bf2531b856186e676
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz: ' 
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 84eec08e7e9e8dfea71136e8865e9a487f436674f04ec6de93c652e6193c184d3b072af6334157ca3f4176ef0532126c93b19e57f1e366248bb6171536785986
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: '05934bd3792f3b6a264aa7ec12cb93e9bcf9a50c518bc4f45780c8cb8fad46456cc45f87bfcc7d590eff9166145192e2c12a433753cf4c99b5e494ef9a2bc4b6'
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -194,6 +194,28 @@ class Product 
     | 
|
| 
       194 
194 
     | 
    
         
             
            end
         
     | 
| 
       195 
195 
     | 
    
         
             
            ```
         
     | 
| 
       196 
196 
     | 
    
         | 
| 
      
 197 
     | 
    
         
            +
            ### Load Documents from Source
         
     | 
| 
      
 198 
     | 
    
         
            +
             
     | 
| 
      
 199 
     | 
    
         
            +
            To fetch documents without an additional request to a backing ActiveRecord database you can load the documents from `_source`.
         
     | 
| 
      
 200 
     | 
    
         
            +
             
     | 
| 
      
 201 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 202 
     | 
    
         
            +
            Product.elastic_index.loading_from_source do
         
     | 
| 
      
 203 
     | 
    
         
            +
              Product.elastic_search.filter(name: "Pizza")
         
     | 
| 
      
 204 
     | 
    
         
            +
            end
         
     | 
| 
      
 205 
     | 
    
         
            +
             
     | 
| 
      
 206 
     | 
    
         
            +
            ```
         
     | 
| 
      
 207 
     | 
    
         
            +
             
     | 
| 
      
 208 
     | 
    
         
            +
            Use `elastic_index.load_from_source = true` to configure an index without ActiveRecord.
         
     | 
| 
      
 209 
     | 
    
         
            +
             
     | 
| 
      
 210 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 211 
     | 
    
         
            +
            class Product
         
     | 
| 
      
 212 
     | 
    
         
            +
              include ActiveModel::Model
         
     | 
| 
      
 213 
     | 
    
         
            +
              include ElasticRecord::Record
         
     | 
| 
      
 214 
     | 
    
         
            +
             
     | 
| 
      
 215 
     | 
    
         
            +
              self.elastic_index.load_from_source = true
         
     | 
| 
      
 216 
     | 
    
         
            +
            end
         
     | 
| 
      
 217 
     | 
    
         
            +
            ```
         
     | 
| 
      
 218 
     | 
    
         
            +
             
     | 
| 
       197 
219 
     | 
    
         
             
            ### Index Management ###
         
     | 
| 
       198 
220 
     | 
    
         | 
| 
       199 
221 
     | 
    
         
             
            If you need to manage multiple indexes via the rake tasks, you will need to declare them explicitly:
         
     | 
    
        data/elastic_record.gemspec
    CHANGED
    
    
    
        data/lib/elastic_record/index.rb
    CHANGED
    
    | 
         @@ -37,6 +37,7 @@ module ElasticRecord 
     | 
|
| 
       37 
37 
     | 
    
         
             
                attr_accessor :disabled
         
     | 
| 
       38 
38 
     | 
    
         
             
                attr_accessor :model
         
     | 
| 
       39 
39 
     | 
    
         
             
                attr_accessor :partial_updates
         
     | 
| 
      
 40 
     | 
    
         
            +
                attr_accessor :load_from_source
         
     | 
| 
       40 
41 
     | 
    
         | 
| 
       41 
42 
     | 
    
         
             
                def initialize(models)
         
     | 
| 
       42 
43 
     | 
    
         
             
                  models = Array.wrap(models)
         
     | 
| 
         @@ -65,6 +66,13 @@ module ElasticRecord 
     | 
|
| 
       65 
66 
     | 
    
         
             
                  @disabled = false
         
     | 
| 
       66 
67 
     | 
    
         
             
                end
         
     | 
| 
       67 
68 
     | 
    
         | 
| 
      
 69 
     | 
    
         
            +
                def loading_from_source(&block)
         
     | 
| 
      
 70 
     | 
    
         
            +
                  @load_from_source = true
         
     | 
| 
      
 71 
     | 
    
         
            +
                  yield
         
     | 
| 
      
 72 
     | 
    
         
            +
                ensure
         
     | 
| 
      
 73 
     | 
    
         
            +
                  @load_from_source = false
         
     | 
| 
      
 74 
     | 
    
         
            +
                end
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
       68 
76 
     | 
    
         
             
                def real_connection
         
     | 
| 
       69 
77 
     | 
    
         
             
                  model.elastic_connection
         
     | 
| 
       70 
78 
     | 
    
         
             
                end
         
     | 
| 
         @@ -35,7 +35,7 @@ module ElasticRecord 
     | 
|
| 
       35 
35 
     | 
    
         
             
                end
         
     | 
| 
       36 
36 
     | 
    
         | 
| 
       37 
37 
     | 
    
         
             
                def to_a
         
     | 
| 
       38 
     | 
    
         
            -
                  @records ||= load_hits 
     | 
| 
      
 38 
     | 
    
         
            +
                  @records ||= load_hits
         
     | 
| 
       39 
39 
     | 
    
         
             
                end
         
     | 
| 
       40 
40 
     | 
    
         | 
| 
       41 
41 
     | 
    
         
             
                def to_ids
         
     | 
| 
         @@ -62,11 +62,12 @@ module ElasticRecord 
     | 
|
| 
       62 
62 
     | 
    
         
             
                  klass.current_elastic_search = previous
         
     | 
| 
       63 
63 
     | 
    
         
             
                end
         
     | 
| 
       64 
64 
     | 
    
         | 
| 
       65 
     | 
    
         
            -
                def search_hits
         
     | 
| 
       66 
     | 
    
         
            -
                  search_results['hits']['hits']
         
     | 
| 
       67 
     | 
    
         
            -
                end
         
     | 
| 
       68 
     | 
    
         
            -
             
     | 
| 
       69 
65 
     | 
    
         
             
                private
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
      
 67 
     | 
    
         
            +
                  def search_hits
         
     | 
| 
      
 68 
     | 
    
         
            +
                    search_results['hits']['hits']
         
     | 
| 
      
 69 
     | 
    
         
            +
                  end
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
       70 
71 
     | 
    
         
             
                  def reset
         
     | 
| 
       71 
72 
     | 
    
         
             
                    @search_results = @records = nil
         
     | 
| 
       72 
73 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -75,13 +76,21 @@ module ElasticRecord 
     | 
|
| 
       75 
76 
     | 
    
         
             
                    @search_results ||= begin
         
     | 
| 
       76 
77 
     | 
    
         
             
                      options = search_type_value ? {search_type: search_type_value} : {}
         
     | 
| 
       77 
78 
     | 
    
         | 
| 
       78 
     | 
    
         
            -
                      klass.elastic_index. 
     | 
| 
      
 79 
     | 
    
         
            +
                      unless klass.elastic_index.load_from_source
         
     | 
| 
      
 80 
     | 
    
         
            +
                        as_elastic.update('_source' => false)
         
     | 
| 
      
 81 
     | 
    
         
            +
                      end
         
     | 
| 
      
 82 
     | 
    
         
            +
             
     | 
| 
      
 83 
     | 
    
         
            +
                      klass.elastic_index.search(as_elastic, options)
         
     | 
| 
       79 
84 
     | 
    
         
             
                    end
         
     | 
| 
       80 
85 
     | 
    
         
             
                  end
         
     | 
| 
       81 
86 
     | 
    
         | 
| 
       82 
     | 
    
         
            -
                  def load_hits 
     | 
| 
       83 
     | 
    
         
            -
                     
     | 
| 
       84 
     | 
    
         
            -
             
     | 
| 
      
 87 
     | 
    
         
            +
                  def load_hits
         
     | 
| 
      
 88 
     | 
    
         
            +
                    if klass.elastic_index.load_from_source
         
     | 
| 
      
 89 
     | 
    
         
            +
                       search_hits.map { |hit| klass.new(hit['_source'].update('id' => hit['_id'])) }
         
     | 
| 
      
 90 
     | 
    
         
            +
                    else
         
     | 
| 
      
 91 
     | 
    
         
            +
                      scope = select_values.any? ? klass.select(select_values) : klass
         
     | 
| 
      
 92 
     | 
    
         
            +
                      scope.find(to_ids)
         
     | 
| 
      
 93 
     | 
    
         
            +
                    end
         
     | 
| 
       85 
94 
     | 
    
         
             
                  end
         
     | 
| 
       86 
95 
     | 
    
         
             
              end
         
     | 
| 
       87 
96 
     | 
    
         
             
            end
         
     | 
| 
         @@ -51,9 +51,13 @@ namespace :index do 
     | 
|
| 
       51 
51 
     | 
    
         
             
                    index_name = model.elastic_index.create
         
     | 
| 
       52 
52 
     | 
    
         
             
                  end
         
     | 
| 
       53 
53 
     | 
    
         | 
| 
       54 
     | 
    
         
            -
                   
     | 
| 
       55 
     | 
    
         
            -
             
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
      
 54 
     | 
    
         
            +
                  if model.elastic_index.load_from_source
         
     | 
| 
      
 55 
     | 
    
         
            +
                    puts "  Reindexing into #{index_name} [skipped]"
         
     | 
| 
      
 56 
     | 
    
         
            +
                  else
         
     | 
| 
      
 57 
     | 
    
         
            +
                    puts "  Reindexing into #{index_name}"
         
     | 
| 
      
 58 
     | 
    
         
            +
                    model.find_in_batches(batch_size: 100) do |records|
         
     | 
| 
      
 59 
     | 
    
         
            +
                      model.elastic_index.bulk_add(records, index_name: index_name)
         
     | 
| 
      
 60 
     | 
    
         
            +
                    end
         
     | 
| 
       57 
61 
     | 
    
         
             
                  end
         
     | 
| 
       58 
62 
     | 
    
         | 
| 
       59 
63 
     | 
    
         
             
                  puts "  Deploying index..."
         
     | 
    
        data/test/dummy/.env.test
    CHANGED
    
    | 
         @@ -1 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            DATABASE_URL=postgres://localhost/ 
     | 
| 
      
 1 
     | 
    
         
            +
            DATABASE_URL=postgres://localhost/elastic_record_subtest
         
     | 
| 
         @@ -28,6 +28,13 @@ class ElasticRecord::IndexTest < MiniTest::Test 
     | 
|
| 
       28 
28 
     | 
    
         
             
                refute index.disabled
         
     | 
| 
       29 
29 
     | 
    
         
             
              end
         
     | 
| 
       30 
30 
     | 
    
         | 
| 
      
 31 
     | 
    
         
            +
              def test_loading_from_source
         
     | 
| 
      
 32 
     | 
    
         
            +
                index.loading_from_source do
         
     | 
| 
      
 33 
     | 
    
         
            +
                  assert index.load_from_source
         
     | 
| 
      
 34 
     | 
    
         
            +
                end
         
     | 
| 
      
 35 
     | 
    
         
            +
                refute index.load_from_source
         
     | 
| 
      
 36 
     | 
    
         
            +
              end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
       31 
38 
     | 
    
         
             
              private
         
     | 
| 
       32 
39 
     | 
    
         | 
| 
       33 
40 
     | 
    
         
             
                def index
         
     | 
| 
         @@ -42,6 +42,19 @@ class ElasticRecord::RelationTest < MiniTest::Test 
     | 
|
| 
       42 
42 
     | 
    
         
             
                assert array.first.is_a?(Widget)
         
     | 
| 
       43 
43 
     | 
    
         
             
              end
         
     | 
| 
       44 
44 
     | 
    
         | 
| 
      
 45 
     | 
    
         
            +
              def test_to_a_from_source
         
     | 
| 
      
 46 
     | 
    
         
            +
                Widget.elastic_index.loading_from_source do
         
     | 
| 
      
 47 
     | 
    
         
            +
                  create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                  array = Widget.elastic_relation.to_a
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                  assert_equal 2, array.size
         
     | 
| 
      
 52 
     | 
    
         
            +
                  assert array.first.is_a?(Widget)
         
     | 
| 
      
 53 
     | 
    
         
            +
                  assert_equal 'red', array.first.color
         
     | 
| 
      
 54 
     | 
    
         
            +
                  assert_equal 'blue', array.last.color
         
     | 
| 
      
 55 
     | 
    
         
            +
                end
         
     | 
| 
      
 56 
     | 
    
         
            +
              end
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
       45 
58 
     | 
    
         
             
              def test_delete_all
         
     | 
| 
       46 
59 
     | 
    
         
             
                project_red = Project.create! name: 'Red'
         
     | 
| 
       47 
60 
     | 
    
         
             
                project_blue = Project.create! name: 'Blue'
         
     | 
    
        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: 4.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 4.1.4
         
     | 
| 
       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: 2017-09- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2017-09-08 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: arelastic
         
     |