2
votes

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.

1
_all in elasticsearch represent actual field _all that contains combination of all your fields. So if you have name and descrption all will include name+description ...it's good for debuggingequivalent8

1 Answers

3
votes

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: {
      :"*" => {}
    }
  }
}