gpt4 book ai didi

java - Spring solr动态字段注解

转载 作者:搜寻专家 更新时间:2023-11-01 01:49:40 24 4
gpt4 key购买 nike

我正在尝试执行类似从文档中检索一个特定字段的查询,我在执行查询时没有出现运行时错误,但我没有得到我应该从中检索的 3 个字段查询,只有日期和来源,但没有变量,应该返回所有变量的变量都是空值。如何选择我只想在查询中检索的字段?

目前我的查询是这样的:

  @Query(value = "id:?0", fields = {"?1","date","origin"})
List<Data> getRecord(String id,String field);

最佳答案

简介

阅读您的评论后,我发现对 SolrJ 的内容有些困惑。和 Spring Data for Apache Solr .

SolrJ 是 Solr 客户端(我个人也会添加标准和官方客户端)。

SolrJ is an API that makes it easy for Java applications to talk to Solr. SolrJ hides a lot of the details of connecting to Solr and allows your application to interact with Solr with simple high-level methods.

Spring Data for Apache Solr 是更大的框架 Spring Data 的一部分,提供从 Spring 应用程序配置和访问 Apache Solr Search Server(为了在内部与 Solr 对话,它使用 SolrJ)。

到目前为止,Solr Spring Data ver. 1.2.0.RELEASE 依赖于 SolrJ 4.7.2,它可能与 Solr 6.2.0 不兼容(如果您使用的是 SolrCloud,请确定)。

鉴于此适当的介绍,我建议将 Solr 实例和 SolrJ 客户端保持在同一版本(或至少在同一主要版本)。

因此,对于 Solr v.6.2.1,您应该使用 SolrJ 6.2.1,但不幸的是,最新的 Solr Spring Data 版本是 2.1.3.RELEASE(其内部依赖于 SolrJ 5.5.0)。

<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>

您的问题

关于您没有收到您正在寻找的字段列表这一事实,只是 Solr Data 不支持字段列表的占位符。

与其苦苦挣扎于 Solr Spring Data,我建议扩展您的 Repository 类,创建一个新的 RepositoryImpl,在其中使用简单而普通的 SolrJ 客户端添加自定义搜索。

@Component
public class ProductsRepositoryImpl implements ProductsRepositoryCustom {

@Autowired
private SolrServer solrServer;

public ProductsRepositoryImpl() {};

public ProductsRepositoryImpl(SolrServer solrServer) {
this.solrServer = solrServer;
}

public List<Map<String, Object>> findFields(String id, List<String> fields)
{
SolrQuery solrQuery = new SolrQuery("id:" + id);
solrQuery.setFields(fields.toArray(new String[0]));
try {
QueryResponse response = this.solrServer.query(solrQuery);
return response.getResults()
.stream()
.map(d ->
{
return d.entrySet()
.stream()
.filter(e -> fields.contains(e.getKey()))
.collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));
}).collect(Collectors.toList());
} catch (SolrServerException e) {
// TODO Auto-generated catch block
}
return Collections.emptyList();
}

}

关于java - Spring solr动态字段注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44250653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com