elastic / elasticsearch-rails

Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Apache License 2.0
3.07k stars 793 forks source link

Indexes are not refreshing, even with refresh_interval being set #1039

Open crimson-knight opened 2 years ago

crimson-knight commented 2 years ago

I ran into this situation after doing a ruby & rails upgrade (rails 6.0.x -> 6.1.x, ruby 2.7.x -> ruby 3.1.x)

We have a model setup similarly to this:

class Voter < ApplicationRecord
  include Elasticsearch::Model

  # ...
  # All kinds of business code
  # ..

  settings index: {
    analysis: {
      tokenizer: {
        autocomplete: {
          type: :edge_ngram,
          min_gram: 2,
          max_gram: 10,
          token_chars: [
            :letter,
            :digit
          ]
        }
      },
      analyzer: {
        folding: {
          tokenizer: :standard,
          filter: [ :lowercase, :asciifolding ]
        },
        autocomplete: {
          tokenizer: :autocomplete,
          filter: [
            :lowercase,
            :asciifolding
          ]
        },
        autocomplete_search: {
          tokenizer: :lowercase
        }
      }
    }
  } do
    mappings dynamic: false do
      # Specify a bunch of indexes in here
    end
  end
end

This was working before we did the upgrade. Here's the command and diff output to show the gem versions that changed:

 git diff master Gemfile.lock | grep elastic
-    elasticsearch (7.9.0)
-      elasticsearch-api (= 7.9.0)
-      elasticsearch-transport (= 7.9.0)
-    elasticsearch-api (7.9.0)
+    elasticsearch (7.13.3)
+      elasticsearch-api (= 7.13.3)
+      elasticsearch-transport (= 7.13.3)
+    elasticsearch-api (7.13.3)
-    elasticsearch-model (7.1.1)
+    elasticsearch-model (7.2.1)
-      elasticsearch (> 1)
+      elasticsearch (~> 7)
-    elasticsearch-rails (7.1.1)
-    elasticsearch-transport (7.9.0)
+    elasticsearch-rails (7.2.1)
+    elasticsearch-transport (7.13.3)
-  elasticsearch-model (~> 7.x)
-  elasticsearch-rails (~> 7.x)
+  elasticsearch (< 7.14)
+  elasticsearch-model (~> 7.2.1)
+  elasticsearch-rails (~> 7.2.1)

Expected Behavior:

When a voter is created and indexed, the searching the index should return the voter. This is how everything worked on the previous gem versions.

Attempted solution

I experimented and added refresh_interval: "1s" to our settings hash, and the expected behavior has resumed.

class Voter < ApplicationRecord
  include Elasticsearch::Model

  # ...
  # All kinds of business code
  # ..

  settings index: {
    refresh_interval: "1s",
    analysis: {
      # same as above
      },
      analyzer: {
        # same as above
        },
        autocomplete: {
          # same as above
        },
        autocomplete_search: {
          # same as above
        }
      }
    }
  } do
    mappings dynamic: false do
      # Specify a bunch of indexes in here
    end
  end
end

This worked temporarily then stopped working. I've now had to add Voter.__elasticsearch__.refresh_index! for the updates in the index to appear.

We have other models that the indexes are updating, but they are not using the index: {} hash in the settings method at all. This seems to be the only outlying difference.