-
-
Save foo4u/02d8f7ea14271fb1a5c1 to your computer and use it in GitHub Desktop.
Advanced queries with Spring Data Solr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.fugu.cms.search.service; | |
import io.fugu.cms.search.domain.SearchDocument; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.Pageable; | |
/** | |
* Provides a content search service. | |
* | |
* @author Scott Rossillo | |
* | |
*/ | |
public interface SearchService { | |
/** | |
* Returns a page of search documents matching the given terms. | |
*/ | |
public Page<SearchDocument> search(String terms, Pageable page); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.fugu.cms.search.service; | |
import io.fugu.cms.search.domain.SearchDocument; | |
import org.apache.solr.client.solrj.SolrQuery; | |
import org.apache.solr.client.solrj.SolrServer; | |
import org.apache.solr.client.solrj.SolrServerException; | |
import org.apache.solr.client.solrj.response.QueryResponse; | |
import org.apache.solr.common.params.DisMaxParams; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.Pageable; | |
import org.springframework.data.solr.core.SolrCallback; | |
import org.springframework.data.solr.core.SolrTemplate; | |
import org.springframework.data.solr.core.query.result.SolrResultPage; | |
import org.springframework.util.Assert; | |
import java.io.IOException; | |
import java.util.List; | |
public class SolrExtendedDisMaxSearchService implements SearchService { | |
private final SolrTemplate solrTemplate; | |
public SolrExtendedDisMaxSearchService(final SolrTemplate solrTemplate) { | |
this.solrTemplate = solrTemplate; | |
} | |
@Override | |
public Page<SearchDocument> search(final String terms, final Pageable page) { | |
Assert.notNull(terms, "Terms cannot be null!"); | |
final SolrQuery query = new SolrQuery(); | |
// edismax query setup | |
query.set("q", terms); | |
query.set(DisMaxParams.QF, "title^20.0 summary^2.5"); | |
query.set("defType", "edismax"); | |
// paging | |
query.setStart(page.getOffset()); | |
query.setRows(page.getPageSize()); | |
return solrTemplate.execute(new SolrCallback<Page<SearchDocument>>() { | |
@Override | |
public Page<SearchDocument> doInSolr(SolrServer solr) throws SolrServerException, IOException { | |
final QueryResponse resp = solr.query(query); | |
final List<SearchDocument> beans = solrTemplate.convertQueryResponseToBeans(resp, SearchDocument.class); | |
return new SolrResultPage<SearchDocument>(beans, page, resp.getResults().getNumFound()); | |
} | |
}); | |
} | |
} |
Defining request handler's is definitely simpler and more stable in the long run. My point was to illustrate how you can extend Spring Data Solr's basic query support with a more complex callback. Probably worth noting on my original article that server site request handlers are more ideally suited to solving this particular problem.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing. I did this slightly differently by defining those parameter in request handler (Solr-side) instead and doing basic custom query:
and on Solr side: