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 8460945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:44:39+00:00 2026-06-10T13:44:39+00:00

I’m having trouble getting the highlighting to work with Elasticsearch (and Tire) in a

  • 0

I’m having trouble getting the highlighting to work with Elasticsearch (and Tire) in a Rails app. I can successfully index PDF attachments and query them but I cannot get the highlighting to work.

Not that familiar with ES so not sure where to look to troubleshoot. Will start with mappings and a curl query but feel free to ask for more info.

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

  attr_accessible :title, :content, :published_on, :filename 

  mapping do
    indexes :id, :type =>'integer'
    indexes :title
    indexes :content
    indexes :published_on, :type => 'date'
    indexes :attachment, :type => 'attachment',
                            :fields => {
                            :name       => { :store => 'yes' },
                            :content    => { :store => 'yes' },
                            :title      => { :store => 'yes' },
                            :file       => { :term_vector => 'with_positions_offsets', :store => 'yes' },
                            :date       => { :store => 'yes' }
                          }
  end

  def to_indexed_json
    to_json(:methods => [:attachment])
  end

  def attachment
    if filename.present?
      path_to_pdf = "/Volumes/Calvin/sample_pdfs/#{filename}.pdf"
      Base64.encode64(open(path_to_pdf) { |pdf| pdf.read })
    else
      Base64.encode64("missing")
    end
  end
end

Mappings (via Curl):

$ curl -XGET 'http://localhost:9200/_mapping?pretty=true'
{
  "articles" : {
    "article" : {
      "properties" : {
        "attachment" : {
          "type" : "attachment",
          "path" : "full",
          "fields" : {
            "attachment" : {
              "type" : "string"
            },
            "title" : {
              "type" : "string",
              "store" : "yes"
            },
            "name" : {
              "type" : "string",
              "store" : "yes"
            },
            "date" : {
              "type" : "date",
              "ignore_malformed" : false,
              "store" : "yes",
              "format" : "dateOptionalTime"
            },
            "content_type" : {
              "type" : "string"
            }
          }
        },
        "content" : {
          "type" : "string"
        },
        "created_at" : {
          "type" : "date",
          "ignore_malformed" : false,
          "format" : "dateOptionalTime"
        },
        "filename" : {
          "type" : "string"
        },
        "id" : {
          "type" : "integer",
          "ignore_malformed" : false
        },
        "published_on" : {
          "type" : "date",
          "ignore_malformed" : false,
          "format" : "dateOptionalTime"
        },
        "title" : {
          "type" : "string"
        },
        "updated_at" : {
          "type" : "date",
          "ignore_malformed" : false,
          "format" : "dateOptionalTime"
        }
      }
    }
  }
}%

A query with a ‘hit’ inside a 125 page indexed PDF:

$ curl "localhost:9200/_search?pretty=true" -d '{
quote>   "fields" : ["title"],
quote>   "query" : {
quote>     "query_string" : {
quote>       "query" : "xerox"
quote>     }
quote>   },
quote>   "highlight" : {
quote>     "fields" : {
quote>       "attachment" : {}
quote>     }
quote>   }
quote> }'

{
  "took" : 1077,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.036417194,
    "hits" : [ {
      "_index" : "articles",
      "_type" : "article",
      "_id" : "13",
      "_score" : 0.036417194,
      "fields" : {
        "title" : "F-E12"
      }
    } ]
  }
}%    

I was expecting a section like:

"highlight" : {
        "attachment" : [ "\nLast Year <em>Xerox</em> moved their facilities" ]
  }

Thanks for any help!

Edit2: adjusted query (changed attachment to attachment.file) to no avail:

$ curl "localhost:9200/_search?pretty=true" -d '{
  "fields" : ["title","attachment"],
  "query" : {"query_string" : {"query" : "xerox"}},
  "highlight" : {"fields" : {"attachment.file" : {}}}
}'

{
  "took" : 221,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.036417194,
    "hits" : [ {
      "_index" : "articles",
      "_type" : "article",
      "_id" : "13",
      "_score" : 0.036417194,
      "fields" : {
        "title" : "F-E12",
        "attachment" : "JVBERi0xLjYNJeLjz9MNCjk4NSAwIG9iag08PC9MaW5lYXJpemVkIDEvTCA...\n"
      }
    } ]
  }
}

Edit3 (remove “fields”):

$ curl "localhost:9200/_search?pretty=true" -d '{
>   "query" : {"query_string" : {"query" : "xerox"}},
>   "highlight" : {"fields" : {"attachment" : {}}}
> }'

{
  "took" : 1078,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.036417194,
    "hits" : [ {
      "_index" : "articles",
      "_type" : "article",
      "_id" : "13",
      "_score" : 0.036417194, "_source" : {"content":"Real report","created_at":"2012-08-28T22:44:08Z","filename":"F-E12","id":13,"published_on":"2007-12-28","title":"F-E12","updated_at":"2012-08-28T22:44:08Z","attachment":"JVBERi0xLjYNJeLjz9MNCjk4NSAwIG9iag08PC9MaW5lYXJpemVkID...\n"
      }
    } ]
  }
}

Edit4 (mapping from Attachment Type in Action tutorial):

$ curl -XGET 'http://localhost:9200/test/_mapping?pretty=true'
{
  "test" : {
    "attachment" : {
      "properties" : {
        "file" : {
          "type" : "attachment",
          "path" : "full",
          "fields" : {
            "file" : {                #<== This appears to be missing 
              "type" : "string",      #<== from my Articles mapping
              "store" : "yes",        #<==
              "term_vector" : "with_positions_offsets"  #<==
            },
            "author" : {
              "type" : "string"
            },
            "title" : {
              "type" : "string",
              "store" : "yes"
            },
            "name" : {
              "type" : "string"
            },
            "date" : {
              "type" : "date",
              "ignore_malformed" : false,
              "format" : "dateOptionalTime"
            },
            "keywords" : {
              "type" : "string"
            },
            "content_type" : {
              "type" : "string"
            }
          }
        }
      }
    }
  }
}
  • 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-10T13:44:41+00:00Added an answer on June 10, 2026 at 1:44 pm

    I figured it out! Finally…

    problem was with my mapping syntax in Article class. Needed to rename “:file” to “:attachment”.

      tire.mapping do
        indexes :id, :type =>'integer'
        indexes :title
        indexes :content
        indexes :published_on, :type => 'date'
        indexes :attachment, :type => 'attachment', #:null_value => 'missing_file',
                                :fields => {
                                :name       => { :store => 'yes' },  # exists?!?
                                :content    => { :store => 'yes' },
                                :title      => { :store => 'yes' },
      # WRONG! see next line => :file       => { :term_vector => 'with_positions_offsets', :store => 'yes' },
                                :attachment => { :term_vector => 'with_positions_offsets', :store => 'yes' },
                                :date       => { :store => 'yes' }
                              }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload

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.