1

我正在尝试从 Solr 搜索中输出突出显示的搜索词。我正在使用带有 Scorched 的 Django 1.8.4。

我已经激活了突出显示(resp = ('highlighting', self.solr_response.highlighting))和搜索视图,我从搜索中得到的 json 输出是:

"highlighting": {

"f0109b89-4882-44cc-90b2-6a51561d14ee": { }, <!-- nothing here, though the result comes up)
"73bc1fe4-2c4a-4036-9373-242811e3e7d9": { },<!-- nothing here, though the result comes up)
"b7e7a44a-57c4-4378-94fc-273229b0ac7f":
{
"some_field":
[
    "Bla bla bla, <em>highlighted search-term</em> bla bla bla..."
]
},
)

问题是我找不到告诉 Django 模板系统访问它的方法some_field,因为它在下面content.highlighting(并且它的 id 在下面result.id)。当然content.highlighting.result.id.some_field不起作用 - 有没有办法连接类似的东西{{ content.highlighting}} + {{ result.id }},以便我可以在模板中输出突出显示的字符串?

4

1 回答 1

0

这就是我解决问题的方法

基本上,我在视图中添加了这段代码:

for d in response:
    d['highlighted_string'] = response.highlighting[d['id']]
results_list = response

这样来自 solr 的每个结果项都包含与其自己的 ID 对应的 highlight_string。然后,在模板中,result.highlighted_string 输出与该结果对应的正确 highlight_string。

我希望它可以对其他人有所帮助。

于 2015-10-14T15:02:04.430 回答