在Solr中计数

use*_*468 6 solr

我将以下文档存储在Solr中:

  doc {
    id: string; // this is a unique string that looks like an md5 result
    job_id: string; // this also looks like an md5 result -- this is not unique
    doc_id: number; // this is a long number -- this is not unique
    text: string; // this is stored, indexed text -- this is not unique
  }
Run Code Online (Sandbox Code Playgroud)

现在,我要做的是计算其中包含文本foo的文档(doc_id)的数量。因此,如果这是SQL,我将要发出如下内容:

SELECT count(distinct doc_id)
FROM Doc
WHERE text like '%foo%';
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Fux*_*uxi 4

为了使其工作(使用结果分组/归档折叠),您需要满足一些条件。

  • 您必须使文本查询(“%foo%”)才能在常规搜索中工作
  • doc_id 必须是字符串,您可以拥有该字段的副本并将其命名为 doc_id_str

然后你可以这样提出请求:

/select/?q=foo&rows=0&group=true&group.field=doc_id_str&group.limit=0&group.ngroups&group.format=simple&wt=json
Run Code Online (Sandbox Code Playgroud)

这个查询对我有用。它如何为您工作,取决于您的索引和它的大小。请询问您是否需要更多指导。