Coder Social home page Coder Social logo

Import Data Faster about elasticsearch-rails HOT 8 CLOSED

elastic avatar elastic commented on September 24, 2024
Import Data Faster

from elasticsearch-rails.

Comments (8)

karmi avatar karmi commented on September 24, 2024 1

To perform the join or limit the scope of the import method, just write your own importing routine, using find_in_batches, something like:

errors       = 0

Article.where("published_on > 2013-12-01").find_in_batches(batch_size: 100) do |articles|
  response = Article.__elasticsearch__.client.bulk \
    index:   Article.index_name,
    type:    Article.document_type,
    body:    articles.map { |a| { index: { _id: a.id, data: a.__elasticsearch__.as_indexed_json } } }

  errors += response['items'].map { |k, v| k.values.first['error'] }.compact.length
end

It would make sense to add an import method to the ActiveRecord::Relation / Mongoid::Collection classes, so you'd simply call Article.where("published_on > 2013-12-01"). I'll keep that in mind.

from elasticsearch-rails.

AaronRustad avatar AaronRustad commented on September 24, 2024

@bcackerman the ability to specify ActiveRecord scope on the Rake import task has recently been added in #104.

from elasticsearch-rails.

bcackerman avatar bcackerman commented on September 24, 2024

Is there a way to use force: true with the version @karmi posted?

from elasticsearch-rails.

karmi avatar karmi commented on September 24, 2024

@bcackerman What do you mean by using the force option in this context? As @aackerman says (thanks!), you can specify the scope in the environment variable for the Rake task, and then just use FORCE=y.

If you wanna manage it yourself, then the create_index! method accepts a force option -- please see the RDoc.

from elasticsearch-rails.

netwire88 avatar netwire88 commented on September 24, 2024

Hi @karmi, has any user been able to replicate your bulk import example with Mongoid instead of ActiveRecord? I search all over the net but since Mongoid doesn't have find_in_batches, I'm not sure how to implement your example above.

from elasticsearch-rails.

karmi avatar karmi commented on September 24, 2024

@netwire88 It should work exactly the same for Mongoid -- there's an adapter for Mongoid shipped.

from elasticsearch-rails.

karmi avatar karmi commented on September 24, 2024

@netwire88 I mean, if you wanna write a custom importing code like that, just use the appropriate Mongoid methods, see eg. this comment, https://github.com/mongoid/mongoid/issues/1334#issuecomment-2419621

from elasticsearch-rails.

netwire88 avatar netwire88 commented on September 24, 2024

Thanks @karmi, does the default import method that comes with elasticsearch rails use bulk import?

from elasticsearch-rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.