【发布时间】:2018-03-04 21:28:50
【问题描述】:
如何获得与给定查询匹配的文档总数。我使用了以下查询:
result = solr.search('ad_id : 20')
print(len(result))
由于默认返回值是'10',所以输出只有10个,但是count是4000,怎么才能得到count的总数呢?
【问题讨论】:
标签: python-2.7 solr pysolr
如何获得与给定查询匹配的文档总数。我使用了以下查询:
result = solr.search('ad_id : 20')
print(len(result))
由于默认返回值是'10',所以输出只有10个,但是count是4000,怎么才能得到count的总数呢?
【问题讨论】:
标签: python-2.7 solr pysolr
如果您只想要满足查询的项目总数,这是我的 Python3 代码(使用 pysolr 模块):
collection='bookindex' # or whatever your collection is called
solr_url = f"http://{SOLR_HOST}/solr/{collection}"
solr = pysolr.Solr(url=solr_url, timeout=120, always_commit=True)
result = solr.search("*:*", rows=0);
return result.hits
这会查询所有文档(“:”)——在我的例子中是 315913——但您可以缩小范围以满足您的要求。例如,如果我想知道有多少书籍条目有 title:pandas,我可以搜索("title:pandas", rows=0) 并得到 41 作为标题中有 pandas 的数字。通过设置 rows=0,您让 Solr 知道它不需要为您格式化任何结果,而您只需返回元信息,因此比对行设置上限更有效。
【讨论】:
正如@MatsLindh 提到的 -
result = solr.search('ad_id : 20')
print(result.hits)
【讨论】:
计数存储在 numFound 变量中。使用下面的代码:
result = solr.search('ad_id : 20')
print(result.raw_response['response']['numFound'])
【讨论】:
results object from pysolr has a hits property that contains the total number of hits,无论返回多少文档。这在 Solr 的原始响应中被命名为 numFound。
您的解决方案并不适合任何具有较大数据集的内容,因为它需要您检索所有文档,即使您不需要它们或想要显示它们的内容。
【讨论】:
终于得到答案了:
在查询末尾添加rows=1000000。
result = solr.search('ad_id : 20', rows=1000000)
但如果行数大于此值,则应在查询中更改该数字。这可能是一个糟糕的解决方案,但有效。 如果有人有更好的解决方案,请回复。
【讨论】: