Last active
January 14, 2025 22:34
-
-
Save marcosgz/0bb692c7337e37006e60734f1e503846 to your computer and use it in GitHub Desktop.
Example of inline index using `esse` gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Usage: | |
| # ruby esse_index.rb | |
| require "bundler/inline" | |
| gemfile(true, quiet: true) do | |
| source "https://rubygems.org" | |
| gem "esse" | |
| gem "opensearch-ruby" | |
| gem "pry" | |
| end | |
| Esse.configure do |config| | |
| config.cluster(:default) do |cluster| | |
| cluster.client = OpenSearch::Client.new(url: "http://localhost:9200") | |
| end | |
| end | |
| class ExampleIndex < Esse::Index | |
| self.index_name = "example" | |
| repository :default do | |
| collection do |**_context, &block| | |
| [ | |
| (1..10).map { |i| { id: i, name: "name-#{i}" } }, | |
| (11..15).map { |i| { id: i, name: "name-#{i}" } }, | |
| ].each do |collection| | |
| puts "Processing collection: #{collection.map { |h| h[:id] }}" | |
| block.call(collection) # You may pass kwargs to the block, document will receive them in each interaction | |
| end | |
| end | |
| document do |record, **_collection_given_context| | |
| puts "--> Processing document: #{record[:id]}" | |
| { | |
| _id: record[:id], | |
| name: record[:name], | |
| indexed_at: Time.now.utc, | |
| } | |
| end | |
| end | |
| end | |
| require "pry" | |
| binding.pry |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "bundler/inline" | |
| gemfile(true, quiet: true) do | |
| source "https://rubygems.org" | |
| gem "esse" | |
| gem "opensearch-ruby" | |
| gem "pry" | |
| end | |
| Esse.configure do |config| | |
| config.cluster(:default) do |cluster| | |
| cluster.client = OpenSearch::Client.new(url: "http://localhost:9200") | |
| end | |
| end | |
| class ExampleIndexCollection < Esse::Collection | |
| DATA = [ | |
| (1..10).map { |i| { id: i, name: "name-#{i}" } }, | |
| (11..15).map { |i| { id: i, name: "name-#{i}" } }, | |
| ] | |
| def each | |
| DATA.each do |collection| | |
| puts "Processing collection: #{collection.map { |h| h[:id] }}" | |
| yield collection | |
| end | |
| end | |
| end | |
| class ExampleIndexDocument < Esse::Document | |
| def initialize(doc, **) | |
| puts "--> Processing document: #{doc[:id]}" | |
| super | |
| end | |
| def id | |
| object[:id] | |
| end | |
| def source | |
| { | |
| name: object[:name], | |
| indexed_at: Time.now | |
| } | |
| end | |
| end | |
| class ExampleIndex < Esse::Index | |
| self.index_name = "example" | |
| repository :default do | |
| collection ExampleIndexCollection | |
| document ExampleIndexDocument | |
| end | |
| end | |
| require "pry" | |
| binding.pry |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [1] pry(main)> ExampleIndex.documents | |
| => #<Enumerator: ...> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [2] pry(main)> ExampleIndex.documents.first | |
| Processing collection: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
| --> Processing document: 1 | |
| --> Processing document: 2 | |
| --> Processing document: 3 | |
| --> Processing document: 4 | |
| --> Processing document: 5 | |
| --> Processing document: 6 | |
| --> Processing document: 7 | |
| --> Processing document: 8 | |
| --> Processing document: 9 | |
| --> Processing document: 10 | |
| => #<Esse::HashDocument id: 1, source: {:name=>"name-1", :indexed_at=>2025-01-14 21:39:26.200505929 UTC}> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [3] pry(main)> ExampleIndex.documents.count | |
| Processing collection: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
| --> Processing document: 1 | |
| --> Processing document: 2 | |
| --> Processing document: 3 | |
| --> Processing document: 4 | |
| --> Processing document: 5 | |
| --> Processing document: 6 | |
| --> Processing document: 7 | |
| --> Processing document: 8 | |
| --> Processing document: 9 | |
| --> Processing document: 10 | |
| Processing collection: [11, 12, 13, 14, 15] | |
| --> Processing document: 11 | |
| --> Processing document: 12 | |
| --> Processing document: 13 | |
| --> Processing document: 14 | |
| --> Processing document: 15 | |
| => 15 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [5] pry(main)> ExampleIndex.create_index(alias: true, suffix: 'v1') | |
| => {"acknowledged"=>true, "shards_acknowledged"=>true, "index"=>"example_v1"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [6] pry(main)> ExampleIndex.count | |
| => 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [7] pry(main)> ExampleIndex.import | |
| Processing collection: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
| --> Processing document: 1 | |
| --> Processing document: 2 | |
| --> Processing document: 3 | |
| --> Processing document: 4 | |
| --> Processing document: 5 | |
| --> Processing document: 6 | |
| --> Processing document: 7 | |
| --> Processing document: 8 | |
| --> Processing document: 9 | |
| --> Processing document: 10 | |
| Processing collection: [11, 12, 13, 14, 15] | |
| --> Processing document: 11 | |
| --> Processing document: 12 | |
| --> Processing document: 13 | |
| --> Processing document: 14 | |
| --> Processing document: 15 | |
| => 15 | |
| [8] pry(main)> ExampleIndex.count | |
| => 15 | |
| [9] pry(main)> ExampleIndex.get(id: 12) | |
| => {"_index"=>"example_v1", "_id"=>"12", "_version"=>1, "_seq_no"=>11, "_primary_term"=>1, "found"=>true, "_source"=>{"name"=>"name-12", "indexed_at"=>"2025-01-14 21:41:43 UTC"}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment