Solr常用检索语法
概要
本文对Solr常用检索语法进行介绍。
说明
为了更好的介绍Solr常用语法,约定Solr服务器地址:http://localhost:8080/solr,已部署检索实例sample。
Solr常用检索语法
结果集返回格式:&wt=json&indent=on
wt参数定义返回封装的数据格式,可选值有:xml、json、Python、ruby、php、csv等。indent参数为on或者true,定义返回结果是否缩进,一般xml类型的结果集不需要。
检索全部:/select?q=*:*&wt=xml
对指定字段进行检索:/select?q=(field:keyword)
field:word表示在Field域检索包含所有keyword关键字的文档
关键字高亮:&hl=true&hl.fl=field
hl=true表示开启高亮特性;hl.fl定义了需要高亮的域。
多个关键字的检索:/select? q=cjqk:(keyword1+OR+keyword2)
多个关键字通过or连接。同样的,使用keyword1+AND+keyword2得到多关键字的交集。
获取结果的分页数据:&start=5&rows=10
start定义了记录的开始序号;rows定义了返回的总记录数。
多关键字的Facet统计:&facet.query=field:keyword
keyword在Field的统计结果,比如
http://localhost:8080/solr/sample/select?q=text:(红色+OR+条纹)&facet.query=text:红色&facet.query=text:条纹&facet.query=text:(红色+AND+条纹)&wt=json&indent=true
自定义时间过滤统计:&fq=field:[2008-01-01T00:00:00Z TO 2008-08-25T00:00:00Z]
对时间域field中指定时间范围的结果统计,如:
http://localhost:8080/solr/sample/select?q=text:条纹&fq=sample:[2008-01-01T00:00:00Z TO 2008-08-25T00:00:00Z]&wt=json&indent=true
按时间间隔过滤统计
&facet=true&facet.date=field&facet.date.start=NOW/YEAR-10YEAR&facet.date.end=NOW%2B1DAY&facet.date.gap=%2B1YEAR
参数说明:facet:true表示开启Facet统计功能
facet.date:指定时间域
facet.date.start:指定时间范围的开始时间
facet.date.end:指定时间范围的结束时间
facet.date.gap:指定时间范围的时间间隔
http://localhost:8080/solr/sample/select?q=text:条纹&facet=true&facet.date=saleTime&facet.date.start=NOW/YEAR-10YEAR&facet.date.end=NOW%2B1DAY&facet.date.gap=%2B1YEAR
衍生的时间间隔过滤统计
过去一天的统计:
http://localhost:8080/solr/sample/select?&*:*&facet=true&facet.date=saleTime&facet.date.start=NOW/DAY-1DAY&facet.date.end=NOW/DAY&facet.date.gap=%2B1DAY&wt=json&indent=true
过去一周的统计:
http://localhost:8080/solr/sample/select?&*:*&facet=true&facet.date=saleTime&facet.date.start=NOW/DAY-7DAYS&facet.date.end=NOW/DAY&facet.date.gap=%2B7DAYS&wt=json&indent=true
过去一月的统计:
http://localhost:8080/solr/sample/select?&*:*&facet=true&facet.date=saleTime&facet.date.start=NOW/DAY-1MONTH&facet.date.end=NOW/DAY&facet.date.gap=%2B1MONTH&wt=json&indent=true
过去一年的统计:
http://localhost:8080/solr/sample/select?&*:*&facet=true&facet.date=saleTime&facet.date.start=NOW/DAY-1YEAR&facet.date.end=NOW/DAY&facet.date.gap=%2B1YEAR&wt=json&indent=true
检索记录数限制:&facet.mincount=1
facet.mincount指定返回的统计结果至少为1,小于1的不要显示出来。
字段域别名服务,对指定字段名的别名进行定义:&fl=nickname:field
指定field别名为nickname
facet统计:&facet.field=field_name
指定返回Facet的域名称
Pivot:&facet=true&facet.pivot=field1,field2,field3
参数说明:facet.pivot:指定具有层级结构的域(至少两个)
Pivot指定服务:&facet=true&fq=field:facet_name
说明:fq=field:facet_name 只获取facet_name在field域中的统计信息
获取指定检索core的状态信息。
定义为:/cores?action=STATUS&core=core_name
说明:action指定状态命令
core:指定检索core的名称
如:http://localhost:8080/solr/admin/cores?action=STATUS&core=core0
创建新的检索core。
定义为:
/cores?action=CREATE&name=coreX&instanceDir=path_to_instance_directory&config=config_file_name.xml&schema=schema_file_name.xml&dataDir=data
服务参数说明:action指定创建命令
Name:指定要创建的检索核心名
instanceDir:指定新创建的核心路径
config:指定新创建核心的配置路径
schema:指定新创建核心的schema文件路径
dataDir:指定新创建核心的索引数据存放路径
如:
http://localhost:8080/solr/admin/cores?action=CREATE&name=sample&instanceDir=D:/solr/example/solr/sample&config=D:/solr/example/solr/example/solr.xml&schema=schema.xml&dataDir=data
重新加载指定的core
定义为:/cores?action=RELOAD&core=core_name
参数说明:
Action:指定重新加载命令
Core:指定需要重新加载的检索核心的名称
如:http://localhost:8080/solr/admin/cores?action=RELOAD&core=sample
重命名检索实例core。
定义为:/cores?action=RENAME&core=core1&other=core2
参数说明:action:指定重命名命令
core:指定需要重命名的检索核心名
other:指定重名后的检索核心名
如:http://localhost:8080/solr/admin/cores?action=RENAME&core=sample&other=anothersample
卸载指定core。
定义为:/cores?action=UNLOAD&core=core_name
参数说明:
action:指定卸载命令
core:指定需要卸载的检索核心的名称
如:http://localhost:8080/solr/admin/cores?action=UNLOAD&core=sample