【问题标题】:Is there a way to count all elements of an index in ElasticSearch or Tire?有没有办法计算 ElasticSearch 或轮胎中索引的所有元素?
【发布时间】:2011-12-19 15:54:37
【问题描述】:

如果不需要像这样的搜索就可以计算元素数量会很好

Obj.search("id:*").count

这可能吗?

【问题讨论】:

    标签: ruby elasticsearch


    【解决方案1】:

    在 ElasticSearch 中,您可以使用 count API 计算所有元素

    curl -XGET http://localhost:9200/index/_count
    

    请参阅他们网站上的 Count API 文档。

    【讨论】:

      【解决方案2】:

      首先,您应该使用match_all 查询:MyModel.search( { query { all } }).results.total。 (在 Lucene 中,不惜一切代价避免通配符查询。)

      目前,Tire 不公开“计数”API。这会改变。

      【讨论】:

      • 轮胎中没有count查询吗?
      【解决方案3】:

      刚从 Karmi 那里得到一个提示。 count API 现已推出。

      您可以执行以下操作:

      s = Tire.search 'articles-test', :search_type => 'count' do
        query { term :tags, 'ruby' }
      end
      

      只有 s.results.total 才被定义。

      请看这里:https://github.com/karmi/tire/blob/master/test/integration/count_test.rb

      【讨论】:

        【解决方案4】:

        如果您使用 gem 'elasticsearch-model',这里有一个很好的衬里:

        Elasticsearch::Model.client.count(index: 'your_index_name_here')['count']
        

        【讨论】:

          【解决方案5】:

          从控制台:

          Model.search("*:*").results.total
          

          也许可以帮助某人;)

          【讨论】:

            【解决方案6】:

            您也可以在 elasticsearch-model gem 中执行此操作:

            Article.search("cats", search_type: 'count').results.total
            # => 2026
            

            而且你不会导致获取...

            Article.search("cats", search_type: 'count').map {|r| r.title}
            # => []
            

            【讨论】:

              【解决方案7】:

              如果要获取索引中的文档数量,也可以检查index stats,如下所示:

              curl -XGET localhost:9200/_stats
              

              结果您将获得文档数/已删除文档数(文档尚未合并)。

              【讨论】:

                猜你喜欢
                • 2012-03-22
                • 1970-01-01
                • 2014-07-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-06-02
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多