Elasticsearch-rails,突出显示查询

时间:2015-08-28 10:38:50

标签: ruby-on-rails elasticsearch elasticsearch-rails

我试图从Elasticsearch-rails gem获得亮点,但我无法让它发挥作用。

我的搜索方法:

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

当我在视图中映射结果以检查是否有任何高光时,我得到一个false的数组:

= @results.map(&:highlight?)

我在这里阅读了Elasticsearch文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html以及此处的gem文档:https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model我的查询似乎是正确的。不知道如何继续。

1 个答案:

答案 0 :(得分:3)

显然,解决方案是使用"*"而不是"_all"

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