有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring solr动态字段解析

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

当前我的查询如下所示:

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

共 (2) 个答案

  1. # 1 楼答案

    是的,您可以分别使用SimpleQuery对象和条件来执行此操作

    为要从存储库接口查询的方法创建自定义实现类,并执行以下操作

    https://www.petrikainulainen.net/programming/solr/spring-data-solr-tutorial-dynamic-queries/

      @Resource private SolrTemplate solrTemplate; // Create a SolrTemplate Bean
    
    
    String QueryString = "*:*"; // all search query
    String[] fields = {"Flight", "Hotel", "Car", "Bike"};
    SimpleQuery query = new SimpleQuery(QueryString , new SolrPageRequest(0,10));
    query.addProjectionOnFields(fields);
    FilterQuery fq = new SimpleFilterQuery(new Criteria("Car").is(“Ford”)); // any Filter or criteria to build
    query.addFilterQuery(fq);
    Page<SolrDocumentPojo> resultPage= solrTemplate.query("CoreName ",queryString,Model.class);
    
  2. # 2 楼答案

    简介

    读了你的评论,我发现关于SolrJSpring 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.

    ApacheSolr的Spring数据是更大框架Spring数据的一部分,它提供了从Spring应用程序对ApacheSolr搜索服务器的配置和访问(为了在内部与Solr对话,它使用SolrJ)

    到目前为止,Solr Spring数据已经发布。1.2.0.该版本依赖于SolrJ 4.7.2,它可能与Solr 6.2.0不兼容(如果您使用的是SolrCloud,这是肯定的)

    鉴于这一适当的介绍,我建议将Solr instance和SolrJ client保持在同一个版本上(或者至少保持在同一个主版本上)

    所以对于Solr v.6.2.1,您应该使用SolrJ 6.2.1,但不幸的是,最新的Solr Spring数据版本是2.1.3。发行版(内部取决于SolrJ 5.5.0)

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

    你的问题

    考虑到您没有收到正在查找的字段列表,simply Solr Data不支持字段列表的占位符

    我建议您扩展存储库类,创建一个新的RepositoryImpl,使用简单明了的SolrJ客户端添加自定义搜索,而不是使用Solr Spring数据

    @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();
      }
    
    }