2014-10-09 38 views
10

如果我想獲得該elasticsearch創建(我使用的rails elasticsearch gem)索引的所有令牌,我怎麼會去這樣做呢?做這樣的事情只得到一組特定的標記爲搜索詞:如何打印出由elasticsearch創建的倒排索引?

curl -XGET 'http://localhost:9200/development_test/_analyze?text=John Smith' 
+2

沒有通過elasticsearch提供以查看Lucene索引API。 但是有一些工具可以讓你查看lucene索引,如Luke。 以下是關於如何爲彈性搜索設置它的[博客] [1]可能可能有所幫助。 [1]:http://rosssimpson.com/blog/2014/05/06/using-luke-with-elasticsearch/ – keety 2014-10-09 15:45:30

+0

謝謝我設法讓盧克起來和運行..任何想法在哪裏指數彈性搜索創建存儲在Linux上?我檢查了/etc/init.d並沒有看到任何.idx文件。 – Nona 2014-10-09 17:19:16

+0

索引路徑應該在elasticsearch的config的path.data字段中提供。該指數應該是類似的路徑/<羣集名>/// /指數/ – keety 2014-10-09 20:14:16

回答

1

您可以用Term Vectors APIScroll API結合,列舉出倒排索引項:

require "elastomer/client" 
require "set" 

client = Elastomer::Client.new({ :url => "http://localhost:9200" }) 
index = "someindex" 
type = "sometype" 
field = "somefield" 

terms = Set.new 

client.scan(nil, :index => index, :type => type).each_document do |document| 
    term_vectors = client.index(index).docs(type).termvector({ :fields => field, :id => document["_id"] })["term_vectors"] 
    if term_vectors.key?(field) 
    term_vectors[field]["terms"].keys.each do |term| 
     unless terms.include?(term) 
     terms << term 
     puts(term) 
     end 
    end 
    end 
end 

這是相當緩慢並且浪費,因爲它對索引中的每個單獨文檔執行一個_termvectors HTTP請求,將所有條目保存在RAM中,並在枚舉期間保持滾動上下文打開。然而,這並不需要像Luke這樣的其他工具,而且這些術語可以從索引中流出。