我试图做这样的查询,从文档中检索一个特定的字段,我在执行查询时没有出现运行时错误,但我没有从查询中检索到的3个字段,只是日期和起源,但没有变量,应该返回所有变量的变量为null。
如何选择只想在查询中检索的字段?

目前我的查询看起来像这样:

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

最佳答案

简介

阅读您的评论后,我发现SolrJ
Spring Data for Apache Solr

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

SolrJ是一种API,可让Java应用程序轻松与之通信
Solr。 SolrJ隐藏了许多连接到Solr和
允许您的应用程序通过简单的高级与Solr进行交互
方法。

用于Apache Solr的Spring Data是更大的框架Spring Data的一部分,并提供配置和从Spring应用程序访问Apache Solr Search Server的功能(内部使用SolrJ与Solr进行通信)。

到目前为止,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();
  }

}

10-05 19:13