Is there a way to count all the elements of an index in ElasticSearch or Tire?

It would be nice to have a number of items without having to search, for example

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

Is it possible?

+6
source share
7 answers

First you must use the match_all query: MyModel.search( { query { all } }).results.total . (In Lucene, avoid oncoming requests at all costs.)

Tire does not currently provide a "count" API. That will change.

+6
source

In ElasticSearch, you can count all the elements using the account API

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

See the Count API on your website.

+9
source

Just got a hint from Karmi. The counting API is now available.

You can do the following:

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

Defined only then s.results.total .

See here: https://github.com/karmi/tire/blob/master/test/integration/count_test.rb

+9
source

You can do this in elasticsearch-model stone:

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

And you will not call a selection ...

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

If you are using gem 'elasticsearch-model', here is a good liner:

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

If you want to get the number of documents inside the index, you can also just check the index statistics , for example:

 curl -XGET localhost:9200/_stats 

As a result, you will receive the number of documents / deleted documents (documents have not yet been combined).

0
source

From the console:

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

can help someone;)

0
source

Source: https://habr.com/ru/post/900538/


All Articles