2016-06-24 35 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「用法的更新示例。 –

相關問題