Search code examples
ruby-on-railselasticsearchelasticsearch-rails

Elasticsearch-rails, highlights query


I'm trying to get highlights from the Elasticsearch-rails gem, but I can't get it to work.

My search method:

query = {
  query: {
    filtered: {
      query: {
        match: {
          _all: params[:q]
        }
      },
      filter: {
        term: {
          active: true
        }
      }
    },
  },
  highlight: {
    fields: {
      _all: {fragment_size: 150, number_of_fragments: 3}
    }
  }
}

@results = Elasticsearch::Model.search(query, [Market, Component]).results

When I map my results in the view to check if there are any highlights, I get an array of false:

= @results.map(&:highlight?)

I read through the Elasticsearch docs here: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html and the gem's documentation here: https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model and my query seems to be correct. Not sure how to proceed.


Solution

  • Apparently, the solution was to use "*" instead of "_all":

    query = {
      query: {
        filtered: {
          query: {
            match: {
              _all: params[:q]
            }
          },
          filter: {
            term: {
              active: true
            }
          }
        },
      },
      highlight: {
        tags_schema: "styled",
        fields: {
          :"*" => {}
        }
      }
    }