Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-model index example #34

Closed
gangster opened this issue Jul 3, 2014 · 5 comments
Closed

Multi-model index example #34

gangster opened this issue Jul 3, 2014 · 5 comments

Comments

@gangster
Copy link

gangster commented Jul 3, 2014

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!

@pyromaniac
Copy link
Contributor

Hey! The basic thing is: one model - one type defined inside the index. Like:

class AutocompleteIndex < Chewy::Index
  define_type City do
    field :name
  end

  define_type Country do
    field :name
  end
end

AutocompleteIndex.query(match: {name: 'Singapore'}).to_a # will return country and city

# And don't forget about updating:
class City < ActiveRecord::Base
  update_index 'autocomplete#city', :self
end

class Country < ActiveRecord::Base
  update_index 'autocomplete#country', :self
end

@pyromaniac
Copy link
Contributor

And try to check out http://www.toptal.com/ruby-on-rails/elasticsearch-for-ruby-on-rails-an-introduction-to-chewy as well

@gangster
Copy link
Author

gangster commented Jul 4, 2014

Awesome, thank you!

@gangster
Copy link
Author

gangster commented Jul 5, 2014

One more question, and I apologize in advance, I am new to Elastic Search so I'm still trying to get my head around it, and how Chewy's interface into it works. So, given the following index:

class OrganizationsIndex < Chewy::Index
  settings analysis: {
    analyzer: {
      standard: {
        type: 'standard'
      }
    }
  }
  define_type ::Organization.all do
    field :id, :name, :slug, :website, :city, :state, :zip, :country
    field :phone, :mission
    field :created_at, type: :date
    field :updated_at, type: :date
    field :tags, value: ->{ tags.map(&:name) }
  end
end

Say I have an array of string values for tags. And I want to filter Organizations based on the presence of a specific tag. For this example, say the tags are ["one", "two", "three"] and I want fetch all Organizations in the index that contain the tag "one". How is that done with Chewy?

Thanks again!

@pyromaniac
Copy link
Contributor

OrganizationsIndex.filter { tags == ['one'] }.to_a
OrganizationsIndex.filter { tags == variable }.to_a
OrganizationsIndex.filter { tags == o{variable} }.to_a # in case of variable is not in current scope
OrganizationsIndex.filter(terms: {tags: ['one']}).to_a
OrganizationsIndex.filter(terms: {tags: variable}).to_a

All the examples are equivalent and produces terms filter.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants