首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用rails在弹性搜索中突出显示问题

用rails在弹性搜索中突出显示问题
EN

Stack Overflow用户
提问于 2016-03-09 02:57:27
回答 1查看 373关注 0票数 0

在阅读了几个网站(包括elasticsearch的文档)并进行了大量的实验之后,我很难获得高光。我可以做基本的关键词搜索,但很明显我没有抓住什么。这是我的密码。

宝石:

代码语言:javascript
复制
gem 'elasticsearch-model'
gem 'elasticsearch-rails'

主计长:

代码语言:javascript
复制
class TermsController < ApplicationController

  def search
    @terms = Term.search(params[:query]).results
  end
end

型号:

代码语言:javascript
复制
require 'elasticsearch/model'

class Term < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings index: { number_of_shards: 1, number_of_replicas: 0 } do
    mappings dynamic: 'false' do
      indexes :id, index: :not_analyzed
      indexes :name, analyzer: 'spanish'
      indexes :gender, index: :not_analyzed
      indexes :part_of_speech, index: :not_analyzed
      indexes :definition
      indexes :etymology1
      indexes :etymology2
      indexes :uses
      indexes :romance_cognates
      indexes :notes1
      indexes :notes2
      indexes :quote1, analyzer: 'spanish'
      indexes :quote2, analyzer: 'spanish'
    end
  end

  def as_indexed_json(options = {})
    as_json(
      only: [:name, :gender, :part_of_speech, :definition, :etymology1, :etymology2, :uses, :romance_cognates, :notes1, :notes2, :quote1, :quote2]
    )
  end

  def self.search(query)
    __elasticsearch__.search(
      {
        query: {
          multi_match: {
            query: query,
            fields: ['name', 'definition', 'etymology1', 'etymology2', 'uses', 'romance_cognates', 'notes1', 'notes2', 'quote1', 'quote2']
          }
        },
        highlight: {
          tags_schema: 'styled',
          fields: {
            :'*' => {}
          }
        }
      }
    )
  end
end

# Delete the previous terms index in Elasticsearch
Term.__elasticsearch__.client.indices.delete index: Term.index_name rescue nil

# Create the new index with the new mapping
Term.__elasticsearch__.client.indices.create \
  index: Term.index_name,
  body: { settings: Term.settings.to_hash, mappings: Term.mappings.to_hash }

# Index all term records from the db to Elasticsearch
Term.import(force: true)

我也试过:

代码语言:javascript
复制
      {
        query: {
          multi_match: {
            query: query,
            fields: ['name', 'definition', 'etymology1', 'etymology2', 'uses', 'romance_cognates', 'notes1', 'notes2', 'quote1', 'quote2']
          }
        },
        highlight: {
          fields: {
            content: {'force_source': true}
          }
        }
      }

代码语言:javascript
复制
      {
        query: {
          multi_match: {
            query: query,
            fields: ['name', 'definition', 'etymology1', 'etymology2', 'uses', 'romance_cognates', 'notes1^5', 'notes2', 'quote1', 'quote2']
          }
        },
        highlight: {
          fields: {
            content: {type: 'plain'}
          }
        }
      }

代码语言:javascript
复制
  {
    query: {
      multi_match: {
        query: query,
        fields: ['name', 'definition', 'etymology1', 'etymology2', 'uses', 'romance_cognates', 'notes1^5', 'notes2', 'quote1', 'quote2']
      }
    },
    highlight: {
      pre_tags: ['<tag1>']
      post_tags: ['</tag1>']
      fields: {
        _all: {}
      }
    }
  }

...Along还有很多其他的尝试,我不记得了

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-31 02:26:12

作为图解,我缺少的关键似乎是我需要视图模板中的try()方法。我确信有一种更简洁的编写方法,但我的视图语法示例如下所示:

代码语言:javascript
复制
<%= term.try(:highlight).try(:definition) ? term.highlight.definition[0].html_safe : term.definition.html_safe %>
<%= term.try(:highlight).try(:etymology1) ? term.highlight.etymology1[0].html_safe : term.etymology1.html_safe %>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35882055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档