设置使用SolrJ
问题描述:
我在Solr的配置自定义HTTP滤波器,它首先被调用(org.apache.solr.servlet.SolrDispatchFilter之前执行),选择那些命中solr的每个请求的定制请求头。自定义过滤器会在所有传入的solr请求中查找特定的请求标头,并且只有当它存在时,才会将其发送给solr进一步处理。设置使用SolrJ
我使用SolrJ进行某些查询。查询Solr到SolrJ时,有什么方法可以设置HTTP请求标头?
我的Solr和SorlJ版本是5.4.0
答
您可以扩展HttpSolrClient
创建自己的类MyHttpSolrClient
和定制的executeMethod
添加自定义请求头的行为。
public class MyHttpSolrClient extends HttpSolrClient {
public MyHttpSolrClient(String baseURL) {
super(baseURL);
}
public MyHttpSolrClient(String baseURL, HttpClient client) {
super(baseURL, client);
}
public MyHttpSolrClient(String baseURL, HttpClient client, ResponseParser parser) {
super(baseURL, client, parser);
}
protected NamedList<Object> executeMethod(HttpRequestBase method, final ResponseParser processor) throws SolrServerException {
// **Here you add your custom header**
method.addHeader("Name", "Value");
return super.executeMethod(method, processor);
}
}