Solr Facet Search spring-data-solr
- by sv1
I am new to Solr and we are using Spring-data for Solr
I have a question may be its too simple but I am unable to comprehend.
Basically I need to search on any field but I have a "zip" field as one of the facet fields.
I have a Solr repository . (Am not sure if the annotations on the Repository are correct.)
public interface MyRepository extends SolrCrudRepository<MyPOJO, String> {
@Query(value = "*:*")
@Facet(fields={"zip"})
public FacetPage<MyPOJO> findByQueryandAnno(String searchTerm,Pageable page);
}
In my Service class I am trying to call this methods by Injecting MyRepository like below
public class MyService {
@Inject
MyRepository solrRepo;
@Inject
SolrTemplate solrTemplate;
public FacetPage<MyPOJO> tryFacets(String searchString){
//Here is where I am struggling
SimpleFacetQuery query =
new SimpleQuery(new SimpleStringCriteria(searchString));
query.setFacetOptions(new FacetOptions("zip"));
//Not sure how to get the Pageable object.
//But the repository doesnt accept without it.
return solrTemplate.queryForPage(query,{Pageable Instance to be passed here})
}
From my jUnit I am loading context files needed for Solr and Spring
//In jUnit the test method looks like this
service.tryFacets("some value");
and it fails with - method not found in the service class at the call to the repository method.
*********EDIT****************
As per ChristophStrobl's advice created a copyfield called multivaluedCopyField and the searchString argument works good. But the facet still isnt working...Now my code looks like this. I get the MyPOJO object as response but the facetcount and the faceted values are missing.
public interface MyRepository extends SolrCrudRepository<MyPOJO, String> {
@Query(value = "*:*")
@Facet(fields={"zip"})
public FacetPage<MyPOJO> findByQueryandAnno(String searchTerm,Pageable page);
}
My service class looks like
public class MyService {
@Inject
MyRepository solrRepo;
public FacetPage<MyPOJO> tryFacets(String searchString){
//Here is where I am struggling
SimpleFacetQuery query =
new SimpleQuery(new SimpleStringCriteria(searchString)).setPageRequest new
PageRequest(0,5));
query.setFacetOptions(new FacetOptions("zip"));
FacetPage<MyPOJO> facetedPOJO=
solrRepo.findByQueryandAnno(searchString,query.getPageRequest());
return facetedPOJO;
}
My jUnit method call is like
service.tryFacets("some value");