简体   繁体   中英

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

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

}

Yes you can do this using SimpleQuery Object and Criteria respectively.

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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