查询参数
常用:
q
- 查询字符串,必须的。fl
- 指定返回那些字段内容,用逗号或空格分隔多个。start
- 返回第一条记录在完整找到结果中的偏移位置,0开始,一般分页用。rows
- 指定返回结果最多有多少条记录,配合start来实现分页。sort
- 排序,格式:sort=<field name>+<desc|asc>[,<field name>+<desc|asc>]
。示例:(inStock desc, price asc)表示先 "inStock" 降序, 再 "price" 升序,默认是相关性降序。wt
- (writer type)指定输出格式,可以有 xml, json, php, phps。fq
- (filter query)过虑查询,作用:在q查询符合结果中同时是fq查询符合的,例如:q=mm&fq=date_time:[20081001 TO 20091031]
,找关键字mm,并且date_time是20081001到20091031之间的- 注意:solr查询时间为UTC 格式(2013-04-25T22:22:12.000Z)
- 如:
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表示会返回分组的分组
下面是查询结果:
- <response>
- <lst name="responseHeader">
- <int name="status">0</int>
- <int name="QTime">81</int>
- <lst name="params">
- <str name="q">province:湖</str>
- <str name="indent">true</str>
- <str name="group.limit">20</str>
- <str name="rows">100</str>
- <str name="group.ngroups">true</str>
- <str name="wt">xml</str>
- <arr name="group.field">
- <str>province</str>
- <str>province</str>
- </arr>
- <str name="group">true</str>
- </lst>
- </lst>
- <lst name="grouped">
- <lst name="province">
- <int name="matches">13</int>
- <int name="ngroups">3</int>
- <arr name="groups">
- <lst>
- <str name="groupValue">1</str>
- <result name="doclist" numFound="1" start="0">
- <doc>
- <str name="id">10</str>
- <str name="province">湖1</str>
- <str name="suggestText">湖1</str>
- <long name="_version_">1535191400888401920</long>
- </doc>
- </result>
- </lst>
- <lst>
- <str name="groupValue">2</str>
- <result name="doclist" numFound="11" start="0">
- <doc>
- <str name="id">11</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191406371405824</long>
- </doc>
- <doc>
- <str name="id">13</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191409744674816</long>
- </doc>
- <doc>
- <str name="id">14</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191412264402944</long>
- </doc>
- <doc>
- <str name="id">15</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191414894231552</long>
- </doc>
- <doc>
- <str name="id">16</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191417461145600</long>
- </doc>
- <doc>
- <str name="id">17</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191419842461696</long>
- </doc>
- <doc>
- <str name="id">18</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191422327586816</long>
- </doc>
- <doc>
- <str name="id">19</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191424609288192</long>
- </doc>
- <doc>
- <str name="id">20</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191431052787712</long>
- </doc>
- <doc>
- <str name="id">23</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191435711610880</long>
- </doc>
- <doc>
- <str name="id">30</str>
- <str name="province">湖2</str>
- <str name="suggestText">湖2</str>
- <long name="_version_">1535191428366336000</long>
- </doc>
- </result>
- </lst>
- <lst>
- <str name="groupValue">111</str>
- <result name="doclist" numFound="1" start="0">
- <doc>
- <str name="id">111</str>
- <str name="province">湖111</str>
- <str name="suggestText">湖111</str>
- <long name="_version_">1535191723089592320</long>
- </doc>
- </result>
- </lst>
- </arr>
- </lst>
- <lst name="province">...</lst>
- </lst>
- </response>
suggest 联想查询
配置solrconfig.xml文件。
- <span style="font-family:Microsoft YaHei;font-size:12px;"><!-- suggest 配置 start -->
- <searchComponent name="suggest" class="solr.SuggestComponent">
- <lst name="suggester">
- <str name="name">mySuggester</str>
- <str name="lookupImpl">FuzzyLookupFactory</str> <!-- org.apache.solr.spelling.suggest.fst -->
- <str name="dictionaryImpl">DocumentDictionaryFactory</str> <!-- org.apache.solr.spelling.suggest.HighFrequencyDictionaryFactory -->
- <!-- 索引字段 -->
- <str name="field">suggestText</str>
- <!-- 索引的type -->
- <str name="suggestAnalyzerFieldType">suggestField</str>
- <str name="buildOnStartup">false</str>
- </lst>
- </searchComponent>
- <requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy">
- <lst name="defaults">
- <str name="suggest">true</str>
- <str name="suggest.count">10</str>
- </lst>
- <arr name="components">
- <str>suggest</str>
- </arr>
- </requestHandler>
- <!-- suggest 配置 end --></span>
配置field的name和type 需要和solrconfig中对应
- <span style="font-family:Microsoft YaHei;font-size:12px;"><field name="suggestText" type="suggestField" indexed="true" stored="true"/></span>
- <span style="font-family:Microsoft YaHei;font-size:12px;"> <copyField source="province" dest="suggestText"/></span>
- <span style="font-family:Microsoft YaHei;font-size:12px;"><fieldType class="solr.TextField" name="suggestField"
- positionIncrementGap="100">
- <analyzer>
- <tokenizer class="solr.StandardTokenizerFactory"/>
- <filter class="solr.StandardFilterFactory"/>
- <filter class="solr.LowerCaseFilterFactory"/>
- </analyzer>
- </fieldType></span>
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中添加<str name="buildOnCommit">true</str>
suggest.dictionary=mySuggester:其中mySuggester是在searchComponent中配置的<str name="name">mySuggester</str>
suggest.q="湖":表示进行查询建议的文本
suggest.count=3:表示返回的数据量
一般常用的就是这些属性,具体详情可以查看:http://www.cnblogs.com/yjf512/p/5166382.html
上述url最终返回结果如下:
- <response>
- <lst name="responseHeader">
- <int name="status">0</int>
- <int name="QTime">1</int>
- </lst>
- <lst name="suggest">
- <lst name="mySuggester">
- <lst name=""湖"">
- <int name="numFound">3</int>
- <arr name="suggestions">
- <lst>
- <str name="term">湖1</str>
- <long name="weight">0</long>
- <str name="payload"/>
- </lst>
- <lst>
- <str name="term">湖111</str>
- <long name="weight">0</long>
- <str name="payload"/>
- </lst>
- <lst>
- <str name="term">湖2</str>
- <long name="weight">0</long>
- <str name="payload"/>
- </lst>
- </arr>
- </lst>
- </lst>
- </lst>
- </response>