0

Using Elasticsearch through Searchkick.

My documents look something like this:

{
    "_id" : ObjectId("54f8672f258f83ac4e7783e5"),
    "n" : "Figth Club",
    "dst" : "video",
    "detail" : {
        est: "El club de la lucha",
        ent: "Figth club",
        hut: "Harcosok klubja"
    }
}

My Item model:

class Item
 include Mongoid::Document
 searchkick

 def search_data
        {
            n: n,
            est: detail.est,
            ent: detail.ent,
            hut: detail.hut,
        }
 end

end

The search query would look something like:

Item.search(query, fields: [:n, :est, :ent, :hut], limit: 10).to_a

I would like to know what field the query was found on. For example, if query="El club de la lucha" I want to know that detail.est is the fields where it has been found on. Is that possible?

borjagvo
  • 1,802
  • 2
  • 20
  • 35

1 Answers1

0

What you need is to highlight the results that match your search. Please take a look at the offered api. https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html

Your query should look like this.

{
    "query" : {...},
    "highlight" : {
        "fields" : {
             "_all" : {}
        }
    }
}
Manolis
  • 728
  • 8
  • 24