【问题标题】:how to get number of documents term appear in, in elasticsearch rails如何在elasticsearch rails中获取出现的文档数量
【发布时间】:2023-04-03 18:54:02
【问题描述】:

如果我有很多文档并且想知道“'rails' 一词出现在多少文档中” 有没有办法从索引中的弹性搜索“轮胎宝石”中获取这些信息?

【问题讨论】:

    标签: ruby-on-rails search elasticsearch


    【解决方案1】:

    您可以使用 Facets 来制作类似于 SQL 'group-by' 聚合函数的东西,如 elasticsearch 文档中的here 所述。 示例代码如下所示:

    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']
    

    希望这会有所帮助。

    【讨论】:

    • 如何重新索引模型?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 2023-04-02
    • 2021-07-06
    • 2021-11-12
    相关资源
    最近更新 更多