如果我想获取 elasticsearch 创建的索引的所有标记(我使用的是 rails elasticsearch gem ),我该怎么做呢?这样做只会为搜索词获取一组特定的标记:
curl -XGET 'http://localhost:9200/development_test/_analyze?text=John Smith'
最佳答案
您可以结合 Scroll API与 Term 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
这是相当缓慢和浪费的,因为它对索引中的每个文档执行 _termvectors
HTTP 请求,将所有术语保存在 RAM 中,并在枚举期间保持滚动上下文打开。但是,这不需要像 Luke 这样的其他工具,并且术语可以从索引中流出。
关于ruby-on-rails - 如何打印出elasticsearch创建的倒排索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26269206/