Java 在Spring数据解决方案中使用POST方法进行大查询
我在我的项目中使用SpringDataSolr。在某些情况下,生成的Solr查询太大(例如15Kb+)并导致Solr异常。此解决方案: 对于某些查询仍然失败。 因为通过POST直接将这些查询发送到Solr很好,所以我选择朝这个方向工作。我在SpringDataSolr中找不到为查询配置首选方法(GET/POST)的任何方法。因此,我得到了以下解决方案:扩展SolrServerJava 在Spring数据解决方案中使用POST方法进行大查询,java,spring,solr,spring-data-solr,Java,Spring,Solr,Spring Data Solr,我在我的项目中使用SpringDataSolr。在某些情况下,生成的Solr查询太大(例如15Kb+)并导致Solr异常。此解决方案: 对于某些查询仍然失败。 因为通过POST直接将这些查询发送到Solr很好,所以我选择朝这个方向工作。我在SpringDataSolr中找不到为查询配置首选方法(GET/POST)的任何方法。因此,我得到了以下解决方案:扩展SolrServer public class CustomSolrServer extends HttpSolrServer {
public class CustomSolrServer extends HttpSolrServer {
public CustomSolrServer(String home, String core) {
super(home);
setCore(core);
}
@Override
public QueryResponse query(SolrParams params) throws SolrServerException {
METHOD method = METHOD.GET;
if (isBigQuery(params)) {
method = METHOD.POST;
}
return new QueryRequest( params, method ).process( this );
}
}
(跳过了一些细节,setCore()和isBigQuery()都是琐碎的,也被跳过了)
并将其用作SolrConfiguration.class中的SolrServer bean:
@Configuration
@EnableSolrRepositories(basePackages = { "com.vvy.repository.solr" }, multicoreSupport=false)
@Import(value = SolrAutoConfiguration.class)
@EnableConfigurationProperties(SolrProperties.class)
public class SolrConfiguration {
@Autowired
private SolrProperties solrProperties;
@Value("${spring.data.solr.core}")
private String solrCore;
@Bean
public SolrServer solrServer() {
return new CustomSolrServer(solrProperties.getHost(),solrCore) ;
}
}
这工作正常,但有两个缺点:我必须将MultiResupport设置为false。之所以这样做,是因为当Spring Data Solr从接口实现存储库时,其上的MultiResupport使用MultiResolrServerFactory并尝试为每个核心存储一个服务器,这是通过将它们克隆到保留映射来完成的。当然,它会在定制的SolrServer上崩溃,因为SolrServerUtils不知道如何克隆它。此外,我必须手动设置核心,而不是从实体类的@SolrDocument注释参数中提取Spring数据
以下是问题
1) 主要和一般性的问题:有没有合理的方法来解决SpringDataSolr中查询过长的问题(或者更具体地说,使用POST而不是GET)?
2) 一个小问题:在Spring Data Solr中是否有一种合理的方法来定制SolrServer,同时维护多再支持?问题1的答案:是的,你可以使用POST而不是GET 问题2的答案:是的,你已经做了一半。除了以下几点:
package com.x.x.config;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.SolrParams;
public class HttpSolrServer extends org.apache.solr.client.solrj.impl.HttpSolrServer {
public HttpSolrServer(String host) {
super(host);
}
@Override
public QueryResponse query(SolrParams params) throws SolrServerException {
SolrRequest.METHOD method = SolrRequest.METHOD.POST;
return new QueryRequest(params, method).process(this);
}
}
@Configuration
@EnableSolrRepositories(basePackages = { "com.vvy.repository.solr" }, multicoreSupport=true)
@Import(value = SolrAutoConfiguration.class)
@EnableConfigurationProperties(SolrProperties.class)
public class SolrConfiguration {
@Autowired
private SolrProperties solrProperties;
@Bean
public SolrServer solrServer() {
return new com.x.x.config.HttpSolrServer(solrProperties.getHost()) ;
}
}
1) 您必须将“CustomSolrServer”重命名为“HttpSolrServer”,您可以检查方法
org.springframework.data.solr.server.support.SolrServerUtils#clone(T,java.lang.String)
是有原因的
2) 您不必指定具体的核心名称。您可以使用注释指定核心名称
org.springframework.data.solr.core.mapping.SolrDocument
关于相应的solr模型
3) 将MultiResupport设置为true
根据您的类示例,它们应该如下所示:
package com.x.x.config;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.SolrParams;
public class HttpSolrServer extends org.apache.solr.client.solrj.impl.HttpSolrServer {
public HttpSolrServer(String host) {
super(host);
}
@Override
public QueryResponse query(SolrParams params) throws SolrServerException {
SolrRequest.METHOD method = SolrRequest.METHOD.POST;
return new QueryRequest(params, method).process(this);
}
}
@Configuration
@EnableSolrRepositories(basePackages = { "com.vvy.repository.solr" }, multicoreSupport=true)
@Import(value = SolrAutoConfiguration.class)
@EnableConfigurationProperties(SolrProperties.class)
public class SolrConfiguration {
@Autowired
private SolrProperties solrProperties;
@Bean
public SolrServer solrServer() {
return new com.x.x.config.HttpSolrServer(solrProperties.getHost()) ;
}
}
ps:最新的SpringDataSolr3.x.x已经支持自定义查询请求方法,请参阅