首页 > 代码库 > Solr自学笔记 2 —— Solr 查询,排序, 高亮
Solr自学笔记 2 —— Solr 查询,排序, 高亮
1.查询(Querying Data) --q 文档 fl 表示相应的属性
1) 内容: 搜索过程是通过带q参数的GET HTTP请求select URL.同时可以通过传递表示可选择的请求参数的数字给请求处理器来控制相应的返回信息。(You can pass a number of optional request parameters to the request handler to control what information is returned)
下面f1参数来控制相应的返回的属性值:
Solr Query Syntax (q参数的规则)地址: http://wiki.apache.org/solr/SolrQuerySyntax
-
- q=video&fl=name,id (return only name and id fields)
- q=video&fl=name,id,score (return relevancy score as well)
- q=video&fl=*,score (return all stored fields, as well as relevancy score)
- q=video&sort=price desc&fl=name,id,price (add sort specification: sort by price descending)
- q=video&wt=json (return response in JSON format)
查询例子1:
GET请求:http://localhost:8983/solr/collection1/select?q=*:*&fl=name,id&wt=json&fl=*
分析:q=*:* 表示 属性:属性值中包含的词
fl=* 表示 查询的结果返回所有的属性值
返回的内容:
{
"responseHeader": {
"status": 0,
"QTime": 1,
"params": {
"fl": [
"name,id",
"*"
],
"q": "*:*",
"wt": "json"
}
},
"response": {
"numFound": 2,
"start": 0,
"docs": [
{
"id": "SOLR1000",
"name": "Solr, the Enterprise Search Server",
"manu": "Apache Software Foundation",
"cat": [
"software",
"search"
],
"features": [
"Advanced Full-Text Search Capabilities using Lucene",
"Optimized for High Volume Web Traffic",
"Standards Based Open Interfaces - XML and HTTP",
"Comprehensive HTML Administration Interfaces",
"Scalability - Efficient Replication to other Solr Search Servers",
"Flexible and Adaptable with XML configuration and Schema",
"Good unicode support: héllo (hello with an accent over the e)"
],
"price": 0,
"price_c": "0,USD",
"popularity": 10,
"inStock": true,
"incubationdate_dt": "2006-01-17T00:00:00Z",
"_version_": 1546511824707387400
},
{
"id": "3007WFP",
"name": "Dell Widescreen UltraSharp 3007WFP",
"manu": "Dell, Inc.",
"manu_id_s": "dell",
"cat": [
"electronics and computer1"
],
"features": [
"30\" TFT active matrix LCD, 2560 x 1600, .25mm dot pitch, 700:1 contrast"
],
"includes": "USB cable",
"weight": 401.6,
"price": 2199,
"price_c": "2199,USD",
"popularity": 6,
"inStock": true,
"store": "43.17614,-90.57341",
"_version_": 1546511824826925000
}
]
}
}
查询例子2:
GET请求:http://localhost:8983/solr/collection1/select?q=Search&fl=name,id
&wt=json&fl=*
分析:q=Search 表示 所有属性值中必包含的这个词,不一定所有属性都包含,也可以为*。
fl=* 表示 查询的结果返回所有的属性值
返回的内容:
{
"responseHeader": {
"status": 0,
"QTime": 36,
"params": {
"fl": [
"name,id",
"*"
],
"q": "Search",
"wt": "json"
}
},
"response": {
"numFound": 1,
"start": 0,
"docs": [
{
"id": "SOLR1000",
"name": "Solr, the Enterprise Search Server",
"manu": "Apache Software Foundation",
"cat": [
"software",
"search"
],
"features": [
"Advanced Full-Text Search Capabilities using Lucene",
"Optimized for High Volume Web Traffic",
"Standards Based Open Interfaces - XML and HTTP",
"Comprehensive HTML Administration Interfaces",
"Scalability - Efficient Replication to other Solr Search Servers",
"Flexible and Adaptable with XML configuration and Schema",
"Good unicode support: h¨|llo (hello with an accent over the e)"
],
"price": 0,
"price_c": "0,USD",
"popularity": 10,
"inStock": true,
"incubationdate_dt": "2006-01-17T00:00:00Z",
"_version_": 1546511824707387400
}
]
}
}
2. 排序(Sorting)--sort 属性
-
-
- 普通排序
-
- q=video&sort=price desc
- q=video&sort=price asc
- q=video&sort=inStock asc, price desc
-
"score"相关性分数排序
-
- q=video&sort=score desc
- q=video&sort=inStock asc, score desc
-
复杂函数排序
-
- q=video&sort=div(popularity,add(price,1)) desc
- 如果没有指定相应的排序,则默认score desc排序( the default is score desc to return the matches having the highest relevancy.)
-
3.高亮 (Highlighting)
Hit highlighting returns relevant snippets of each returned document, and highlights terms from the query within those context snippets.
高亮的格式:...&q=video card&fl=name,id&hl=true&hl.fl=name,features
返回的形式:这会导致高亮的部分会显示在返回值上相关的高亮的词语上包含在<em></em>(for emphasis) tags
更多帮助地址:http://wiki.apache.org/solr/HighlightingParameters
get请求URL: http://localhost:8983/solr/collection1/select?q=Search&fl=name,id&wt=json
&hl=true&hl.fl=features
返回值:
{
"responseHeader": {
"status": 0,
"QTime": 46,
"params": { //请求参数
"fl": "name,id", //请求返回的属性值
"q": "Search", //关键词搜索
"hl.fl": "features", //高亮所包含的关键词
"wt": "json", //返回的形式
"hl": "true" // 是否高亮
}
},
"response": {
"numFound": 1,
"start": 0,
"docs": [
{
"id": "SOLR1000",
"name": "Solr, the Enterprise Search Server"
}
]
},
"highlighting": {
"SOLR1000": {
"features": [
"Advanced Full-Text <em>Search</em> Capabilities using Lucene"
]
}
}
}
Solr自学笔记 2 —— Solr 查询,排序, 高亮
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。