2016-06-24 34 views
1

这似乎是一个非常简单的问题,但是我从其他解决方案和网站尝试的所有内容都无法正常工作。我有三个字段,我不想索引或查询 - :p_s,:gender:part_of_speech - 但elasticsearch仍然从这些字段返回值,即使我没有指定它们应该被索引或查询。大约一半下来,this article说要no索引,但他们不表示这会发生。在rails中返回elasticsearch的特定字段

期限控制器:

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

型号:

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_analyzer 
     indexes :definition, analyzer: :combined_analyzer 
     indexes :etymology1, analyzer: :combined_analyzer 
     indexes :etymology2, analyzer: :combined_analyzer 
     indexes :uses, analyzer: :combined_analyzer 
     indexes :notes1, analyzer: :combined_analyzer 
     indexes :notes2, analyzer: :combined_analyzer 
    end 
    end 

    def self.search(query) 
    __elasticsearch__.search(
     { 
     query: { 
      multi_match: { 
      query: query, 
      fields: ['name^7', 'definition^6', 'etymology1^5', 'etymology2^4', 'uses^3', 'notes1^2', 'notes2^1'], 
      operator: 'and' 
      } 
     } 
     } 
    ) 
    end 
end 

# Delete the previous term 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) 

回答

0

我认为使用附带的elasticsearch方法使得有很大的意义。然而,在我自己的情况下,在我的模型我做了这样的事情,修改为你自己的情况:

def as_indexed_json 
    as_json(only: [:id, :name, :definition, :etymology1, :etymology2, :uses, :notes1, :notes2]) 
end 

这应该是因为在默认情况下工作Elasticsearch会叫你的模型中as_indexed_json方法来获得它需要的数据进行索引。

+0

对不起,我有一个新问题出现,我需要先解决......您的解决方案是我尝试过的方法之一对我无效。为了清楚起见,这段代码应该代替我的'def self.search(query)'方法,不是吗?它也应该跟在我的映射之后,对吧? – evanejin

+0

使用此方法,您不再需要执行映射,因为这些字段不会再被索引,因此根本无法查询。您的搜索仍然可以保持原样,我想所有其他配置都可以在您的设置中完成。 – oreoluwa

+0

嗯,好吧,我试着就像你说的,但它仍然返回那些额外的领域,我不想包括 – evanejin

0

为了纪念一个字段作为非索引使用本:

mappings dynamic: 'false' do 
    ... 
    indexes :p_s, index: :no 
    indexes :gender, index: :no 
    indexes :part_of_speech, index: :no 
    ... 
end 

默认情况下elasticsearch返回下"_source"键中的所有文档字段。只得到特定的字段,你可以在顶部查询水平这样

def self.search(query) 
    __elasticsearch__.search(
     { 
     query: { 
      multi_match: { 
      query: query, 
      fields: ['name^7', 'definition^6', 'etymology1^5', 'etymology2^4', 'uses^3', 'notes1^2', 'notes2^1'], 
      operator: 'and' 
      } 
     }, 
     fields: ['name', 'definition', 'etymology1', 'etymology2', 'uses', 'notes1', 'notes2'] 
     } 
    ) 
    end 

指定fields阵列或过滤"_source"

def self.search(query) 
    __elasticsearch__.search(
     { 
     query: { 
      multi_match: { 
      query: query, 
      fields: ['name^7', 'definition^6', 'etymology1^5', 'etymology2^4', 'uses^3', 'notes1^2', 'notes2^1'], 
      operator: 'and' 
      } 
     }, 
     '_source': ['name', 'definition', 'etymology1', 'etymology2', 'uses', 'notes1', 'notes2'] 
     } 
    ) 
end 

See Elasticsearch source filtering docs for more.

当使用multi_match子句,内部fields元素指定要在其上运行搜索的字段,以及o有点像你的例子中的提升。外部fields或'_source'子句反过来确定要返回哪些字段,并且这是您之后的字段。

要更好地了解调试弹性搜索查询时发生了什么,请使用Sense之类的工具。当你得到你想要的结果时,将查询转换为ruby代码比反过来容易得多。

+0

好吧,我试着在顶级查询中指定字段,但是我收到了这个错误'undefined method'slug'for #<%= term.name %>'我删除了链接并再次执行查询,但收到了'term.name'的相同错误 – evanejin

+0

我试了'_source'方法,但我想我把它放在错误的位置/没有正确输入。我已经阅读了大量的文档,并努力去理解,但作为一个新手,我发现很多这些都超出了我的头脑。例如,我应该如何应用这一行'“术语”:{“user”:“kimchy”}'我的代码?我真的不明白我应该用什么替代它。 – evanejin

+0

请参阅'_source“用法的更新示例。 –

相关问题