solr的一些查询参数

fl: 是逗号分隔的列表,用来指定文档结果中应返回的 Field 集。默认为 “*”,指所有的字段。

defType: 指定query parser,常用defType=lucene, defType=dismax, defType=edismax

q: query。

q.alt: 当q字段为空时,用于设置缺省的query,通常设置q.alt为*:*。

qf: query fields,指定solr从哪些field中搜索。

pf: 用于指定一组field,当query完全匹配pf指定的某一个field时,来进行boost。

简言之pf的作用是boosting phrases over words。

fq: filter query,过虑查询。

mm: minimal should match。Solr支持三种查询clause,即“必须出现”, “不能出现”和“可以出现”,分别对应于AND, -, OR。

在默认情况下,使用OR这个clause。mm用于设置在使用OR这个clause时,需要出现最少的满足条件的clause数量,详见这里

ps: Phrase Slop. Amount of slop on phrase queries built for "pf" fields (affects boosting). ps is about pf parameter. ps affects boosting, if you play with ps value, numFound and result set do not change. But the order of result set change. This is about the phrase query that is constructed out of the entire "q" param. ps is slop applied to the phrases created from the entire query for evaluating pf boosts. ps will only (potentially) change the ranked ordering of your result set, by loosening what a "phrase match" means to the pf boost.

ps的例子:

Lets say your query is apache solr. (without quotation marks) 

Lets say these three documents contains all of these words and returned. 

1-) solr is built on the top of apache lucene. 
2-) apache solr is fast, mature and popular. 
3-) solr is hosted under apache umbrella. 

Even if you don't use pf and ps parameters, those documents will be in result set anyway. Lets say that they appear in this order 1,2,3. 

Then we include pf and ps parameter, q=apache solr&pf=title^1.2&ps=1 
Second document is boosted, lets say it comes first now. The order is changed. The documents - that have the all query words close each other - are boosted. Again the same three documents are returned.

qs: Query Phrase Slop. Amount of slop on phrase queries explicitly included in the user's query string (in qf fields; affects matching). qs affects matching. If you play with qs, numFound changes. This parameter is about when you have explicitphrase query in your raw query. i.e. &q="apache lucene" . qs is slop applied to phrases explicitly in the &q with double quotes. qs will (potentially) change your result set.

tie: tie breaker。

bq: 对某个field的value进行boost,例如brand:IBM^5.0。

bf: Function (with optional boosts) that will be included in the user's query to influence the score. Any function supported natively by Solr can be used, along with a boost value, e.g.: recip(rord(myfield),1,2,3)^1.5

wt: writer type,指定输出格式,可以有 xml, json, php, phps。

q.op覆盖schema.xmldefaultOperator(有空格时用"AND"还是用"OR"操作逻辑)。 
df
默认的查询字段。 
qt
query type,指定那个类型来处理查询请求,一般不用指定,默认是standard

### 注入原理 Solr查询参数注入恶意代码的原理主要源于对用户输入的查询参数未进行严格的过滤和验证。当用户在搜索框或通过API接口提交查询参数时,若应用程序直接将这些参数拼接到Solr查询语句中,而没有对特殊字符进行处理,攻击者就可以利用这一漏洞注入恶意代码。 例如,在Solr中常见的查询语句格式为 `q=field:value`,如果应用程序将用户输入的 `value` 部分直接拼接进查询语句,攻击者可以输入包含特殊字符的内容,如 `*:*` (表示查询所有文档),或者构造更为复杂的恶意查询语句,利用Solr的语法特性来执行非法操作,像绕过访问控制、获取敏感数据等。 ### 防范措施 - **输入验证和过滤**:对用户输入的查询参数进行严格的验证和过滤,只允许合法的字符和格式通过。可以使用白名单机制,明确规定允许的字符集和查询格式,拒绝包含非法字符的输入。例如,只允许字母、数字和特定的符号,对于其他字符进行过滤或拒绝。 ```python import re def validate_query(query): pattern = r'^[a-zA-Z0-9 :]+$' if re.match(pattern, query): return query else: return None user_query = "malicious*:*query" validated_query = validate_query(user_query) if validated_query: # 执行查询 pass else: # 拒绝非法输入 pass ``` - **使用参数查询**:避免直接将用户输入的参数拼接到查询语句中,而是使用Solr提供的参数查询功能。参数查询会自动对输入进行转义和处理,防止恶意代码注入。不同的编程语言和Solr客户端库有不同的实现方式,以下是Java的示例: ```java import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.response.QueryResponse; public class SolrQueryExample { public static void main(String[] args) throws Exception { String solrUrl = "http://localhost:8983/solr/your_core"; HttpSolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build(); String userInput = "valid_query"; SolrQuery query = new SolrQuery(); query.setQuery("field:" + userInput); // 这里可以使用参数化方式 QueryResponse response = solrClient.query(query); solrClient.close(); } } ``` - **权限控制**:对Solr的访问进行严格的权限控制,只授予应用程序必要的查询权限,避免使用具有过高权限的账户进行查询操作。同时,限制对敏感数据的访问,确保只有授权的用户和操作才能获取这些数据。 - **定期更新和监控**:及时更新Solr到最新版本,因为新版本通常会修复已知的安全漏洞。同时,对Solr的日志进行监控,及时发现异常的查询行为,如频繁的异常查询、大量的数据获取请求等,并采取相应的措施。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值