摘要: 查询参数fq即过滤查询(filter query)。一般用来将查询的结果限定在某一范围,其作用类似于参数q,有时候可以被q取代。
限制某一字段值
- 搜索,有时候需要建立在某一特定条件下进行的。比如,我搜索的数据希望其类型是图书,那么可以用使用过滤条件
fq=type_s:图书
,我的HTTP请求如下:
http://localhost:8080/solr/core/select?q=*:*&fq=type_s:图书
类似于这样:
http://localhost:8080/solr/core/select?q=type_s:图书
返回的结果中包含所有类型字段为图书
的文档,当然首先它要满足参数q
的条件。
{
"responseHeader":{
"status":0,
"QTime":1,
"params":{
"q":"*:*",
"indent":"true",
"fq":"type_s:图书",
"wt":"json"}},
"response":{"numFound":40,"start":0,"docs":[
{
"id":"1900554874",
"title_s":"你都不配我毒舌【精装】",
"author_s":"金国栋",
"category_s":"穿越/言情",
"page_i":2980,
"price_d":29.8,
"odertime_dt":"2015-08-02T21:47:04Z",
"publisher_s":"不知名出版社",
"type_s":"图书",
"description_s":"有人问我你怎么不去写金戈铁马,男人应该...",
"_version_":1552794974378000384},
},
...
]
}
}
- 这次,我希望搜索的数据希望其类型是不是图书,可以添加负号(-)在对应的filed前,过滤条件可以写成
fq=-type_s:图书
,我的HTTP请求如下:
http://localhost:8080/solr/core/select?q=*:*&fq=-type_s:图书
返回的数据就是类型不为图书
的文档,当然首先它要满足参数q
的条件。
{
"responseHeader":{
"status":0,
"QTime":1,
"params":{
"q":"*:*",
"indent":"true",
"fq":"-type_s:图书",
"wt":"json"}},
"response":{"numFound":66,"start":0,"docs":[
{
"id":"9787535449481",
"catagory_s":"文学",
"title_s":"我不是潘金莲",
"author_s":"刘震云",
"pubData_s":"2012-08-01",
"price_d":7.99,
"size_s":"1.61M",
"publisher_s":"长江文艺出版社",
"type_s":"电子书",
"_version_":1551516474391134208
},
...
]
}
}
限制某一范围
- 有时候,我希望搜索结果被限制在某一区间之内。比如查询价格在50到100之间的图书。过滤条件可以写成
price_d:[50 TO 100]
,(price是double类型)其中字段名后必须有方括号包裹数值,单词TO
必须是大写,且前后有空格。我的HTTP请求如下:
http://localhost:8080/solr/core/select?q=*:*&fq=price_d:[50 TO 100]
结果就是符合这一范围的文档,当然首先它要满足参数q
的条件。
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"*:*",
"indent":"true",
"fq":"price_d:[50 TO 100]",
"wt":"json"}},
"response":{"numFound":8,"start":0,"docs":[
{
"id":"1010296100",
"catagory_s":"文学",
"title_s":"平凡的世界",
"author_s":"路遥",
"pubData_s":"2016-09-01",
"price_d":60.5,
"pages_i":688,
"publisher_s":"北京十月文艺出版社",
"type_s":"书籍",
"_version_":1551505799913668608
},
...
]
}
}
- 某一时间段的处理:Solr默认的时间格式是yyyy-mm-ddTHH:MM:SSZ,如果要指定某一时间范围,需要满足其时间格式,比如,我要搜索的书籍的出版时间在2015年10月到2016年,过滤条件可以写成
odertime_dt:[2015-10-01T00:00:00Z TO 2015-12-31T23:59:59Z]
,(odertime_dt是datetime类型)。我的HTTP请求如下:
http://localhost:8080/solr/core/select?q=*:*&fq=odertime_dt:[2015-10-01T00:00:00Z TO 2015-12-31T23:59:59Z]
结果就是符合这一范围的文档,当然首先它要满足参数q
的条件。
{
"responseHeader":{
"status":0,
"QTime":1,
"params":{
"q":"*:*",
"indent":"true",
"fq":"odertime_dt:[2015-10-01T00:00:00Z TO 2015-12-31T23:59:59Z]",
"wt":"json"}},
"response":{"numFound":20,"start":0,"docs":[
{
"id":"1900599683",
"title_s":"狗日的战争(套装共3册)",
"author_s":"冰河",
"category_s":"历史普及读物",
"page_i":8800,
"price_d":999.0,
"odertime_dt":"2015-10-02T21:47:04Z",
"publisher_s":"不知名出版社",
"type_s":"图书",
"description_s":"漫长的战争硝烟终于散尽,老旦带着残缺之躯幸存下来,而真正的折磨才刚刚开始……",
"_version_":1552794664585658368
},
...
]
}
}