如何打印出elasticsearch创建的倒排索引?

Non*_*ona 10 ruby-on-rails elasticsearch

如果我想获得elasticsearch创建的索引的所有标记(我正在使用rails elasticsearch gem),我将如何去做呢?做这样的事情只会获得搜索词的一组特定标记:

curl -XGET 'http://localhost:9200/development_test/_analyze?text=John Smith'
Run Code Online (Sandbox Code Playgroud)

Chr*_*ndt 1

您可以将Scroll APITerm Vectors 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
Run Code Online (Sandbox Code Playgroud)

这是相当慢且浪费的,因为它对_termvectors索引中的每个文档执行 HTTP 请求,将所有术语保存在 RAM 中,并在枚举期间保持滚动上下文打开。但是,这不需要像 Luke 这样的其他工具,并且术语可以从索引中流出。