使用 python 在 solr 中突出显示

Highlighting in solr with python

如果我想用python[=28在Django中实现Solr的高亮功能=],如何使用包 solrpy?

solrpy 是如何处理它的,因为突出显示结果存在于 SolrResponse 对象的绝对片段中,显示为字典词典数量。

更重要的是,solrpy除了基本的查询[=]之外,还有solr的更多功能,比如分面,高亮等等。 12=]

sc = solr.SolrConnection("http://localhost:8080/solr/cases")
response_c=sc.query('name:*%s'%q+'*',fields='name,decision_date', highlight='name')

print(response_c.results)
for hit in response_c.results:
  print(hit)

为什么上面的代码不能实现高亮?

是的。下面的代码允许突出显示(pysolr,版本 3.6.0):

import pysolr

solr = pysolr.Solr('http://localhost:8983/solr/<core/collection>')  
results = solr.search('hello', **{
        'hl': 'true',
        'hl.fragsize': 10,
        'hl.field': 'text'
    })

for i in results:
   print(i)
print(results.highlighting)

results.highlighting 字段将存储搜索的突出显示片段。其他字段为 facetsgroupedhitsspellcheckstats。在 https://github.com/django-haystack/pysolr

查看更多信息

突出显示信息存储在响应对象上名为 highlighting 的单独条目中:

If you pass in `highlight` to the SolrConnection.query call,
then the response object will also have a "highlighting" property,
which will be a dictionary.

也就是说,我强烈建议使用 pysolr 而不是 solrpy,因为 pysolr 由 django-haystack 项目维护,并且与 solrpy 相比在过去几年中不断发展。