|
Solr学习笔记之5、Component(组件)与Handler(处理器)学习
一、搜索篇
拼写检查(spellCheck)
作用:用来检查用户输入的检索内容是否存在,如果不存在则给它提示出相近或相似的内容
配置:在solrconfig.xml中配置如下
default
Title
spellchecker
true
false
false
1
spellcheck
View Code 举例:
http://localhost:8080/solr/collection1/spell?q=Title:tests&spellcheck=true
请求结果如下图:
0
119
1
6
11
0
test
6
false
Title:test
6
test
View Code 检索建议(suggest)
作用:检索建议则是用户输入某个检索条件后,会立刻友好的给出一系列提示内容,并推荐首个出现的相似的词,作为推荐词。如果这个条件想关的东西一个都没有,则不会提示,所以某种意义上来说,可以在用户输入检索条件时使用suggest,而在点击完搜索时,使用拼写检查,二者结合给可以用户带来比较好的用户体验。
配置:在solrconfig.xml中配置如下
text
suggest
org.apache.solr.spelling.suggest.Suggester
org.apache.solr.spelling.suggest.tst.TSTLookup
Title
0.0001
freq
true
true
true
suggest
10
true
false
true
suggest
View Code 举例:
http://localhost:8080/solr/collection1/suggest?wt=xml&indent=true&spellcheck=true&spellcheck.q=tes
http://localhost:8080/solr/collection1/suggest?q=Title:tes&wt=xml&indent=true
该请求执行结果如下:
0
1
103-
test
test
View Code 分层查询(facet)
作用:Facet是solr的高级搜索功能之一,可以给用户提供更友好的搜索体验。在搜索关键字的同时,能够按照Facet的字段进行分组并统计。Facet是Solr默认集成的一个组件。
配置:无需额外配置
特别说明:
1、适宜被Facet的字段
一般代表了实体的某种公共属性,如商品的分类、商品的制造厂家、书籍的出版商等等。
2、Facet字段的要求
Facet的字段必须被索引,一般来说该字段无需分词,无需存储。
无需分词是因为该字段的值代表了一个整体概念,另外该字段的值无需进行大小写转换等处理,保持其原貌即可。
无需存储是因为一般而言用户所关心的并不是该字段的具体值,而是作为对查询结果进行分组的一种手段,用户一般会沿着这个分组进一步深入搜索。
3、特殊情况
对于一般查询而言,分词和存储都是必要的。比如CPU类型”Intel 酷睿2双核 P7570”, 拆分成”Intel”,”酷睿”,”P7570”这样一些关键字并分别索引,可能提供更好的搜索体验。但是如果将CPU作为Facet字段,最好不进行分词,这样就造成了矛盾,解决方法为,将CPU字段设置为不分词不存储,然后建立另外一个字段为它的COPY,对这个COPY的字段进行分词和存储。
参数说明:
Field Facet :Facet字段通过在请求中加入facet.field参数加以声明,如果需要对多个字段进行Facet查询,那么将该参数声明多次。
各个Facet字段互不影响,且可以针对每个Facet字段设置查询参数。形式为:f.字段名.参数名=参数值,字段为为空代表应用于所有facet字段
举例:
http://localhost:8080/solr/collection1/select/?q=*:*&indent=on&facet=on&facet.field=ArticleTypeName&facet.field=EditorialOfficeName
该请求执行结果如下:
0
1
on
on
ArticleId:5
ArticleTypeName
EditorialOfficeName
2
5
false
2
content five
2014-03-24T16:00:00Z
test title 5 five
体育
燕赵都市报
1463443978500177920
1
0
1
0
0
View Code Date Facet :Solr为日期字段提供了更为方便的日期查询统计方式,字段的类型必须是DateField(或其子类型)。
需要注意的是使用Date Facet时,字段名、起始时间、结束时间、时间间隔这4个参数都必须提供。
举例:
http://localhost:8080/solr/collection1/select/?q=*:*&indent=on&facet=on&facet.date=CreateDate&facet.date.start=2014-3-10T0:0:0Z&facet.date.end=2014-3-26T0:0:0Z&facet.date.gap=%2B1DAY&facet.date.other=all
该请求执行结果如下:
0
39
2014-3-10T0:0:0Z
on
on
*:*
CreateDate
all
+1DAY
2014-3-26T0:0:0Z
2
5
false
2
content five
2014-03-24T16:00:00Z
test title 5 five
体育
燕赵都市报
1463443978500177920
2
6
false
3
content six
2014-03-25T16:00:00Z
test title 6 six
体育
北京晚报
1463443978552606720
1
7
false
1
content seven
2014-03-26T16:00:00Z
test title 7 seven
财经
光明日报
1463443978554703872
1
8
false
2
content eight
2014-03-27T16:00:00Z
test title 8 eight
财经
燕赵都市报
1463443978556801024
1
9
false
3
content nine
2014-03-28T16:00:00Z
test title 9 nine
财经
北京晚报
1463443978558898176
1
10
false
2
content ten
2014-03-23T16:00:00Z
test title 10 ten
财经
燕赵都市报
1463443978559946752
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
+1DAY
2014-03-10T00:00:00Z
2014-03-26T00:00:00Z
0
3
3
View Code Facet Query :Facet Query利用类似于filter query的语法提供了更为灵活的Facet,通过facet.query参数,可以对任意字段进行筛选。
举例:
http://localhost:8080/solr/collection1/select/?q=*:*&indent=on&facet=on&facet.query=CreateDate:[2014-3-24T0:0:0Z TO 2014-3-26T0:0:0Z]
该请求执行结果如下:
0
1
on
on
CreateDate:[2014-3-24T0:0:0Z TO 2014-3-26T0:0:0Z]
*:*
2
5
false
2
content five
2014-03-24T16:00:00Z
test title 5 five
体育
燕赵都市报
1463443978500177920
2
6
false
3
content six
2014-03-25T16:00:00Z
test title 6 six
体育
北京晚报
1463443978552606720
1
7
false
1
content seven
2014-03-26T16:00:00Z
test title 7 seven
财经
光明日报
1463443978554703872
1
8
false
2
content eight
2014-03-27T16:00:00Z
test title 8 eight
财经
燕赵都市报
1463443978556801024
1
9
false
3
content nine
2014-03-28T16:00:00Z
test title 9 nine
财经
北京晚报
1463443978558898176
1
10
false
2
content ten
2014-03-23T16:00:00Z
test title 10 ten
财经
燕赵都市报
1463443978559946752
2
View Code Range Facet 举例:范围查询统计
http://localhost:8080/solr/collection1/select/?q=*:*&indent=on&facet=on&facet.range=CreateDate&facet.range.start=2014-03-24T16:00:00Z&facet.range.end=2014-03-26T16:00:00Z&facet.range.gap=%2B1DAY
0
2
on
on
*:*
2014-03-24T16:00:00Z
CreateDate
+1DAY
2014-03-26T16:00:00Z
2
5
false
2
content five
2014-03-24T16:00:00Z
test title 5 five
体育
燕赵都市报
1463443978500177920
2
6
false
3
content six
2014-03-25T16:00:00Z
test title 6 six
体育
北京晚报
1463443978552606720
1
7
false
1
content seven
2014-03-26T16:00:00Z
test title 7 seven
财经
光明日报
1463443978554703872
1
8
false
2
content eight
2014-03-27T16:00:00Z
test title 8 eight
财经
燕赵都市报
1463443978556801024
1
9
false
3
content nine
2014-03-28T16:00:00Z
test title 9 nine
财经
北京晚报
1463443978558898176
1
10
false
2
content ten
2014-03-23T16:00:00Z
test title 10 ten
财经
燕赵都市报
1463443978559946752
1
1
+1DAY
2014-03-24T16:00:00Z
2014-03-26T16:00:00Z
View Code 分组统计:
分组示例(group):
http://localhost:8080/solr/collection1/select?q=*:*&wt=xml&indent=true&group=true&group.field=TypeId&group.ngroups=true
统计示例(stats):
httphttp://localhost:8080/solr/select?q=*:*&stats=true&stats.field=Price&rows=10&indent=true
该请求执行结果如下:
0
32
true
Price
true
*:*
10
2
6
6.0
false
3
content six
2014-03-25T16:00:00Z
test title 6 six
体育
北京晚报
1463715628722421760
1
7
7.0
false
1
content seven
2014-03-26T16:00:00Z
test title 7 seven
财经
光明日报
1463715628782190592
1
8
8.0
false
2
content eight
2014-03-27T16:00:00Z
test title 8 eight
财经
燕赵都市报
1463715628784287744
1
9
9.0
false
3
content nine
2014-03-28T16:00:00Z
test title 9 nine
财经
北京晚报
1463715628786384896
1
10
10.0
false
2
content ten
2014-03-23T16:00:00Z
test title 10 ten
财经
燕赵都市报
1463715628788482048
6.0
10.0
5
0
40.0
330.0
8.0
1.5811388300841898
View Code 注:统计字段应为数字类型,如果为字符串类型则统计结果不全。
自动聚合(clustering)
作用:能够把检索到的内容自动分类。
配置:在solrconfig.xml中配置如下
lingo
org.carrot2.clustering.lingo.LingoClusteringAlgorithm
clustering/carrot2
stc
org.carrot2.clustering.stc.STCClusteringAlgorithm
kmeans
org.carrot2.clustering.kmeans.BisectingKMeansClusteringAlgorithm
true
true
name
id
features
true
false
edismax
text^0.5 features^1.0 name^1.2 sku^1.5 id^10.0 manu^1.1 cat^1.4
*:*
10
*,score
clustering
View Code 举例:
http://localhost:8080/solr/clustering?q=*:*&rows=10&LingoClusteringAlgorithm.desiredClusterCountBase=20
该请求执行结果如下:
0
12
2
5
false
2
content five
2014-03-24T16:00:00Z
test title 5 five
体育
燕赵都市报
1463443978500177920
0.42292467
2
6
false
3
content six
2014-03-25T16:00:00Z
test title 6 six
体育
北京晚报
1463443978552606720
0.42292467
1
7
false
1
content seven
2014-03-26T16:00:00Z
test title 7 seven
财经
光明日报
1463443978554703872
0.42292467
1
8
false
2
content eight
2014-03-27T16:00:00Z
test title 8 eight
财经
燕赵都市报
1463443978556801024
0.42292467
1
9
false
3
content nine
2014-03-28T16:00:00Z
test title 9 nine
财经
北京晚报
1463443978558898176
0.42292467
1
10
false
2
content ten
2014-03-23T16:00:00Z
test title 10 ten
财经
燕赵都市报
1463443978559946752
0.42292467
Other Topics
0.0
true
5
6
7
8
9
10
View Code 注意事项:
使用该功能需要在%solr_home%/lib目录下添加扩展包:
从下载的solr项目中将
dist/apache-solr-clustering-*.jar,
contrib/clustering目录下的所有jar包,
contrib/clustering/downloads 目录下的所有jar包
加入到%solr_home%/lib中。
简单方法:直接拷贝源码中 dist 及 contrib 文件夹到 %solr_home%/collection1\conf中即可。
相似匹配(MoreLikeThis)
作用:查找相似的document
配置:在solrconfig.xml中配置如下
参数说明:
mlt:在查询时,打开/关闭 MoreLikeThisComponent 的布尔值。 (true|false)
mlt.count:可选。每一个结果要检索的相似文档数。 (> 0)
mlt.fl:用于创建 MLT 查询的字段。 模式中任何被储存的或含有检索词向量的字段。
mlt.maxqt:可选。查询词语的最大数量。由于长文档可能会有很多关键词语,这样 MLT 查询可能会很大,从而导致反应缓慢或可怕的 TooManyClausesException,该参数只保留最关键的词语。 (> 0)
举例:
http://localhost:8080/solr/mlt?q=ArticleId:5&mlt.true&mlt.fl=Title&mlt.mintf=1&mlt.mindf=1
该请求的意思是查找 ArticleId为 5 的 document ,然后返回与此 document 在 Title 字段上相似的其他 document。需要注意的是 mlt.fl 中的 field 的 termVector=true 才有效果
该请求执行结果如下:
0
34
2
5
false
2
content five
2014-03-24T16:00:00Z
test title 5 five
体育
燕赵都市报
1463443978500177920
2
6
false
3
content six
2014-03-25T16:00:00Z
test title 6 six
体育
北京晚报
1463443978552606720
1
7
false
1
content seven
2014-03-26T16:00:00Z
test title 7 seven
财经
光明日报
1463443978554703872
1
8
false
2
content eight
2014-03-27T16:00:00Z
test title 8 eight
财经
燕赵都市报
1463443978556801024
1
9
false
3
content nine
2014-03-28T16:00:00Z
test title 9 nine
财经
北京晚报
1463443978558898176
1
10
false
2
content ten
2014-03-23T16:00:00Z
test title 10 ten
财经
燕赵都市报
1463443978559946752
View Code 高亮显示
作用:将结果中与搜索关键词匹配的地方高亮显示。
配置:无需额外配置
参数说明:
hl 是否启用高亮显示 (true|false)
hl.fl 要进行高亮显示的字段,如需对多个字段显示用逗号分隔(hl.fl=name,name2,name3)
hl.simple.pre 高亮显示前缀标签 (默认)
hl.simple.post 高亮显示后缀标签(默认)
举例:
http://localhost:8080/solr/select?q=ArticleId:9&start=0&rows=10&hl=true&hl.fl=Title
该请求执行结果如下:
0
2
0
ArticleId:9
Title
true
10
1
9
false
3
content nine
2014-03-28T16:00:00Z
test title 9 nine
财经
北京晚报
1463443978558898176
test title 9 nine
View Code
二、索引篇
更新索引(update)
|
|
|