Skip to content

Instantly share code, notes, and snippets.

@nono
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nono/9047047 to your computer and use it in GitHub Desktop.
Save nono/9047047 to your computer and use it in GitHub Desktop.
Error when paginating the results of a complex query with es-model
/home/nono/.gem/ruby/2.1.0/bundler/gems/elasticsearch-rails-fc966380167b/elasticsearch-model/lib/elasticsearch/model/response/pagination.rb:57:in `[]': no implicit conversion of Symbol into Integer (TypeError)
from /home/nono/.gem/ruby/2.1.0/bundler/gems/elasticsearch-rails-fc966380167b/elasticsearch-model/lib/elasticsearch/model/response/pagination.rb:57:in `offset_value'
from /home/nono/.gem/ruby/2.1.0/bundler/gems/elasticsearch-rails-fc966380167b/elasticsearch-model/lib/elasticsearch/model/response/pagination.rb:23:in `offset_value'
from /home/nono/.gem/ruby/2.1.0/gems/kaminari-0.15.1/lib/kaminari/models/page_scope_methods.rb:38:in `current_page'
from ./es-page.rb:44:in `<main>'
#!/usr/bin/env ruby
require 'logger'
require 'ansi/core'
require 'jbuilder'
require 'sqlite3'
require 'active_record'
require 'kaminari'
require 'elasticsearch/model'
ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: ":memory:" )
ActiveRecord::Schema.define(version: 1) do
create_table :people do |t|
t.string :first_name, :last_name
t.timestamps
end
end
Kaminari::Hooks.init
class Person < ActiveRecord::Base
include Elasticsearch::Model
end
Person.create first_name: 'John', last_name: 'Smith'
Person.create first_name: 'Robert', last_name: 'Smith'
Person.import(force: true)
Elasticsearch::Model.client.indices.refresh
query = Jbuilder.encode do |json|
json.query do
json.simple_query_string do
json.query "John OR Will"
end
end
end
response = Person.search query
puts response.results.current_page
source 'https://rubygems.org'
gem "ansi"
gem "jbuilder"
gem "sqlite3"
gem "activerecord"
gem "kaminari"
gem "elasticsearch-model", :github => "elasticsearch/elasticsearch-rails"
GIT
remote: git://github.com/elasticsearch/elasticsearch-rails.git
revision: fc966380167b4dedd07380c4bd6b4a0c40351364
specs:
elasticsearch-model (0.1.0.rc1)
activesupport (> 3)
elasticsearch (> 0.4)
hashie
GEM
remote: https://rubygems.org/
specs:
actionpack (4.0.2)
activesupport (= 4.0.2)
builder (~> 3.1.0)
erubis (~> 2.7.0)
rack (~> 1.5.2)
rack-test (~> 0.6.2)
activemodel (4.0.2)
activesupport (= 4.0.2)
builder (~> 3.1.0)
activerecord (4.0.2)
activemodel (= 4.0.2)
activerecord-deprecated_finders (~> 1.0.2)
activesupport (= 4.0.2)
arel (~> 4.0.0)
activerecord-deprecated_finders (1.0.3)
activesupport (4.0.2)
i18n (~> 0.6, >= 0.6.4)
minitest (~> 4.2)
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
ansi (1.4.3)
arel (4.0.2)
atomic (1.1.14)
builder (3.1.4)
elasticsearch (1.0.0)
elasticsearch-api (= 1.0.0)
elasticsearch-transport (= 1.0.0)
elasticsearch-api (1.0.0)
multi_json
elasticsearch-transport (1.0.0)
faraday
multi_json
erubis (2.7.0)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
hashie (2.0.5)
i18n (0.6.9)
jbuilder (2.0.2)
activesupport (>= 3.0.0)
multi_json (>= 1.2.0)
kaminari (0.15.1)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
minitest (4.7.5)
multi_json (1.8.4)
multipart-post (2.0.0)
rack (1.5.2)
rack-test (0.6.2)
rack (>= 1.0)
sqlite3 (1.3.8)
thread_safe (0.1.3)
atomic
tzinfo (0.3.38)
PLATFORMS
ruby
DEPENDENCIES
activerecord
ansi
elasticsearch-model!
jbuilder
kaminari
sqlite3
@karmi
Copy link

karmi commented Feb 17, 2014

#42 : Due to the problem in the gem, you have to pass the query as a Hash with symbolised keys to the search method:

response = Person.search MultiJson.load(query, symbolize_keys: true)

This will be fixed.
#44 : You have to call the page method, a la> response.page(1).results.to_a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment