简体   繁体   English

如何获取出现在Elasticsearch Rails中的文档数量

[英]how to get number of documents term appear in, in elasticsearch rails

如果我有很多文档,并且想知道“ rails”一词出现在“多少文档”中,有没有办法从索引中的elasticsearch“ tyre gem”中获取此信息?

You can use Facets to make something like SQL 'group-by' aggregate function, as described here in elasticsearch documentation. 您可以使用构面“由组”聚合功能,使类似SQL,如所描述这里 elasticsearch文档。 A sample code would look like: 示例代码如下所示:

index_name = "documents" # name of your index
search_word = 'rails' # word searching for
field_name = 'document_text' # field to search in
search = Tire.search index_name do
  query do
    string "#{field_name}:#{search_word}"
  end
  facet 'ids-facets', :global => false do
    terms :id
  end
end
document_ids = search.results.facets['ids-facets']['terms']

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM