|
| 1 | +# Cequel and Elasticsearch |
| 2 | +# ================================== |
| 3 | +# |
| 4 | +# https://github.com/cequel/cequel |
| 5 | + |
| 6 | + |
| 7 | +$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) |
| 8 | + |
| 9 | +require 'pry' |
| 10 | +Pry.config.history.file = File.expand_path('/tmp/elasticsearch_development.pry', __FILE__) |
| 11 | + |
| 12 | +require 'benchmark' |
| 13 | +require 'logger' |
| 14 | + |
| 15 | +require 'ansi/core' |
| 16 | +require 'cequel' |
| 17 | + |
| 18 | +require 'elasticsearch/model' |
| 19 | +require 'elasticsearch/model/callbacks' |
| 20 | + |
| 21 | +require 'rake' |
| 22 | + |
| 23 | +# Load default tasks from Cequel |
| 24 | +# |
| 25 | +spec = Gem::Specification.find_by_name 'cequel' |
| 26 | +load "#{spec.gem_dir}/lib/cequel/record/tasks.rb" |
| 27 | + |
| 28 | +# Cassandra connection settings |
| 29 | +# |
| 30 | +cequel_config = { |
| 31 | + host: '127.0.0.1', |
| 32 | + port: 9042, |
| 33 | + keyspace: 'cequel_test', |
| 34 | + max_retries: 3, |
| 35 | + retry_delay: 1, |
| 36 | + replication: { |
| 37 | + class: 'SimpleStrategy', |
| 38 | + replication_factor: 1 |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +# Elastic config |
| 43 | +# |
| 44 | +elastic_config = { |
| 45 | + host: 'localhost:9200', |
| 46 | + log: true |
| 47 | +} |
| 48 | + |
| 49 | +connection = Cequel.connect cequel_config |
| 50 | +Cequel::Record.connection = connection |
| 51 | + |
| 52 | +Elasticsearch::Model.client = Elasticsearch::Client.new elastic_config |
| 53 | + |
| 54 | + |
| 55 | +class Article |
| 56 | + include Cequel::Record |
| 57 | + |
| 58 | + key :id, :int |
| 59 | + |
| 60 | + column :title, :text |
| 61 | + column :published_at, :timestamp |
| 62 | + |
| 63 | + def as_indexed_json(options = {}) |
| 64 | + as_json(except: [:id, :_id]) |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | +Article.__send__ :include, Elasticsearch::Model |
| 69 | +Article.__send__ :include, Elasticsearch::Model::Callbacks |
| 70 | + |
| 71 | +# Initialize Cassandra and synchronize schema |
| 72 | +# |
| 73 | +Rake.application['cequel:reset'].invoke |
| 74 | +Article.synchronize_schema |
| 75 | + |
| 76 | +Article.delete_all |
| 77 | +Article.new(id: 1, title: 'Foo').save! |
| 78 | +Article.new(id: 2, title: 'Bar').save! |
| 79 | + |
| 80 | +client = Elasticsearch::Client.new elastic_config |
| 81 | + |
| 82 | +client.indices.delete index: 'articles' rescue nil |
| 83 | + |
| 84 | + |
| 85 | +client.bulk index: 'articles', |
| 86 | + type: 'article', |
| 87 | + body: Article.all.map { |a| { index: { _id: a.id, data: a.attributes } } }, |
| 88 | + refresh: true |
| 89 | + |
| 90 | +Article.new(id: 3, title: 'Foo Bar').save! |
| 91 | + |
| 92 | +response = Article.search 'bar' |
| 93 | +#x = response.records |
| 94 | +#puts x.class |
| 95 | +#puts x.to_a.to_s |
| 96 | + |
| 97 | + |
| 98 | +#puts x.records.where({ :id => [3] }) |
| 99 | + |
| 100 | +# puts Benchmark.realtime { 9_875.times { |i| Article.new( id: i, title: "Foo #{i}").save! } } |
| 101 | + |
| 102 | +Pry.start(binding, prompt: lambda { |obj, nest_level, _| '> ' }, |
| 103 | + input: StringIO.new('response.records.to_a'), |
| 104 | + quiet: true) |
0 commit comments