Create a gist now

Instantly share code, notes, and snippets.

@foo4u /SearchService.java Secret
Last active Aug 29, 2015

What would you like to do?
Advanced queries with Spring Data Solr
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);
}
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());
}
});
}
}

Thanks for sharing. I did this slightly differently by defining those parameter in request handler (Solr-side) instead and doing basic custom query:

@Query(requestHandler = "/lookup", value = "?0")

and on Solr side:

<requestHandler name="/lookup" class="solr.SearchHandler" >
  <lst name="defaults">
    <str name="defType">edismax</str>
    <str name="qf">packageName^16 className^8 methodName^4 fieldName^2 type</str>
    <str name="tie">0.1</str>
    <str name="mm">100%</str>
  </lst>
</requestHandler>
Owner

foo4u commented Feb 17, 2015

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