简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM