【问题标题】:Highlighting in solr with python使用 python 在 solr 中突出显示
【发布时间】:2019-08-12 14:09:49
【问题描述】:

如果我想用pythonDjango中实现Solr的高亮功能,如何使用包 solrpy?

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

此外,solrpy 是否仍然适用于 solr 的更多功能,例如分面、突出显示和其他东西,除了基本查询

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)

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

【问题讨论】:

    标签: python django solr highlight


    【解决方案1】:

    突出显示信息存储在响应对象上名为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 相比在过去几年中不断开发。

    【讨论】:

    • 感谢您的好意,我做到了!更进一步,我想知道如何用突出显示的内容替换相应的内容,以便在 Django 的网页中显示 标签。我应该创建一个新模型并将其放入页面还是做其他事情?您能否教我一些方法来实现它或向我展示代码,因为我对 Django 不是很熟悉。再次感谢
    • 我已经很多年没有写过 django 了,但我假设你会将结果发送到你的模板并确保包含的内容允许 HTML。请注意字段中的其他 HTML 内容是否正确转义。尝试先解决它,如果没有解决,请提出一个新问题。
    【解决方案2】:

    是的。下面的代码允许突出显示(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 字段将存储搜索的突出显示的 sn-ps。其他字段为facetsgroupedhitsspellcheckstats。在https://github.com/django-haystack/pysolr查看更多信息

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 2015-10-30
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多