ruby-on-rails - 如何打印出elasticsearch创建的倒排索引?

标签 ruby-on-rails elasticsearch

如果我想获取 elasticsearch 创建的索引的所有标记(我使用的是 rails elasticsearch gem ),我该怎么做呢?这样做只会为搜索词获取一组特定的标记:

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

最佳答案

您可以结合 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

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

关于ruby-on-rails - 如何打印出elasticsearch创建的倒排索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26269206/

相关文章:

c# - 使用 mySQL 数据库在 C# 中实现 ElasticSearch

elasticsearch - 基巴纳。通过匹配来自另一个过滤器的值来过滤记录

elasticsearch - 查询数组仅适用于第一个元素

ruby-on-rails - Rails 4 接受嵌套属性 id 不能为空

ruby-on-rails - Rails新的app_name由于Sqlite3问题而失败

elasticsearch - 安装聚合插件(离线)Logstash

java - 在 Elasticsearch 中查找术语的出现

ruby-on-rails - 如何在 rjs 中使用 if 条件?

ruby-on-rails - 当远程 git 位于非标准路径上时使用 capistrano

ruby-on-rails - 验证错误消息未显示在表单 View 中