solr基本查询和高级查询
查询参数
常用:
q - 查询字符串,必须的。fl - 指定返回那些字段内容,用逗号或空格分隔多个。start - 返回第一条记录在完整找到结果中的偏移位置,0开始,一般分页用。rows - 指定返回结果最多有多少条记录,配合start来实现分页。sort - 排序,格式:sort=
timestamp:[* TO NOW]
createdate:[1976-03-06T23:59:59.999Z TO *]
createdate:[1995-12-31T23:59:59.999Z TO 2007-03-06T00:00:00Z]
pubdate:[NOW-1YEAR/DAY TO NOW/DAY+1DAY]
createdate:[1976-03-06T23:59:59.999Z TO 1976-03-06T23:59:59.999Z+1YEAR]
createdate:[1976-03-06T23:59:59.999Z/YEAR TO 1976-03-06T23:59:59.999Z]
不常用:
defType:q.op - 覆盖schema.xml的defaultOperator(有空格时用"AND"还是用"OR"操作逻辑),一般默认指定df - 默认的查询字段,一般默认指定qt - (query type)指定那个类型来处理查询请求,一般不用指定,默认是standard。
其它:
indent - 返回的结果是否缩进,默认关闭,用 indent=true|on 开启,一般调试json,php,phps,ruby输出才有必要用这个参数。version- 查询语法的版本,建议不使用它,由服务器指定默认值。
检索运算符
: 指定字段查指定值,如返回所有值:? 表示单个任意字符的通配* 表示多个任意字符的通配(不能在检索的项开始使用*或者?符号)~ 表示模糊检索,如检索拼写类似于"roam"的项这样写:roam~将找到形如foam和roams的单词;roam~0.8,检索返回相似度在0.8以上的记录。 邻近检索,如检索相隔10个单词的"apache"和"jakarta","jakarta apache"~10^ 控制相关度检索,如检索jakarta apache,同时希望去让"jakarta"的相关度更加好,那么在其后加上""符号和增量值,即jakarta4 apache布尔操作符AND、||布尔操作符OR、&&布尔操作符NOT、!、-(排除操作符不能单独与项使用构成查询)+ 存在操作符,要求符号"+"后的项必须在文档相应的域中存在() 用于构成子查询[] 包含范围检索,如检索某时间段记录,包含头尾,date:[200707 TO 200710]{}不包含范围检索,如检索某时间段记录,不包含头尾,date:{200707 TO 200710}" 转义操作符,特殊字符包括+ - && || ! ( ) { } [ ] ^ " ~ * ? : "
示例
1. 查询所有
http://localhost:8080/solr/primary/select?q=*:*
2. 限定返回字段
http://localhost:8080/solr/primary/select?q=*:*&fl=productId
表示:查询所有记录,只返回productId字段
3. 分页
http://localhost:8080/solr/primary/select?q=*:*&fl=productId&rows=6&start=0
表示:查询前六条记录,只返回productId字段
4. 增加限定条件
http://localhost:8080/solr/primary/select?q=*:*&fl=productId&rows=6&start=0&fq=category:2002&fq=namespace:d&fl=productId+category&fq=en_US_city_i:1101
表示:查询category=2002、en_US_city_i=110以及namespace=d的前六条记录,只返回productId和category字段
5. 添加排序
http://localhost:8080/solr/primary/select?q=*:*&fl=productId&rows=6&start=0&fq=category:2002&fq=namespace:d&sort=category_2002_sort_i+asc
表示:查询category=2002以及namespace=d并按category_2002_sort_i升序排序的前六条记录,只返回productId字段
6. facet查询
现实分组统计结果
http://localhost:8080/solr/primary/select?q=*:*&fl=productId&fq=category:2002&facet=true&facet.field=en_US_county_i&facet.field=en_US_hotelType_s&facet.field=price_p&facet.field=heatRange_i
http://localhost:8080/solr/primary/select?q=*:*&fl=productId&fq=category:2002&facet=true&facet.field=en_US_county_i&facet.field=en_US_hotelType_s&facet.field=price_p&facet.field=heatRange_i&facet.query=price_p:[300.00000+TO+*]
facet
基本参数:
具体实例和用法参考:
参考:facet用法
group
查询的url:
http://localhost:8080/solr/my_solr/select?q=province:湖&wt=xml&indent=true&rows=100&group=true&group.field=province&group.field=province&group.limit=20&group.ngroups=true
group=true:设置开启分组查询
group.field=province:设置分组字段
group.limit=20:设置分组后展示分组下数据量
group.ngroups=true:设置为true表示会返回分组的分组
下面是查询结果:
[html]
view plain
copy
其实想想和sql的group类似。
suggest 联想查询
配置solrconfig.xml文件。
[html]
view plain
copy
配置schema.xml
配置field的name和type 需要和solrconfig中对应
[html]
view plain
copy
这里suggestText是从其他索引复制过来,这里是province。
[html]
view plain
copy
定义fieldType如下
[html]
view plain
copy
http://localhost:8080/solr/my_solr/suggest?suggest=true&suggest.build=true&suggest.dictionary=mySuggester&suggest.count=3&wt=xml&suggest.q="湖"
suggest=true: 表示开启suggest功能
suggest.build=true:
如果设置为true,这个请求会导致重建suggest索引。这个字段一般用于初始化的操作中,在线上环境,一般不会每个请求都重建索引,如果线上你希望保持字典最新,最好使用buildOnCommit或者buildOnOptimize来操作。如:searchComponent中添加
suggest.dictionary=mySuggester:其中mySuggester是在searchComponent中配置的
suggest.q="湖":表示进行查询建议的文本
suggest.count=3:表示返回的数据量
一般常用的就是这些属性,具体详情可以查看:http://www.cnblogs.com/yjf512/p/5166382.html
上述url最终返回结果如下:
[html]
view plain
copy