Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:13:19+00:00 2026-06-08T14:13:19+00:00

I’m using ElasticSearch with Tire to index and search some ActiveRecord models, and I’ve

  • 0

I’m using ElasticSearch with Tire to index and search some ActiveRecord models, and I’ve been searching for the “right” way to index and search associations. I haven’t found what seems like a best practice for this, so I wanted to ask if anyone has an approach that they think works really well.

As an example setup (this is made up but illustrates the problem), let’s say we have a book, with chapters. Each book has a title and author, and a bunch of chapters. Each chapter has text. We want to index the book’s fields and the chapters’ text so you can search for a book by author, or for any book with certain words in it.

class Book < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  has_many :chapters

  mapping do
    indexes :title, :analyzer => 'snowball', :boost => 100
    indexes :author, :analyzer => 'snowball'
    indexes :chapters, type: 'object', properties: {
      chapter_text: { type: 'string', analyzer: 'snowball' }
    }
  end
end

class Chapter < ActiveRecord::Base
  belongs_to :book
end

So then I do the search with:

s = Book.search do
  query { string query_string }
end

That doesn’t work, even though it seems like that indexing should do it. If instead I index:

indexes :chapters, :as => 'chapters.map{|c| c.chapter_text}.join('|'), :analyzer => 'snowball'

That makes the text searchable, but obviously it’s not a nice hack and it loses the actual associated object. I’ve tried variations of the searching, like:

s = Book.search do
  query do
    boolean do
      should { string query_string }
      should { string "chapters.chapter_text:#{query_string}" }
    end
  end
end

With no luck there, either. If anyone has a good, clear example of indexing and searching associated ActiveRecord objects using Tire, it seems like that would be a really good addition to the knowledge base here.

Thanks for any ideas and contributions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:13:21+00:00Added an answer on June 8, 2026 at 2:13 pm

    The support for ActiveRecord associations in Tire is working, but requires couple of tweaks inside your application. There’s no question the library should do better job here, and in the future it certainly will.

    That said, here is a full-fledged example of Tire configuration to work with Rails’ associations in elasticsearch: active_record_associations.rb

    Let me highlight couple of things here.

    Touching the parent

    First, you have to ensure you notify the parent model of the association about changes in the association.

    Given we have a Chapter model, which “belongs to” a Book, we need to do:

    class Chapter < ActiveRecord::Base
      belongs_to :book, touch: true
    end
    

    In this way, when we do something like:

    book.chapters.create text: "Lorem ipsum...."
    

    The book instance is notified about the added chapter.

    Responding to touches

    With this part sorted, we need to notify Tire about the change, and update the elasticsearch index accordingly:

    class Book < ActiveRecord::Base
      has_many :chapters
      after_touch() { tire.update_index }
    end
    

    (There’s no question Tire should intercept after_touch notifications by itself, and not force you to do this. It is, on the other hand, a testament of how easy is to work your way around the library limitations in a manner which does not hurt your eyes.)

    Proper JSON serialization in Rails < 3.1

    Despite the README mentions you have to disable automatic “adding root key in JSON” in Rails < 3.1, many people forget it, so you have to include it in the class definition as well:

    self.include_root_in_json = false
    

    Proper mapping for elasticsearch

    Now comes the meat of our work — defining proper mapping for our documents (models):

    mapping do
      indexes :title,      type: 'string', boost: 10, analyzer: 'snowball'
      indexes :created_at, type: 'date'
    
      indexes :chapters do
        indexes :text, analyzer: 'snowball'
      end
    end
    

    Notice we index title with boosting, created_at as “date”, and chapter text from the associated model. All the data are effectively “de-normalized” as a single document in elasticsearch (if such a term would make slight sense).

    Proper document JSON serialization

    As the last step, we have to properly serialize the document in the elasticsearch index. Notice how we can leverage the convenient to_json method from ActiveRecord:

    def to_indexed_json
      to_json( include: { chapters: { only: [:text] } } )
    end
    

    With all this setup in place, we can search in properties in both the Book and the Chapter parts of our document.

    Please run the active_record_associations.rb Ruby file linked at the beginning to see the full picture.

    For further information, please refer to these resources:

    • https://github.com/karmi/railscasts-episodes/commit/ee1f6f3
    • https://github.com/karmi/railscasts-episodes/commit/03c45c3
    • https://github.com/karmi/tire/blob/master/test/models/active_record_models.rb#L10-20

    See this StackOverflow answer: ElasticSearch & Tire: Using Mapping and to_indexed_json for more information about mapping / to_indexed_json interplay.

    See this StackOverflow answer: Index the results of a method in ElasticSearch (Tire + ActiveRecord) to see how to fight n+1 queries when indexing models with associations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.