设为首页 收藏本站
查看: 523|回复: 0

[经验分享] solr suggester 组件使用

[复制链接]

尚未签到

发表于 2016-12-15 10:23:31 | 显示全部楼层 |阅读模式
The begining

There is one thing that you must know – Suggest component is not available in Solr version 1.4.1 and below. To start using this component you need to download 3_x or trunk version from Lucene/Solr SVN.

Configuration
Before we get into the index configuration we need to define an search component. So let’s do it:

view sourceprint?
1.
<searchComponent name="suggest" class="solr.SpellCheckComponent">
2.
<lst name="spellchecker">
3.
<str name="name">suggest</str>
4.
<str name="classname">org.apache.solr.spelling.suggest.Suggester</str>
5.
<str name="lookupImpl">org.apache.solr.spelling.suggest.tst.TSTLookup</str>
6.
<str name="field">name_autocomplete</str>
7.
</lst>
8.
</searchComponent>
It is worth mentioning that suggest component is based on solr.SpellCheckComponent and that’s why we can use the above configuration. We have three important attributes in the configuration:

name - name of the component.
lookupImpl – an object that will handle the search. At this point we have two possibilities to use – JasperLookup or TSTLookup. This second one characterizes greater efficiency.
field – the field on the basis of which suggestions are generated.
Now let’s add the appropriate handler:

view sourceprint?
01.
<requestHandler name="/suggest" class="org.apache.solr.handler.component.SearchHandler">
02.
<lst name="defaults">
03.
<str name="spellcheck">true</str>
04.
<str name="spellcheck.dictionary">suggest</str>
05.
<str name="spellcheck.count">10</str>
06.
</lst>
07.
<arr name="components">
08.
<str>suggest</str>
09.
</arr>
10.
</requestHandler>
Quite simple configuration, which defines a handler with an additional search component and tell Solr that the maximum number of suggestions returned is 10, this it should use dictionary named suggest (which is actually a Suggest component) which is exactly the same as our defined component.

Index
Let us assume that our document consists of three fields: id, name and description. We want to generate suggestions on the field that hold the name of the product. Our index could look like this:

view sourceprint?
1.
<field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/>
2.
<field name="name" type="text" indexed="true" stored="true" multiValued="false" />
3.
<field name="name_autocomplete" type="text_auto" indexed="true" stored="true" multiValued="false" />
4.
<field name="description" type="text" indexed="true" stored="true" multiValued="false" />
In addition, there is the following copy field definition:

view sourceprint?
1.
<copyField source="name" dest="name_autocomplete" />
Suggesting single words
In order to achieve individual words suggestions text_autocomplete type should be defined as follows:

view sourceprint?
1.
<fieldType class="solr.TextField" name="text_auto" positionIncrementGap="100">
2.
<analyzer>
3.
<tokenizer class="solr.WhitespaceTokenizerFactory"/>
4.
<filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0" splitOnCaseChange="1"/>
5.
<filter class="solr.LowerCaseFilterFactory"/>
6.
</analyzer>
7.
</fieldType>
Suggesting phrases
To implement the entire phrase suggestions our text_autocomplete type should be defined as follows:

view sourceprint?
1.
<fieldType class="solr.TextField" name="text_auto">
2.
<analyzer>
3.
<tokenizer class="solr.KeywordTokenizerFactory"/>
4.
<filter class="solr.LowerCaseFilterFactory"/>
5.
</analyzer>
6.
</fieldType>
If you want to use phrases you may want to define your own query converter.

Dictionary building
Before we start using the component, we need to build its index. To this send the following command to Solr:

view sourceprint?
1.
/suggest?spellcheck.build=true
Queries
Now we come to use of the component. In order to show how the use the component, I decided suggest whole phrases. The example query could look like that:

view sourceprint?
1.
/suggest?q=har
After running that query I got the following suggestions:

view sourceprint?
01.
<?xml version="1.0" encoding="UTF-8"?>
02.
<response>
03.
<lst name="responseHeader">
04.
<int name="status">0</int>
05.
<int name="QTime">0</int>
06.
</lst>
07.
<lst name="spellcheck">
08.
<lst name="suggestions">
09.
<lst name="dys">
10.
<int name="numFound">4</int>
11.
<int name="startOffset">0</int>
12.
<int name="endOffset">3</int>
13.
<arr name="suggestion">
14.
<str>hard drive</str>
15.
<str>hard drive samsung</str>
16.
<str>hard drive seagate</str>
17.
<str>hard drive toshiba</str>
18.
</arr>
19.
</lst>
20.
</lst>
21.
</lst>
22.
</response>
The end
In the next part of the autocomplete functionality I’ll show how to modify its configuration to use static dictionary into the mechanism and how this can helk you get better suggestions. The last part of the series will be a performance comparison of each method in which I’ll try to diagnose which method is the fastest one in various situations.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-314645-1-1.html 上篇帖子: Solr索引数据同步ReplicationHandler 下篇帖子: 结合源码浅析solr facet
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表