Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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);
like image 357
Progs Avatar asked Mar 08 '23 23:03

Progs


1 Answers

Introduction

Reading your comments I see there is a bit of confusion about what's SolrJ and Spring Data for Apache Solr.

SolrJ is the Solr client (personally I would also add the standard and official client).

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 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).

So far, 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).

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).

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).

<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.

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.

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

}
like image 126
freedev Avatar answered Mar 19 '23 22:03

freedev