Solr 6.0 学习(五)solr基本查询和高级查询
参考:http://www.cnblogs.com/rainbowzc/p/4354224.html
查询参数
常用:
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>
<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>
其实想想和sql的group类似。
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>
<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>
配置schema.xml
配置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;"><field name="suggestText" type="suggestField" indexed="true" stored="true"/></span>
这里suggestText是从其他索引复制过来,这里是province。
- <span style="font-family:Microsoft YaHei;font-size:12px;"> <copyField source="province" dest="suggestText"/></span>
<span style="font-family:Microsoft YaHei;font-size:12px;"> <copyField source="province" dest="suggestText"/></span>
定义fieldType如下
- <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>
<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>
<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>
Solr 6.0 学习(五)solr基本查询和高级查询的更多相关文章
- accp8.0转换教材第4章MySQL高级查询(二)理解与练习
知识点:EXISTS子查询.NOT EXISTS子查询.分页查询.UNION联合查询 一.单词部分 ①exist存在②temp临时的③district区域 ④content内容⑤temporary暂时 ...
- MYSQL中的多类型查询及高级查询操作
离散查询select * from car where price=30 or price=40 or price=50 or price=60;select * from car where pri ...
- ThinkPHP 数据库操作(四) : 聚合查询、时间查询、高级查询
聚合查询 在应用中我们经常会用到一些统计数据,例如当前所有(或者满足某些条件)的用户数.所有用户的最大积分.用户的平均成绩等等,ThinkPHP为这些统计操作提供了一系列的内置方法,包括: 用法示例: ...
- C#-WebForm-LinQ-条件精确查询、高级查询
前台界面,并在后台绑定数据 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ca ...
- accp8.0转换教材第3章MySQL高级查询(一)理解与练习
一.单词部分 ①constraint约束②foreign外键③references参考 ④subquery子查询⑤inner内部的⑥join连接 二.预习部分 1.修改表SQL语句的关键字是什么 RE ...
- Mysql-学习笔记(==》连接查询_高级查询五)
CREATE TABLE t( tid INT UNSIGNED NOT NULL AUTO_INCREMENT, tname VARCHAR(30), PRIMARY KEY (tid))ENGIN ...
- solr6.0学习
solr6.0学习(一)环境搭建准备工作:目前最新版本6.0.下载solr 6.0:Solr6.0下载JDK8 下载jdk1.8:jdk1.8[solr6.0是基于jdk8开发的]tomcat8.0 ...
- Winform开发框架之通用高级查询模块--SNF快速开发平台3.3-Spring.Net.Framework
最近项目确实忙,但也是一直忙于有关项目和框架技术的事情,也一直致力于改善我的WInform开发框架.使得自己及客户使用起来更加方便,更加友好,更加高效. 在很多程序模块中都很常见,也是给客户扩展查询的 ...
- Mysql 基础 高级查询
在西面内容中 car 和 nation 都表示 表名 1.无论 高级查询还是简单查询 都用 select.. from..语句 from 后面 加表名 可以使一张表也可以是 ...
随机推荐
- jsp笔记总结
第一章 JavaWeb简介 1.什么是Web应用程序 什么是Web应用程序是一种通过Web访问的应用程序.Web应用程序一个最大优点就是用户很容易访问.用户只需要有浏览器即可,不需要再安装其他软件.W ...
- Jmter操作数据库
1.导入jdbc的jar包,因为jmeter本身不能直接连接mysql,所以需要导入第三方的jar包,来连接mysql,如下操作: 2.创建数据库连接如下: 3.配置mysql的url.端口号.账号. ...
- zookeeper 学习命令
ls /TianheSoft/nodesls /TianheSoft/nodes/localhost_2181-0000000000ls /TianheSoft/propsls /TianheSoft ...
- ranch分析学习(二)
紧接上篇,今天我们来分析监督树的工作者,打工仔执行任务的人.废话不多少我们直接进入正题. 3.ranch_server.erl 整个文件的功能主要是存储tcp对应参数的的信息.信息的存储方式采用的 ...
- Android学习笔记之Android Studio添加新的Activity
1.创建Android项目工程:AndroidTest 创建过程可参考网上诸多教程. 2.添加新的Activity,步骤如下 a. 在layout文件夹上右键,New-Activity-相应Activ ...
- 白话machine learning之Loss Function
转载自:http://eletva.com/tower/?p=186 有关Loss Function(LF),只想说,终于写了 一.Loss Function 什么是Loss Function?wik ...
- java sundry tips
1.关于Arrays 记得binarySearch方法返回的int 类型的数值的含义. If the array contains multiple elements with the spec ...
- 可能是 BJOI2019 Day1 题解?
T1 给一个有空白字符的串 $S$,和若干模板串 $X_i$,初始 $Ans = 1$,每当一个模板串在 $S$ 中作为子串出现时,$Ans$ 会乘以 $X_i$ 的权值 $Val_i$,然后如果 $ ...
- 自己理解的java工厂模式,希望对大家有所帮助
[http://www.360doc.com/content/11/0824/17/3034429_142983837.shtml] 这两天突然想学学java源代码,不过看到一篇文章说看java源代码 ...
- 学习Selenium同学必看
本文转载 作者:灰蓝蓝蓝蓝蓝蓝链接:http://www.jianshu.com/p/5188cb3ab790來源:简书著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.更多技术博客 ...