关于solr的多字段高亮问题

来源:互联网 发布:geo数据挖掘套路 编辑:程序博客网 时间:2024/06/05 02:05

我们在使用solr的时候,难免碰到使用多字段的高亮问题。
比如:
我们solr的字段创建是这样的:

<entity name="weibo" query="select id, content from weibo">        <field column="content" name="weibo_content"/></entity>

在这个搜索结果中,我们搜索的是“济南”关键字,使用的weibo_content字段,但是我们的结果为:

这是明显不行的,为什么?
因为发微博的用户“济南凡事”的“济南”很明显没有被加亮。

那么我们必须这样创建字段:

<entity name="weibo"             query="select w.id id, w.content wcontent, u.username uname                   from (                   select id, content, master_id                   from weibo                   ) w, user_table u                   where w.master_id = u.id">        <field column="wcontent" name="weibo_content"/>        <field column="uname" name="weibo_content"/></entity>

我们将这条微博的用户名字也加进来,这样,我们就可以同时对微博内容和用户名字进行加亮。
那么如果是普通的高亮设置是无法让solr进行多字段的高亮的。
这是普通的solr查询:

http://localhost:8983/solr/weixinqing/select?df=weibo_content&hl.fl=weibo_content&hl.simple.post=%3C/em%3E&hl.simple.pre=%3Cem%3E&hl=on&indent=on&q=%E6%B5%8E%E5%8D%97&wt=json

我们可以查看结果:

"highlighting":{    "2":{      "weibo_content":["<a class='k' href='https://m.weibo.cn/k/<em>济南</em>身边事?from=feed'>#<em>济南</em>身边事#</a><em>济南</em>美女告白交警小哥--向全世界宣布爱你】网友"]}}}

很明显只有一个字段被加亮了,本来应该两个才是啊,“济南凡事”去哪里了?

我们可以查看官方文档 https://cwiki.apache.org/confluence/display/solr/Highlighting
其中使用hl.preserveMulti 可以让多字段高亮。

If true, multi-valued fields will return all values in the order they were saved in the index. If false, only values that match the highlight request will be returned.

将原来的语句改为:

http://localhost:8983/solr/weixinqing/select?df=weibo_content&hl.fl=weibo_content&hl.simple.post=%3C/em%3E&hl.simple.pre=%3Cem%3E&hl=on&indent=on&q=%E6%B5%8E%E5%8D%97&wt=json&hl.preserveMulti=true

加上&hl.preserveMulti=true就可以了。

原创粉丝点击