Elasticsearch with Tire, highlight not working

Tag: highlighting , elasticsearch , tire Author: jianwang_blog Date: 2012-07-25

I must be missing something here, but when I try to get highlighting on a search with Elasticsearch, I'm not seeing any highlighting at all, but no errors either. I don't think it's a Tire issue, but I mention Tire just in case it's important. The indexing using Tire is pretty simple (some fields taken out for brevity):

mapping :_source => { :excludes => ['attachment'] } do
  indexes :id, :type => 'integer'
  indexes :title, :store => true
  indexes :attachment, :type => 'attachment', :_source => { :enabled => false }
end

Using curl, I can try this query, which works fine but there's no highlighting in the results:

curl -XPOST http://localhost:9200/myobject/_search\?pretty\=true -d '{             
  "query": {"query_string": {"query": "foobar"}},
  "highlight": {"fields": {"Title":{}}}
}'

Note that I added the ":store => true" in the mapping just to make sure, though I don't think it should be necessary to make highlighting work. So I'm guessing I'm missing something either in the mapping or in the query specification, but I'm not seeing it. Any suggestions would be very much appreciated. Thanks.

Best Answer

Field names are case-sensitive in elasticsearch. Title and title are two different fields. Try this query:

curl -XPOST http://localhost:9200/myobject/_search\?pretty\=true -d '{             
  "query": {"query_string": {"query": "foobar"}},
  "highlight": {"fields": {"title":{}}}
}

comments:

Brilliant, thank you. The field in the original data object being indexed was called "Title" but it looks like the Tire mapping being ":title" maps it to lower case, which I should have noticed in the output of the curl but missed somehow. The curl works now, but using Tire doesn't always get the highlighting right. The one-line "s = MyObject.search(q, :highlight => :title)" doesn't work but a block search with "highlight :title" does. In any case, thanks a lot for the quick help.