toptal / chewy Goto Github PK
View Code? Open in Web Editor NEWHigh-level Elasticsearch Ruby framework based on the official elasticsearch-ruby client
License: MIT License
High-level Elasticsearch Ruby framework based on the official elasticsearch-ruby client
License: MIT License
For me, query_spec.rb:148
currently fails. It seems that my version of elasticsearch (1.2.1) will only insert the key_as_string
propery if the underlying field (rating
) is of type "date". I see it passing in travis, so I guess it may be related to elasticsearch.
1) Chewy::Query#aggregations results should == {"ratings"=>{"buckets"=>[{"key"=>0, "key_as_string"=>"0", "doc_count"=>4}, {"key"=>1, "key_as_string"=>"1", "doc_count"=>3}, {"key"=>2, "key_as_string"=>"2", "doc_count"=>3}]}}
Failure/Error: } }
expected: {"ratings"=>{"buckets"=>[{"key"=>0, "key_as_string"=>"0", "doc_count"=>4}, {"key"=>1, "key_as_string"=>"1", "doc_count"=>3}, {"key"=>2, "key_as_string"=>"2", "doc_count"=>3}]}}
got: {"ratings"=>{"buckets"=>[{"key"=>0, "doc_count"=>4}, {"key"=>1, "doc_count"=>3}, {"key"=>2, "doc_count"=>3}]}} (using ==)
Diff:
@@ -1,2 +1,2 @@
-"ratings" => {"buckets"=>[{"key"=>0, "key_as_string"=>"0", "doc_count"=>4}, {"key"=>1, "key_as_string"=>"1", "doc_count"=>3}, {"key"=>2, "key_as_string"=>"2", "doc_count"=>3}]}
+"ratings" => {"buckets"=>[{"key"=>0, "doc_count"=>4}, {"key"=>1, "doc_count"=>3}, {"key"=>2, "doc_count"=>3}]}
[139] milaap_webapp ยป FundraiserCampaignsIndex.filter{ match_all }.to_a
=> [
[0] #<FundraiserCampaignsIndex::FundraiserCampaign:0x108c9050 @attributes={"motivation"=>"I am cycling to help former Devadasi women start independent businesses", "user"=>{"first_name"=>"Mayukh", "last_name"=>"Choudhury"}, "project"=>"Help former Devadasi women start independent businesses", "id"=>"39", "_score"=>1.0, "_explanation"=>nil}>,
[1] #<FundraiserCampaignsIndex::FundraiserCampaign:0x108c8678 @attributes={"motivation"=>"I am pledging my birthday to help former Devadasi women start independent businesses", "user"=>{"first_name"=>"Satya", "last_name"=>"Kothimangalam"}, "project"=>"Help former Devadasi women start independent businesses", "id"=>"41", "_score"=>1.0, "_explanation"=>nil}>,
[2] #<FundraiserCampaignsIndex::FundraiserCampaign:0x108c7cc8 @attributes={"motivation"=>"I am fundraising to revive village economy through traditional crafts", "user"=>{"first_name"=>"mayank", "last_name"=>"choudhury"}, "project"=>"Revive village economy through traditional crafts", "id"=>"46", "_score"=>1.0, "_explanation"=>nil}>,
]
Now notice that last result of match all is
#<FundraiserCampaignsIndex::FundraiserCampaign:0x108c7cc8 @attributes={"motivation"=>"I am fundraising to revive village economy through traditional crafts", "user"=>{"first_name"=>"mayank", "last_name"=>"choudhury"}, "project"=>"Revive village economy through traditional crafts", "id"=>"46", "_score"=>1.0, "_explanation"=>nil}>,
which includes "mayank".
But when I search for "mayank" I get empty array like below
FundraiserCampaignsIndex.filter{ name == "mayank" }.to_a
=> []
I can't figure out how to define a child/parent mapping in chewy. The following gives a routing error, presumably because column isn't being given a parent _id.
define_type Table do
field :_id, type: 'integer', value: { id }
field :user_ids, value: -> { server.users_for_table(self) }
end
define_type Column.includes(:table) do
root _parent: { type: 'table' } do
field :_parent, value: -> { table.id }
end
end
I can't find any examples of parent ids being given anywhere other than the url (like the examples here http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/indexing-parent-child.html). Is it possible to specify a parent id in chewy currently or does there need to be some code added?
Something similar to:
module Chewy
class Query
def took
_response['took']
end
end
end
Basically I want to do something like
Index.reset!.types(:my_type)
Does this functionality exist? If it does not, how could I implement it?
I'm getting the following when trying to use chew in my specs:
Failure/Error: specify { expect { product.save! }.to update_index(ProductsIndex::Product) }
You must pass an argument rather than a block to use the provided matcher (update index ProductsIndex::Product), or the matcher must implement `supports_block_expectations?`.
I also noticed some deprecation warnings. Is there an edge version that's more compatible with rails 4 and rspec 3?
First I'd like to say I'm really happy with this gem!
For me, it would be really awesome if some support for search suggesters is added:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters.html
I think support like facets/aggregations would be sufficient for most users (so just the request part). Is this already on your roadmap by any chance?
I want to integrate chewy with a multi-lang site. What do you propose as a solution? I use globalize
After performing a query(either using term or multi_match), if I call to_a or just iterate over the scope, I noticed that there are nil objects in the collection. I'm not sure if this is a Chewy issue or ES issue. Apologies if this is an ES issue or a me not understanding ES issue. Thanks for your help!
Hi
Would you mind explaining when an index is updated? When do I have to use the urgent: true
option?
I have an Article
model with this line:
update_index("global#news") { self if is_news? && published? }
Whenever I create or updated an article it indexes it correctly.
I also have a Comment
model with this logic:
update_index("global#discussion") { commentable if discussion_comment? }
So, if a comment belongs to a Discussion
(commentable is polymorphic and can also be Article
) it should reindex the Discussion
.
In the Comment
example the index is NOT updated, unless I add the urgent: true
option. Why?
Also, my tests for the Comment
model passes regardless of the urgent
option.
...
expect { comment.save! }.to update_index("global#discussion")
...
That passes but the index is not updated in reality.
I am using Carrierwave for the s3 file upload and have elasticsearch-mapper-attachments plugin installed for my elasticsearch
# app/chewy/candidate_index.rb
class CandidateIndex < Chewy::Index
define_type Candidate.includes({:campaign_invitation => :labels}, :campaign_response_details) do
field :resume, type: 'attachment', value: -> (candidate) {
if candidate.resume.present?
Base64.encode64(open(candidate.resume.url) { |doc| doc.read })
else
""
end
}
end
end
# app/models/candidate.rb
class Candidate < ActiveRecord::Base
mount_uploader :resume, ResumeUploader
def self.search(options)
fields = [ 'resume' ]
CandidateIndex.query(multi_match: { query: options[:keyword], fields: fields })
end
end
I want to use the elasticsearch completion suggest feature to implement autocomplete. when I do something like this:
index.suggest(mysuggest: { text: 'roy', completion: { field: 'suggest' }})
it sends this request to the elasticsearch server:
GET /console/job,table,column/_search [{"suggest":{"mysuggest":{"text":"roy","completion":{"field":"suggest"}}}}]
Since the search endpoint is used and there is no query given, I think elasticsearch just assumes a blank query (so it returns all objects in db). It also has this in the result (when I submit the query with a curl command)
{...
"suggest":{"mysuggest":[{"text":"roy","offset":0,"length":3,"options":[{"text":"Roy Lane","score":1.0}]}]}}
I want to be able to get those suggestion results, but I can't figure out any way to do it. I tried looking into the Chewy code, but I'm new to Ruby and I don't really understand what is going on.
Is there currently any way to retrieve suggestion results? If not, can someone point me to the right places in the code to do this? I think a better way to implement suggest would just be to use the _suggest endpoint of the elasticsearch server so that you are not doing a query if all you want are suggestion results.
Something like this would be good:
GET /console/job,table,column/_suggest [{"mysuggest":{"text":"roy","completion":{"field":"suggest"}}}]
Placeholder for myself to add a note.
I've got a Rails 4 project using AMS and Chewy.
If I define an AMS for default, I'm able to serialise just fine. However, if I use a custom AMS, I get an error: NoMethodError (undefined method 'read_attribute_for_serialization' for #<Chewy::Query:0x000000060c1da8>)
Code (works fine):
@search = Search.new(params)
@results = @search.query.load
render json: @results
Code (breaks):
@search = Search.new(params)
@results = @search.query.load
render json: @results, serializer: MySerializer
Both the default model serializer I wrote and MySerializer
are identical. There's nothing fancy in there either.
class MySerializer < ActiveModel::Serializer
attributes :my_attribute, :other_attribute
has_one :thing
end
I've tried to monkeypatch Chewy::Query:
module Chewy
class Query
alias_method :read_attribute_for_serialization, :send
end
end
but I just get NoMethodError (undefined method 'my_attribute' for #<Chewy::Query:0x000000069af5c8>)
My code was working fine with Rails 3.2 but it gives following error with rails 4.1
LocalJumpError - no block given (yield):
chewy (0.4.1) lib/chewy/config.rb:129:in `atomic'
chewy (0.4.1) lib/chewy.rb:50:in `atomic'
app/controllers/application_controller.rb:15:in `block in <class:ApplicationController>'
code in application controller is (line 15)
around_action { |&block| Chewy.atomic(&block) }
Keep in mind with rails 3.2 I was using around_filter and changed it to around_action according to rails4
When I query elastic search using my web browser I get the following result:
Yet, when I do the same query using chewy I get nothing:
What am I missing here?? In chewy if I remove "baker" and just query using "jimmy" then I get the result that I'm looking for. Is there something that I need to set in chewy or es config to allow multiple words to be searched?
Hi there!
So far I'm really enjoying using Chewy and it's working great, except one little nit and reading mappings.rb isn't helping me sort it out. I'm sure that I'm just missing the Right Wayโข and hopefully I can at least contribute a doc patch when I get it sorted out :)
I need to have 3 tiers in my document, which look like: Event -> Category ->>> Licenses
(licenses are attached to categories, and are important for faceting in presented views).
In plain english, an Event has a Category, and Categories have many Licenses.
The mapping works with:
define_type Event do
field :category, type: 'object' do
field :licenses, type: 'object'
end
end
However, when I want to limit what is exposed on the licenses (say the id and the name), changing it to:
field :licenses, type: 'object' do
field :id
field :name
end
I get an error because it seems the value is no longer the record value, but the ActiveRecord collection:
undefined method `id' for #<License::ActiveRecord_Associations_CollectionProxy:0x007fd30e7807b0>
Ok, so reading through I see that I may need to pass values so I changed it to:
field :licenses, type: 'object', value: ->(license) do
field :id
field :name
end
But then I get the error:
undefined method `name' for #<Category:0x007fd2f18a60a8>
This is where my understanding and expectations come undone :). When I have a value being passed in, and licenses is a collection, why is it the category and not the license being iterated?
And then I thought, perhaps it is about using the value: :some_field
syntax, so I used:
field :licenses, type: 'object' do
field :id, value: :id
field :name, value: :name
end
And then I get an error (Even if I put name: 'event' when I declare this index, it makes no difference):
Index `EventsIndex` doesn't have type named `event`
Ultimately, what I need is a document stored that looks like:
"event": {
"id": 1,
"category": {
"id" : 2,
"licenses": {
"id" : 3,
"name": "Some Name"
}
}
}
However, there are other fields in licenses
that I don't want to be present (or rather, do not need to be present)
I did finally figure out that I can return an array of hashes, but it gets awkward to manage these variables and I have the distinct feeling I'm doing something wrong.
Thanks very much,
-Jay
Hi there. I'm trying to piece together how defining and querying a multi-model index might work in Chewy. Is there an example somewhere I can check out and if not, what would a quick and dirty one look like?
Thanks!
Not sure is it correct behaviour or not
class User
...
def delete_from_index?
!searchable? || need_completion?
end
end
UsersIndex.filter{ match_all }.total_count #=> 1001
UsersIndex::User.import User.where(phone: phone) #=> true
UsersIndex.filter{ match_all }.total_count #=> 1001
UsersIndex::User.import User.where(phone: phone).to_a #=> true
UsersIndex.filter{ match_all }.total_count #=> 1000
Hi!
I have an index with multiple types in it. Now I want to make a search where I get results for each type.
At first I was thinking about using aggregations but that does not work I think, since it is bound to the result of the query.
To clarify, let's say I have three types in my index, Foo
, Bar
and Baz
.
I mean, if I search for "Lorem Ipsum" with a limit of 10 it will fetch the top 10 results and then group these by type in my aggregations. Maybe the top 5 results are of type Foo
and then 4 are Bar
and only one Baz
which is not what I want...
What I really want is 10 hits of each type. So, the most relevant Foo
the most relevant Bar
and the most relevant Baz
.
Sure, I could do one request per type to get the desired result but it would be cool if it could be done in one request (using something like msearch
I guess, https://github.com/elasticsearch/elasticsearch-ruby/blob/5dc6bc61b85cb681b2453e4b9a6afb9a35e1be98/elasticsearch-api/lib/elasticsearch/api/actions/msearch.rb).
Does Chewy support something like this?
I have a model with a one-to-many association. It's indexed on [:start], ie
field :start, value: ->(thing) { thing.bars.map(&:start) }
Foo
has_many Bars
Bar
belongs_to Foo
Each bar
has a field called start
which is a DateTime
. I want to get any foo
objects with at least one bar
that occurs in the future. So that should not include any foo
s with no bar
s or bar
s with only past dates.
Using the Chewy syntax, I'd like to query ElasticSearch to retrieve all foo
objects with at least one bar
where start > Time.zone.now
.
I've tried a few ways but it's not obvious how to do that. (not sure if this an "issue" but a usage question!)
Hi there,
I'm trying paginate my query results using will_paginate, and despite commit 64130ad adding #paginate
and #page
methods to the query results, I'm getting undefined method errors when I try to use them.
defined?(::WillPaginate)
returns "constant"
, so I'm not sure why the will_paginate module is not being loaded. Gemfile.lock
lists chewy (0.5.2) and will_paginate (3.0.5). Any ideas?
Seems like those two options do not work as expected.
ััะฒะฐะบ there?
field :phone_number, index_analyzer: 'phone', search_analyzer: 'phone' - ะฝะต ัะฐะฑะพัะฐะตั
field :phone_number, analyzer: 'phone' - ะฐ ััะพ ัะฐะฑะพัะฐะตั
ะฒัะต-ัะฐะบะธ ัะตั ะฝะต ัะฐะบ ั ััะธะผะธ ะพะฟัะธัะผะธ
Plus I would want to see exception if I set something wrong in my options.
Hi there
I'm just getting started with your chewy gem because it looks great and I'd like to try it.
A question. Is it possible to hook into the update_index action and perform the updating inside an async sidekiq job?
Example:
class User < ActiveRecord::Base
after_save { Indexer.perform_async(:index, self.class.name, self.id) }
......
end
And a Sidekiq worker named "Indexer":
class Indexer
include Sidekiq::Worker
def perform(operation, model_name, record_id)
#update elasticsearch index here...
end
end
I'm curious as to why you chose to create an index named "Users" in the examples. Elasticsearch documentation describes Indexes (Indicies) as the equivalence of a database in a RDBMS.
Isn't it a bit weird/awkward to have a database (index) called "users" with only a single table (type) called "user"?
Related docs can be found here:
http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_indexing_employee_documents.html
I'm attempting to define an index like this:
module Spree
class ProductsIndex < Chewy::Index
define_type Product
end
end
This shows this stacktrace:
NameError: wrong constant name Spree::Product
from /Users/ryanbigg/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/bundler/gems/chewy-bf2e5bc806b0/lib/chewy/type.rb:14:in `const_set'
from /Users/ryanbigg/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/bundler/gems/chewy-bf2e5bc806b0/lib/chewy/type.rb:14:in `new'
from /Users/ryanbigg/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/bundler/gems/chewy-bf2e5bc806b0/lib/chewy/index.rb:80:in `define_type'
from /Users/ryanbigg/Projects/test/spree_test/app/chewy/spree/products_index.rb:3:in `<class:ProductsIndex>'
from /Users/ryanbigg/Projects/test/spree_test/app/chewy/spree/products_index.rb:2:in `<module:Spree>'
from /Users/ryanbigg/Projects/test/spree_test/app/chewy/spree/products_index.rb:1:in `<top (required)>'
Similarly,
class ProductsIndex < Chewy::Index
define_type Spree::Product
end
Yields the same error. I think this is because my constant is namespaced inside of a module.
Hello
I wanted to use #boost_factor
and #decay
functions as specified in the readme:
UsersIndex.boost_factor(5, filter: {term: {type: 'Expert'}})
Howver, those methods are not defined on the index. In my index I had to add this delegator to make it work:
singleton_class.delegate :boost_factor, :decay, to: :all
I guess that is just a mistake and it should be delegated by default?
Thank you for a great gem :)
Is there a way to add the timeout option?
{
"timeout": "5000ms",
"query": { ... }
}
I have installed the elastic search and jdk 1.7 in windows.
When I start elastic search , Its gets started.
C:\elasticsearch-1.1.1\bin>service.bat start
The service 'elasticsearch-service-x64' has been started
http://localhost:9200/ returns proper json in browser
{
"status" : 200,
"name" : "Ever",
"version" : {
"number" : "1.1.1",
"build_hash" : "f1585f096d3f3985e73456debdc1a0745f512bbc",
"build_timestamp" : "2014-04-16T14:27:12Z",
"build_snapshot" : false,
"lucene_version" : "4.7"
},
"tagline" : "You Know, for Search"
}
I have set my JAVA_HOME to
'C:\Program Files\Java\jdk1.7.0_55\jre'
And my java version
java -version
java version "1.7.0_55"
Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)
But when I am trying to do search or import the database to elastic search
FundraiserCamapignsIndex.import
I gives this error.
Faraday::Error::ConnectionFailed: getaddrinfo: No such host is known.
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:762:in `initialize'
Can you tell what I am missing here.
I have a situation where a nested field needs a date from the parent to do a dynamic calculation, and this works fine with a hash return but I need to apply analyzers to the nested fields and templates are not working well enough :(
Imagine something like this:
define_type Event do
field :start_date
field :rooms, type: 'object' do
field :name
field :is_available, value: -> { is_available?(event.start_date) }
end
end
The event.start_date
part is what I can't sort out. Previously I was simply returning a hash from the rooms
block to set the is_available flag since the hash gets the parent object and not the rooms
relationship.
Is there a way to do this without the hash method? The fields/base#compose
method calls instance_exec with the object, but the object doesn't have any awareness of the calling method above it and now I'm a bit lost on how to proceed.
Couldn't find any info on how to use facets in chewy... Am i missing something or it does not supported?
I have a basic index on 3 fields and I'm building a simple search text box that searches across all 3 fields and should return results if any of the 3 fields have results.
When I do the following query:
scope = PostsIndex.query(term: {body: 'hover', title: 'hover', html_block: 'hover'}).query_mode(:should)
scope.total_count
I get this error:
Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[S1Eit6J9Sq6SHSiqrXsgjg][posts][3]: SearchParseException[[posts][3]: query[body:hover],from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"term\":{\"body\":\"hover\",\"title\":\"hover\",\"html_block\":\"hover\"}}}]]]; nested: SearchParseException[[posts][3]: query[body:hover],from[-1],size[-1]: Parse Failure [No parser for element [html_block]]]; }{[S1Eit6J9Sq6SHSiqrXsgjg][posts][4]: SearchParseException[[posts][4]: query[body:hover],from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"term\":{\"body\":\"hover\",\"title\":\"hover\",\"html_block\":\"hover\"}}}]]]; nested: SearchParseException[[posts][4]: query[body:hover],from[-1],size[-1]: Parse Failure [No parser for element [html_block]]]; }{[S1Eit6J9Sq6SHSiqrXsgjg][posts][0]: SearchParseException[[posts][0]: query[body:hover],from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"term\":{\"body\":\"hover\",\"title\":\"hover\",\"html_block\":\"hover\"}}}]]]; nested: SearchParseException[[posts][0]: query[body:hover],from[-1],size[-1]: Parse Failure [No parser for element [html_block]]]; }{[S1Eit6J9Sq6SHSiqrXsgjg][posts][1]: SearchParseException[[posts][1]: query[body:hover],from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"term\":{\"body\":\"hover\",\"title\":\"hover\",\"html_block\":\"hover\"}}}]]]; nested: SearchParseException[[posts][1]: query[body:hover],from[-1],size[-1]: Parse Failure [No parser for element [html_block]]]; }{[S1Eit6J9Sq6SHSiqrXsgjg][posts][2]: SearchParseException[[posts][2]: query[body:hover],from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"term\":{\"body\":\"hover\",\"title\":\"hover\",\"html_block\":\"hover\"}}}]]]; nested: SearchParseException[[posts][2]: query[body:hover],from[-1],size[-1]: Parse Failure [No parser for element [html_block]]]; }]","status":400}
I figured out I could structure the query this way:
scope = PostsIndex.query(term: {body: 'hover'}).query(term: {title: 'hover'}).query(term: {html_block: 'hover'}).query_mode(:should)
scope.total_count
I'm new to Chewy, so maybe there is a better of way of accomplishing this, but I couldn't find a clearer way in the documentation.
Also, so far chewy rocks btw! Great work!
Hi guys,
If have a rails model called Client, and if I define a chewy index for this model for example:
# client_index.rb
class ClientIndex < Chewy::Index
define_type Client do
field :name
end
end
# client.rb
class Client < ActiveRecord::Base
update_index('client#client') { self }
end
There is a conflict with the elasticsearch 'client' method that you are using
# chewy/lib/chewy/config.rb
def client
Thread.current[:chewy_client] ||= ::Elasticsearch::Client.new configuration
end
This the exception
jruby-1.7.11 :003 > ClientsIndex.client.import
NoMethodError: undefined method `indices' for ClientsIndex::Client:Class
from /Users/joselo/.rvm/gems/jruby-1.7.11@instafac/gems/chewy-0.5.0/lib/chewy/index/actions.rb:15:in `exists?'
# chewy/lib/chewy/index/actions.rb
def exists?
client.indices.exists(index: index_name)
end
It's cool that I can do chaining and stuff with this plugin but I felt like I had to learn yet another DSL when using this and I was already new to elasticsearch so I was a bit annoyed that i had to learn both.
I really like how simple the "elasticsearch-model" gem handles search, you literally just pass it the same json/hash that you would if you were using Elasticsearchs' interactive console Sense.
I could easily copy snippets from elasticsearchs' website and use them is my code with zero effort.
I'd really like to see chewy implement the same thing.
I have the only one index on Tour model:
class ToursIndex < Chewy::Index
define_type Tour.public.includes(:tags, :assets, :prices, days: [:locations]) do
...
On the Tour model i have this:
update_index('tours') { self }
But index does not get updated!
If i do
rake chewy:update[tours]
Index is updated successfuly.
I have tried:
update_index('tours') { self }
update_index('tours#tour') { self }
update_index('tours') { Tour }
Nothing...
When i save tour there is no log entry from chewy...
Any suggestion why index updating does not occur?
Given the following index and definitions:
class AwardsIndex < Chewy::Index
define_type Award
define_type Award::Category
end
when trying AwardsIndex.import will get error
NameError: uninitialized constant AwardsIndex::Award::Category
everything will work fine if just defining Award or Award::Category without the other, but get the error when defining both.
I'd like to work on this library, but I'm not sure where to start. Is there anything specific and maybe smaller that would server as a good starting point?
I noticed that you have a list of 'coming soon' items at the bottom
Hello guys, thanks for your great work!
It seems to me that you missed one very important option. We should add ability to define filters as a top level query option. Because currently I cannot exclude my filters from facets. Please read more about this issue here: http://stackoverflow.com/questions/15649179/how-to-exclude-a-filter-from-a-facet
It seems really urgent for me.
I have a nested item specified in my mappings, eg:
field :user_qualifications, type: 'nested' do
field :qualification_level, value: ->(o) { o.qualification.qualification_level }
end
When updating the index by saving the model and using update_index
, it indexes as expected. When using WhateverIndex.reset!
in Rails console, it ignores my mappings and indexes everything in the nested model, ie. id, created_at, updated_at
, etc. instead of just indexing qualification_level
Wondering if chewy can handle multiple elasticsearch hosts? I know Elasticsearch::Transport::Client can, but it seems like chewy barfs when trying to add multiple hosts.
Any thoughts? workarounds?
Greetings. I'm having trouble issuing searches within my controller spec.
Here's the spec:
describe 'GET /companies/:company_id/products/search' do
before do
@tenant.scope_schema do
create(:product, company: @company, name: 'Battlefield 4')
create(:product, company: @company, name: 'League of Legends')
create(:product, company: @company, name: 'Coors Light')
ProductsIndex::Product.import
end
get :search, company_id: @company.id, q: 'League'
end
it 'should respond successfully' do
expect(response).to be_successful
end
end
and the controller action:
def search
@results = ProductsIndex::Product
.query(term: { _all: params[:q] })
.filter{ tenant_id == current_tenant.id }
.filter{ company_id == company.id }
.to_a
binding.pry
end
Which yields the following error:
1) Api::V1::ProductsController GET /companies/:company_id/products/search should respond successfully
Failure/Error: get :search, company_id: @company.id, q: 'League'
Elasticsearch::Transport::Transport::Errors::BadRequest:
[400] {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[uzV7mCJyQ0eN8xtjOHlhbg][test_products][3]: SearchParseException[[test_products][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"term\":{\"_all\":\"League\"}},\"filter\":{\"and\":[{\"term\":{\"tenant_id\":{\"name\":\"current_tenant.id\",\"args\":[]}}},{\"term\":{\"company_id\":{\"name\":\"company.id\",\"args\":[]}}}]}}}}]]]; nested: QueryParsingException[[test_products] [term] filter does not support [name]]; }{[uzV7mCJyQ0eN8xtjOHlhbg][test_products][4]: SearchParseException[[test_products][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"term\":{\"_all\":\"League\"}},\"filter\":{\"and\":[{\"term\":{\"tenant_id\":{\"name\":\"current_tenant.id\",\"args\":[]}}},{\"term\":{\"company_id\":{\"name\":\"company.id\",\"args\":[]}}}]}}}}]]]; nested: QueryParsingException[[test_products] [term] filter does not support [name]]; }{[uzV7mCJyQ0eN8xtjOHlhbg][test_products][1]: SearchParseException[[test_products][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"term\":{\"_all\":\"League\"}},\"filter\":{\"and\":[{\"term\":{\"tenant_id\":{\"name\":\"current_tenant.id\",\"args\":[]}}},{\"term\":{\"company_id\":{\"name\":\"company.id\",\"args\":[]}}}]}}}}]]]; nested: QueryParsingException[[test_products] [term] filter does not support [name]]; }{[uzV7mCJyQ0eN8xtjOHlhbg][test_products][2]: SearchParseException[[test_products][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"term\":{\"_all\":\"League\"}},\"filter\":{\"and\":[{\"term\":{\"tenant_id\":{\"name\":\"current_tenant.id\",\"args\":[]}}},{\"term\":{\"company_id\":{\"name\":\"company.id\",\"args\":[]}}}]}}}}]]]; nested: QueryParsingException[[test_products] [term] filter does not support [name]]; }{[uzV7mCJyQ0eN8xtjOHlhbg][test_products][0]: SearchParseException[[test_products][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"term\":{\"_all\":\"League\"}},\"filter\":{\"and\":[{\"term\":{\"tenant_id\":{\"name\":\"current_tenant.id\",\"args\":[]}}},{\"term\":{\"company_id\":{\"name\":\"company.id\",\"args\":[]}}}]}}}}]]]; nested: QueryParsingException[[test_products] [term] filter does not support [name]]; }]","status":400}
# /projects/chewy/lib/chewy/query.rb:807:in `block in _response'
# /projects/chewy/lib/chewy/query.rb:805:in `_response'
# /projects/chewy/lib/chewy/query.rb:817:in `_results'
# /projects/chewy/lib/chewy/query.rb:831:in `_collection'
# /projects/chewy/lib/chewy/query.rb:29:in `each'
# ./app/controllers/api/v1/products_controller.rb:41:in `to_a'
# ./app/controllers/api/v1/products_controller.rb:41:in `search'
# ./app/models/tenant.rb:29:in `scope_schema'
# ./app/controllers/api/v1/base_controller.rb:24:in `scope_current_tenant'
# ./spec/controllers/api/v1/products_controller_spec.rb:129:in `block (3 levels) in <top (required)>'
# -e:1:in `<main>'
When checking my test_products index in the browser I see no documents. What am I doing wrong?
I am using the chewy gem to tie ES to my rails app.I am new to chewy, so i am facing a problem when i try to index a field of my model. The field is a text field in the DB which i serialize as a Hash in my model. The hash is dynamic and might have 0 to n elements in it in the form. The field name is items Any help would be much appreciated.
{"0"=>{"property"=>"value","property"=>"value"},"1"=>{"property"=>"value","property"=>"value"}.......}
class ModelNameIndex < Chewy::Index
define_type ModelName do
field :user_id, type: 'integer'
field :enduser_id, type: 'integer'
field :items, type: 'object'
field :created, type: 'date', include_in_all: false,
value: ->{ created_at }
end
end
class ModelName < ActiveRecord::Base
update_index('IndexName#name') { self }
belongs_to :user
serialize :items, Hash
end
I have Tour with Tags (M:M relation)
I have defined index field wich is array of tag ids
field :tags, value: ->(tour) { tour.tags.map(&:id) }
And inside of "join" model updating index like this:
class TourTag < ActiveRecord::Base
update_index('tours', urgent: true) { tour }
So if relation added or deleted it should update Tour with new tags(index for this tour should fully update). The result is totaly unexpected, on tag creation indexed tags array contain only last created tag, but on delete it is updated properly...
If I define an index like it is done in the example:
define_type User.active.includes(:country, :badges, :projects) do
And an active user changes to inactive, it is still included in the index. Is this an issue or is this wanted behaiviour? Because in my opinion, since there is no offensive hint in the docs, this i not very intuitional.
Hi!
I'm trying to move away from (re)Tire to some other gem and found Chewy. I wonder if it is possible to create a boosting query somehow?
What I want to do is demote documents that does not match a specific query.
For example, lets say I want articles written by a certain department to get a bump in the score. I could use a boosting query (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html) to demote all blog posts NOT written by the department.
query: {
boosting: {
positive: [{
filtered: {
query: {
bool: {
should: [some_query_that_matches_stuff],
must: [some_other_query],
minimum_number_should_match: 1
}
},
filter: {}
}
}],
negative: {
filtered: {
filter: {
and: [{ not: { term: { department_id: Department.important.id } } }]
}
}
},
negative_boost: 0.1
}
},
filter: {},
facets: {}
}
Is there a way to accomplish something like this in Chewy?
I've got an index that is made up of two different models. The index already contains a _type
field, populated with the name of the model as appopriate. I can't see a way to query against that field though. Am I missing something blindingly obvious? If not, do I need to build a new Node
to implement this: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-type-filter.html
I need to write either a query with a function_score, which doesn't seem to be implemented yet.
I have a metadata column that I wish to index and search. What is the best strategy for doing this?
Anyone seeing this? During index create, chewy is complaining:
Ethon::Errors::InvalidOption: The option: disable_ssl_peer_verification is invalid.
Please try ssl_verifypeer instead of disable_ssl_peer_verification.
from /Users/yan/.rvm/gems/ruby-2.1.1@reverb/gems/typhoeus-0.6.8/lib/typhoeus/hydra/runnable.rb:17:in `block in run'
from /Users/yan/.rvm/gems/ruby-2.1.1@reverb/gems/chewy-0.4.1/lib/chewy/index/actions.rb:61:in `create!'
Seems like some option it's passing to typhoeus? Maybe I have a different version than it expects?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.