简体   繁体   English

Spring solr 动态字段注解

[英]Spring solr dynamic fields anotation

I'm trying to do a query like that retrieves one specific field from the document, i don't a get runtime error when executing the query but i don't get the 3 fields i'm supposed to retrieve from the query, just date and origin, but no the variable, the one that is supposed to return all of them are nulls.我正在尝试执行类似从文档中检索一个特定字段的查询,在执行查询时我没有得到运行时错误,但我没有得到我应该从查询中检索的 3 个字段,只是日期和原点,但没有变量,应该返回所有这些的变量是空值。 How i can select the fields i only want to retrieve in a query?如何选择我只想在查询中检索的字段?

currently my query looks like this:目前我的查询是这样的:

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

Introduction简介

Reading your comments I see there is a bit of confusion about what's SolrJ and Spring Data for Apache Solr .阅读您的评论,我发现对于什么是SolrJSpring Data for Apache Solr存在一些混淆。

SolrJ is the Solr client (personally I would also add the standard and official client). SolrJ 是 Solr 客户端(我个人也会添加标准和官方客户端)。

SolrJ is an API that makes it easy for Java applications to talk to Solr. SolrJ 是一种 API,它使 Java 应用程序可以轻松地与 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. SolrJ 隐藏了许多连接到 Solr 的细节,并允许您的应用程序使用简单的高级方法与 Solr 交互。

Spring Data for Apache Solr is part of the larger framework Spring Data and provides configuration and access to Apache Solr Search Server from Spring applications ( to talk with Solr internally it uses SolrJ). Spring Data for Apache Solr 是更大框架 Spring Data 的一部分,并提供从 Spring 应用程序对 Apache Solr Search Server 的配置和访问(为了与 Solr 在内部对话,它使用 SolrJ)。

So far, Solr Spring Data ver.到目前为止,Solr Spring Data ver. 1.2.0.RELEASE depends on SolrJ 4.7.2 which could be incompatibile with Solr 6.2.0 (for sure if you're using SolrCloud). 1.2.0.RELEASE 依赖于 SolrJ 4.7.2,它可能与 Solr 6.2.0 不兼容(如果你使用的是 SolrCloud,当然可以)。

Given this appropriate introduction I would suggest to keep Solr instance and SolrJ client on the same version (or at least on the same major version).鉴于此适当的介绍,我建议将 Solr 实例和 SolrJ 客户端保持在同一版本(或至少在同一主要版本上)。

So for Solr v.6.2.1 you should use SolrJ 6.2.1, but unfortunately latest Solr Spring Data version is 2.1.3.RELEASE (which internally depends on SolrJ 5.5.0).因此,对于 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>

Your question你的问题

Regarding the fact that you don't receive the list of fields you're looking for, simply Solr Data does not support placeholders for the field list.关于您没有收到您要查找的字段列表这一事实,只是 Solr Data 不支持字段列表的占位符。

Instead of struggling with Solr Spring Data, I would suggest to extend your Repository class creating a new RepositoryImpl where add a custom search using the simple and plain SolrJ client.与其在 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();
  }

}

Yes you can do this using SimpleQuery Object and Criteria respectively.是的,您可以分别使用 SimpleQuery Object 和 Criteria 来做到这一点。

Create a Custom implementation class for the method you want to query from the repository interface and do as follows .为要从存储库接口查询的方法创建一个自定义实现类,然后执行以下操作。

https://www.petrikainulainen.net/programming/solr/spring-data-solr-tutorial-dynamic-queries/ 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM