NIHVIVO-2437 Generate /individuallist page results in SolrIndividualListController
This commit is contained in:
parent
fabf46d1e7
commit
1176568eb5
3 changed files with 148 additions and 170 deletions
|
@ -17,16 +17,12 @@ import org.apache.commons.logging.Log;
|
|||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.index.CorruptIndexException;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.PrefixQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.ScoreDoc;
|
||||
import org.apache.lucene.search.Sort;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.solr.client.solrj.SolrQuery;
|
||||
import org.apache.solr.client.solrj.SolrServer;
|
||||
import org.apache.solr.client.solrj.response.QueryResponse;
|
||||
import org.apache.solr.common.SolrDocument;
|
||||
import org.apache.solr.common.SolrDocumentList;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
|
@ -36,8 +32,8 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.Exc
|
|||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
|
||||
import edu.cornell.mannlib.vitro.webapp.search.lucene.Entity2LuceneDoc;
|
||||
import edu.cornell.mannlib.vitro.webapp.search.lucene.LuceneIndexFactory;
|
||||
import edu.cornell.mannlib.vitro.webapp.search.lucene.Entity2LuceneDoc.VitroLuceneTermNames;
|
||||
import edu.cornell.mannlib.vitro.webapp.search.solr.SolrSetup;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.ListedIndividualTemplateModel;
|
||||
import freemarker.ext.beans.BeansWrapper;
|
||||
import freemarker.template.TemplateModel;
|
||||
|
@ -176,53 +172,42 @@ public class SolrIndividualListController extends FreemarkerHttpServlet {
|
|||
* into a DAO or similar object.
|
||||
*/
|
||||
public static Map<String,Object> getResultsForVClass(String vclassURI, int page, String alpha, IndividualDao indDao, ServletContext context)
|
||||
throws CorruptIndexException, IOException, ServletException{
|
||||
throws CorruptIndexException, IOException, ServletException {
|
||||
Map<String,Object> rvMap = new HashMap<String,Object>();
|
||||
|
||||
//make lucene query for this rdf:type
|
||||
Query query = getQuery(vclassURI, alpha);
|
||||
// Make solr query for this rdf:type
|
||||
SolrQuery query = getQuery(vclassURI, alpha, page);
|
||||
SolrServer solr = SolrSetup.getSolrServer(context);
|
||||
QueryResponse response = null;
|
||||
|
||||
//execute lucene query for individuals of the specified type
|
||||
IndexSearcher index = LuceneIndexFactory.getIndexSearcher(context);
|
||||
TopDocs docs = null;
|
||||
try{
|
||||
docs = index.search(query, null,
|
||||
ENTITY_LIST_CONTROLLER_MAX_RESULTS,
|
||||
new Sort(Entity2LuceneDoc.term.NAME_LOWERCASE));
|
||||
}catch(Throwable th){
|
||||
log.error("Could not run search. " + th.getMessage());
|
||||
docs = null;
|
||||
// Execute lucene query for individuals of the specified type
|
||||
try {
|
||||
response = solr.query(query);
|
||||
} catch (Throwable t) {
|
||||
log.error(t, t);
|
||||
}
|
||||
|
||||
if( docs == null )
|
||||
if ( response == null ) {
|
||||
throw new ServletException("Could not run search in IndividualListController");
|
||||
}
|
||||
|
||||
//get list of individuals for the search results
|
||||
int size = docs.totalHits;
|
||||
SolrDocumentList docs = response.getResults();
|
||||
|
||||
if (docs == null) {
|
||||
throw new ServletException("Could not run search in IndividualListController");
|
||||
}
|
||||
|
||||
// get list of individuals for the search results
|
||||
long size = docs.getNumFound();
|
||||
log.debug("Number of search results: " + size);
|
||||
|
||||
// don't get all the results, only get results for the requestedSize
|
||||
List<Individual> individuals = new ArrayList<Individual>(INDIVIDUALS_PER_PAGE);
|
||||
int individualsAdded = 0;
|
||||
int ii = (page-1)*INDIVIDUALS_PER_PAGE;
|
||||
while( individualsAdded < INDIVIDUALS_PER_PAGE && ii < size ){
|
||||
ScoreDoc hit = docs.scoreDocs[ii];
|
||||
if (hit != null) {
|
||||
Document doc = index.doc(hit.doc);
|
||||
if (doc != null) {
|
||||
String uri = doc.getField(Entity2LuceneDoc.term.URI).stringValue();
|
||||
Individual ind = indDao.getIndividualByURI( uri );
|
||||
if( ind != null ){
|
||||
individuals.add( ind );
|
||||
individualsAdded++;
|
||||
List<Individual> individuals = new ArrayList<Individual>((int)size);
|
||||
for (SolrDocument doc : docs) {
|
||||
String uri = doc.get(VitroLuceneTermNames.URI).toString();
|
||||
Individual individual = indDao.getIndividualByURI( uri );
|
||||
if (individual != null) {
|
||||
individuals.add(individual);
|
||||
}
|
||||
} else {
|
||||
log.warn("no document found for lucene doc id " + hit.doc);
|
||||
}
|
||||
} else {
|
||||
log.debug("hit was null");
|
||||
}
|
||||
ii++;
|
||||
}
|
||||
|
||||
rvMap.put("count", size);
|
||||
|
@ -240,41 +225,37 @@ public class SolrIndividualListController extends FreemarkerHttpServlet {
|
|||
|
||||
rvMap.put("totalCount", size);
|
||||
rvMap.put("entities",individuals);
|
||||
if (individuals == null)
|
||||
log.debug("entities list is null for vclass " + vclassURI );
|
||||
|
||||
if (individuals.isEmpty())
|
||||
log.debug("entities list is empty for vclass " + vclassURI );
|
||||
|
||||
return rvMap;
|
||||
}
|
||||
|
||||
private static BooleanQuery getQuery(String vclassUri, String alpha){
|
||||
BooleanQuery query = new BooleanQuery();
|
||||
try{
|
||||
//query term for rdf:type
|
||||
query.add(
|
||||
new TermQuery( new Term(Entity2LuceneDoc.term.RDFTYPE, vclassUri)),
|
||||
BooleanClause.Occur.MUST );
|
||||
private static SolrQuery getQuery(String vclassUri, String alpha, int page){
|
||||
|
||||
//Add alpha filter if it is needed
|
||||
Query alphaQuery = null;
|
||||
if( alpha != null && !"".equals(alpha) && alpha.length() == 1){
|
||||
alphaQuery =
|
||||
new PrefixQuery(new Term(Entity2LuceneDoc.term.NAME_LOWERCASE, alpha.toLowerCase()));
|
||||
query.add(alphaQuery,BooleanClause.Occur.MUST);
|
||||
String queryStr = VitroLuceneTermNames.RDFTYPE + ":\"" + vclassUri + "\"";
|
||||
|
||||
// Add alpha filter if it is needed
|
||||
if ( alpha != null && !"".equals(alpha) && alpha.length() == 1) {
|
||||
queryStr += VitroLuceneTermNames.NAME_LOWERCASE + ":" + alpha.toLowerCase() + "*";
|
||||
}
|
||||
|
||||
SolrQuery query = new SolrQuery(queryStr);
|
||||
|
||||
int start = (page-1)*INDIVIDUALS_PER_PAGE;
|
||||
query.setStart(start)
|
||||
.setRows(INDIVIDUALS_PER_PAGE);
|
||||
|
||||
log.debug("Query: " + query);
|
||||
return query;
|
||||
} catch (Exception ex){
|
||||
log.error(ex,ex);
|
||||
return new BooleanQuery();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<PageRecord> makePagesList( int count, int pageSize, int selectedPage){
|
||||
public static List<PageRecord> makePagesList( long size, int pageSize, int selectedPage ) {
|
||||
|
||||
List<PageRecord> records = new ArrayList<PageRecord>( MAX_PAGES + 1 );
|
||||
int requiredPages = count/pageSize ;
|
||||
int remainder = count % pageSize ;
|
||||
int requiredPages = (int) (size/pageSize) ;
|
||||
int remainder = (int) (size % pageSize) ;
|
||||
if( remainder > 0 )
|
||||
requiredPages++;
|
||||
|
||||
|
@ -295,7 +276,7 @@ public class SolrIndividualListController extends FreemarkerHttpServlet {
|
|||
}else if ( requiredPages > MAX_PAGES && selectedPage > requiredPages - MAX_PAGES ){
|
||||
//the selected page is in the end of the list
|
||||
int startPage = requiredPages - MAX_PAGES;
|
||||
double max = Math.ceil(count/pageSize);
|
||||
double max = Math.ceil(size/pageSize);
|
||||
for(int page = startPage; page <= max; page++ ){
|
||||
records.add( new PageRecord( "page=" + page, Integer.toString(page), Integer.toString(page), selectedPage == page ) );
|
||||
}
|
||||
|
|
|
@ -187,6 +187,7 @@ public class SolrAutocompleteController extends VitroAjaxController {
|
|||
// RY 5/18/2011 For now, just doing untokenized query, due to the interactions of wildcard
|
||||
// query and stemming described below. Need to find a way to do this in Solr.
|
||||
// Should take the same approach if we can figure out how to do a disjunction.
|
||||
// Probably just add an explicit "OR" between the terms.
|
||||
|
||||
// String stemParam = (String) request.getParameter("stem");
|
||||
// boolean stem = "true".equals(stemParam);
|
||||
|
|
|
@ -175,10 +175,6 @@ public class SolrPagedSearchController extends FreemarkerHttpServlet {
|
|||
log.debug("Query text is \""+ qtxt + "\"");
|
||||
|
||||
SolrQuery query = getQuery(qtxt, maxHitCount, vreq);
|
||||
|
||||
// ** For xml requested, add version=2.2 for xml version
|
||||
// is that enough, or do we also have to add wt param?
|
||||
|
||||
SolrServer solr = SolrSetup.getSolrServer(getServletContext());
|
||||
QueryResponse response = null;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue