List of usage examples for org.apache.solr.client.solrj SolrQuery setRows
public SolrQuery setRows(Integer rows)
From source file:SolrUpdate.java
License:Apache License
public void getAllSolrRecords() { String pmid;/* ww w . j a v a 2s . c om*/ try { CoreAdminRequest adminRequest = new CoreAdminRequest(); adminRequest.setAction(CoreAdminAction.RELOAD); SolrServer solr = new HttpSolrServer("http://localhost:8983/solr"); String query; query = "pmid:*"; SolrQuery theq = new SolrQuery(); theq.setQuery(query); theq.setStart(0); theq.setRows(10000); QueryResponse response = new QueryResponse(); response = solr.query(theq); SolrDocumentList list = response.getResults(); int docnum = 1; for (SolrDocument doc : list) { Publication currlist = new Publication(); List<String> fullnames = new ArrayList<String>(); String currepubsum1 = "", currepubsum2 = ""; if (doc.getFieldValue("abstract") != null) { currlist.setAbstract(doc.getFieldValue("abstract").toString()); } if (doc.getFieldValue("ptitle") != null) { currlist.setTitle(doc.getFieldValue("ptitle").toString()); } if (doc.getFieldValue("author_fullname_list") != null) { currlist.setFirst5authors(doc.getFieldValue("author_fullname_list").toString()); } if (doc.getFieldValue("pmid") != null) { currlist.setPmid(Integer.valueOf(doc.getFieldValue("pmid").toString())); } if (doc.getFieldValue("completion") != null) { currlist.setCompletion(Boolean.valueOf(doc.getFieldValue("completion").toString())); } if (doc.getFieldValue("lruid") != null) { currlist.setLruid(doc.getFieldValue("lruid").toString()); } if (doc.getFieldValue("draftpoint") != null) { currlist.setDraftpoint(Integer.valueOf(doc.getFieldValue("draftpoint").toString())); } if (doc.getFieldValue("journalname") != null) { currlist.setJournalname(doc.getFieldValue("journalname").toString()); } if (doc.getFieldValue("journalyear") != null) { currlist.setJournalyear(doc.getFieldValue("journalyear").toString()); } if (doc.getFieldValue("journalday") != null) { currlist.setJournalday(doc.getFieldValue("journalday").toString()); } if (doc.getFieldValue("journalmonth") != null) { currlist.setJournalmonth(doc.getFieldValue("journalmonth").toString()); } if (doc.getFieldValue("journalpage") != null) { currlist.setJournalstartpg(doc.getFieldValue("journalpage").toString()); } if (doc.getFieldValue("journalissue") != null) { currlist.setJournalissue(doc.getFieldValue("journalissue").toString()); } if (doc.getFieldValue("journalvolume") != null) { currlist.setJournalvolume(doc.getFieldValue("journalvolume").toString()); } if (doc.getFieldValue("publicationdate_year") != null) { currlist.setYear(doc.getFieldValue("publicationdate_year").toString()); } if (doc.getFieldValue("doi") != null) { currlist.setDoi(doc.getFieldValue("doi").toString()); } if (doc.getFieldValues("pfileinfo") != null) { Collection<Object> currcoll = doc.getFieldValues("pfileinfo"); for (Object currobj : currcoll) { currlist.getFilesanddata().add(currobj.toString()); } } if (doc.getFieldValue("author_firstname") != null) { currlist.setFauthors((List<String>) doc.getFieldValue("author_firstname")); } if (doc.getFieldValue("author_lastname") != null) { currlist.setLauthors((List<String>) doc.getFieldValue("author_lastname")); } if (doc.getFieldValue("epubmonth") != null) { currlist.setEpubmonth(doc.getFieldValue("epubmonth").toString()); } if (doc.getFieldValue("epubyear") != null) { currlist.setEpubyear(doc.getFieldValue("epubyear").toString()); } if (doc.getFieldValue("epubday") != null) { currlist.setEpubday(doc.getFieldValue("epubday").toString()); } int counter = 0; for (String currstring : currlist.getFauthors()) { currstring += " " + currlist.getLauthors().get(counter); fullnames.add(currstring); counter++; } currlist.setFullnames(fullnames); if (currlist.getJournalvolume().length() > 0) { currepubsum2 += currlist.getJournalvolume(); } if (currlist.getJournalissue().length() > 0) { currepubsum2 += "(" + currlist.getJournalissue() + ")" + ":"; } if (currlist.getJournalstartpg().length() > 0) { currepubsum2 += currlist.getJournalstartpg() + "."; } if (currlist.getEpubday().length() < 1 && currlist.getEpubmonth().length() < 1 && currlist.getEpubyear().length() < 1) { currepubsum1 = "[Epub ahead of print]"; } else if (currlist.getEpubyear().length() > 0) { currepubsum1 = "Epub " + currlist.getEpubyear() + " " + currlist.getEpubmonth() + " " + currlist.getEpubday(); } else { currepubsum1 = ""; } currlist.setEpubsum(currepubsum1); currlist.setEpubsum2(currepubsum2); currlist.setIndex(docnum); if (currlist.getCompletion() == false) { currlist.setComp("Hidden"); } else { currlist.setComp("Visible"); } solrrecords.add(currlist); docnum++; } } catch (Exception ex) { System.out.println(ex); } System.out.println("There are a total of this many records gathered: " + solrrecords.size()); }
From source file:at.kc.tugraz.ss.service.solr.impl.fct.SSSolrFct.java
License:Apache License
public static SolrDocumentList query(final SolrClient solrQuerier, final SSSolrQueryPars queryPars) throws Exception { final SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(queryPars.query); solrQuery.setRows(queryPars.numRows); return solrQuerier.query(solrQuery).getResults(); }
From source file:at.newmedialab.lmf.search.services.indexing.SolrCoreRuntime.java
License:Apache License
/** * Ask the server to retrieve all documents that depend on the resource passed as argument; this * query is//from w ww . j a v a 2 s. c o m * carried out by querying the dependencies field of a document. * * @param resource * @return */ public Collection<URI> listDependent(ValueFactory valueFactory, URI resource) { SolrQuery query = new SolrQuery(); query.setQuery("lmf.dependencies:\"" + resource.stringValue() + "\""); query.setFields("lmf.uri"); query.setRows(Integer.MAX_VALUE); try { SolrDocumentList docs = server.query(query).getResults(); Set<URI> result = new HashSet<URI>(); for (SolrDocument doc : docs) { result.add(valueFactory.createURI((String) doc.getFirstValue("lmf.uri"))); } return result; } catch (SolrServerException e) { return Collections.emptyList(); } }
From source file:at.pagu.soldockr.core.QueryParser.java
License:Apache License
private void appendPagination(SolrQuery query, Pageable pageable) { if (pageable == null) { return;/*from w ww . ja v a 2 s .com*/ } query.setStart(pageable.getOffset()); query.setRows(pageable.getPageSize()); }
From source file:at.pagu.soldockr.core.SolrTemplate.java
License:Apache License
@Override public long executeCount(final SolDockRQuery query) { Assert.notNull(query, "Query must not be 'null'."); return execute(new SolrCallback<Long>() { @Override/* ww w . j a v a2 s . c o m*/ public Long doInSolr(SolrServer solrServer) throws SolrServerException, IOException { SolrQuery solrQuery = queryParser.constructSolrQuery(query); solrQuery.setStart(0); solrQuery.setRows(0); return solrServer.query(solrQuery).getResults().getNumFound(); } }); }
From source file:at.tugraz.sss.servs.db.impl.SSDBNoSQLSolrImpl.java
License:Apache License
@Override public List<String> search(final SSDBNoSQLSearchPar par) throws SSErr { try {/* w w w .ja v a2s.c om*/ final List<String> searchResults = new ArrayList<>(); final SolrQuery query = new SolrQuery(); final String globalSearchOp = par.globalSearchOp.toUpperCase(); final String localSearchOp = par.localSearchOp.toUpperCase(); String queryString = new String(); if (par.wheres.isEmpty()) { throw new Exception("no search fields given"); } for (Map.Entry<SSSolrSearchFieldE, List<SSSolrKeywordLabel>> where : par.wheres.entrySet()) { if (where.getValue().isEmpty()) { throw new Exception("values for search field empty"); } queryString += where.getKey().val + SSStrU.colon + "("; for (SSSolrKeywordLabel value : where.getValue()) { if (par.useFuzzySearch && !SSStrU.contains(value, SSStrU.blank)) { queryString += value + SSStrU.tilde + SSStrU.blank + localSearchOp + SSStrU.blank; } else { queryString += SSStrU.doubleQuote + value + SSStrU.doubleQuote + SSStrU.blank + localSearchOp + SSStrU.blank; } } queryString = SSStrU.removeTrailingString(queryString, SSStrU.blank + localSearchOp + SSStrU.blank) + ")" + SSStrU.blank + globalSearchOp + SSStrU.blank; } queryString = SSStrU.removeTrailingString(queryString, SSStrU.blank + globalSearchOp + SSStrU.blank); query.setQuery(queryString); query.setRows(par.maxResults); for (SSSolrSearchResult result : SSSolrSearchResult.get(solrServer.query(query).getResults())) { searchResults.add(result.id); } return searchResults; } catch (Exception error) { SSServErrReg.regErrThrow(error); return null; } }
From source file:au.org.ala.biocache.dao.SearchDAOImpl.java
License:Open Source License
/** * Writes the values for the first supplied facet to output stream * * @param includeCount true when the count should be included in the download * @param lookupName true when a name lsid should be looked up in the bie *//*from w w w . j a v a 2 s .c o m*/ public void writeFacetToStream(SpatialSearchRequestParams searchParams, boolean includeCount, boolean lookupName, boolean includeSynonyms, boolean includeLists, OutputStream out, DownloadDetailsDTO dd) throws Exception { //set to unlimited facets searchParams.setFlimit(-1); queryFormatUtils.formatSearchQuery(searchParams); String queryString = searchParams.getFormattedQuery(); SolrQuery solrQuery = initSolrQuery(searchParams, false, null); solrQuery.setQuery(queryString); //don't want any results returned solrQuery.setRows(0); searchParams.setPageSize(0); solrQuery.setFacetLimit(FACET_PAGE_SIZE); int offset = 0; boolean shouldLookup = lookupName && (searchParams.getFacets()[0].contains("_guid") || searchParams.getFacets()[0].contains("_lsid")); if (dd != null) { dd.resetCounts(); } QueryResponse qr = runSolrQuery(solrQuery, searchParams); if (logger.isDebugEnabled()) { logger.debug("Retrieved facet results from server..."); } if (qr.getResults().getNumFound() > 0) { FacetField ff = qr.getFacetField(searchParams.getFacets()[0]); //write the header line if (ff != null) { String[] header = new String[] { ff.getName() }; // out.write(ff.getName().getBytes()); if (shouldLookup) { header = speciesLookupService.getHeaderDetails(ff.getName(), includeCount, includeSynonyms); } else if (includeCount) { //out.write(",Count".getBytes()); header = (String[]) ArrayUtils.add(header, "count"); } if (includeLists) { header = (String[]) ArrayUtils.addAll(header, listsService.getTypes().toArray(new String[] {})); } CSVRecordWriter writer = new CSVRecordWriter(new CloseShieldOutputStream(out), header); try { boolean addedNullFacet = false; //out.write("\n".getBytes()); //PAGE through the facets until we reach the end. //do not continue when null facet is already added and the next facet is only null while (ff.getValueCount() > 1 || !addedNullFacet || (ff.getValueCount() == 1 && ff.getValues().get(0).getName() != null)) { //process the "species_guid_ facet by looking up the list of guids if (shouldLookup) { List<String> guids = new ArrayList<String>(); List<Long> counts = new ArrayList<Long>(); List<String[]> speciesLists = new ArrayList<String[]>(); if (logger.isDebugEnabled()) { logger.debug("Downloading " + ff.getValueCount() + " species guids"); } for (FacetField.Count value : ff.getValues()) { //only add null facet once if (value.getName() == null) addedNullFacet = true; if (value.getCount() == 0 || (value.getName() == null && addedNullFacet)) continue; guids.add(value.getName()); if (includeCount) { counts.add(value.getCount()); } //Only want to send a sub set of the list so that the URI is not too long for BIE if (guids.size() == 30) { //now get the list of species from the web service TODO may need to move this code //handle null values being returned from the service... writeTaxonDetailsToStream(guids, counts, includeCount, includeSynonyms, includeLists, writer); guids.clear(); counts.clear(); } } //now write any guids that remain at the end of the looping writeTaxonDetailsToStream(guids, counts, includeCount, includeSynonyms, includeLists, writer); } else { //default processing of facets for (FacetField.Count value : ff.getValues()) { //only add null facet once if (value.getName() == null) addedNullFacet = true; if (value.getCount() == 0 || (value.getName() == null && addedNullFacet)) continue; String name = value.getName() != null ? value.getName() : ""; String[] row = includeCount ? new String[] { name, Long.toString(value.getCount()) } : new String[] { name }; writer.write(row); } } offset += FACET_PAGE_SIZE; if (dd != null) { dd.updateCounts(FACET_PAGE_SIZE); } //get the next values solrQuery.remove("facet.offset"); solrQuery.add("facet.offset", Integer.toString(offset)); qr = runSolrQuery(solrQuery, searchParams); ff = qr.getFacetField(searchParams.getFacets()[0]); } } finally { writer.finalise(); } } } }
From source file:au.org.ala.biocache.dao.SearchDAOImpl.java
License:Open Source License
/** * Writes all the distinct latitude and longitude in the index to the supplied * output stream.//w w w . j a v a 2s . co m * * @param out * @throws Exception */ public void writeCoordinatesToStream(SearchRequestParams searchParams, OutputStream out) throws Exception { //generate the query to obtain the lat,long as a facet SearchRequestParams srp = new SearchRequestParams(); SearchUtils.setDefaultParams(srp); srp.setFacets(searchParams.getFacets()); SolrQuery solrQuery = initSolrQuery(srp, false, null); //We want all the facets so we can dump all the coordinates solrQuery.setFacetLimit(-1); solrQuery.setFacetSort("count"); solrQuery.setRows(0); solrQuery.setQuery(searchParams.getQ()); QueryResponse qr = runSolrQuery(solrQuery, srp); if (qr.getResults().size() > 0) { FacetField ff = qr.getFacetField(searchParams.getFacets()[0]); if (ff != null && ff.getValueCount() > 0) { out.write("latitude,longitude\n".getBytes()); //write the facets to file for (FacetField.Count value : ff.getValues()) { //String[] slatlon = value.getName().split(","); if (value.getName() != null && value.getCount() > 0) { out.write(value.getName().getBytes()); out.write("\n".getBytes()); } } } } }
From source file:au.org.ala.biocache.dao.SearchDAOImpl.java
License:Open Source License
/** * Downloads the records for the supplied query. Used to break up the download into components * 1) 1 call for each data resource that has a download limit (supply the data resource uid as the argument dataResource) * 2) 1 call for the remaining records/*from w w w . j ava 2 s.c o m*/ * * @param downloadParams * @param downloadLimit * @param uidStats * @param fields * @param qaFields * @param resultsCount * @param dataResource The dataResource being download. This should be null if multiple data resource are being downloaded. * @return * @throws Exception */ private int downloadRecords(DownloadRequestParams downloadParams, RecordWriterError writer, Map<String, Integer> downloadLimit, ConcurrentMap<String, AtomicInteger> uidStats, String[] fields, String[] qaFields, int resultsCount, String dataResource, boolean includeSensitive, DownloadDetailsDTO dd, boolean limit, String[] analysisLayers) throws Exception { if (logger.isInfoEnabled()) { logger.info("download query: " + downloadParams.getQ()); } SolrQuery solrQuery = initSolrQuery(downloadParams, false, null); solrQuery.setRows(limit ? MAX_DOWNLOAD_SIZE : -1); queryFormatUtils.formatSearchQuery(downloadParams); solrQuery.setQuery(downloadParams.getFormattedQuery()); //Only the fields specified below will be included in the results from the SOLR Query solrQuery.setFields("row_key", "institution_uid", "collection_uid", "data_resource_uid", "data_provider_uid"); if (dd != null) { dd.resetCounts(); } //get coordinates for analysis layer intersection if (analysisLayers.length > 0) { if (!includeSensitive && dd.getSensitiveFq() != null) { for (String s : sensitiveSOLRHdr) solrQuery.addField(s); } else { for (String s : notSensitiveSOLRHdr) solrQuery.addField(s); } } int pageSize = downloadBatchSize; StringBuilder sb = new StringBuilder(downloadParams.getFields()); if (downloadParams.getExtra().length() > 0) { sb.append(",").append(downloadParams.getExtra()); } List<SolrQuery> queries = new ArrayList<SolrQuery>(); queries.add(solrQuery); //split into sensitive and non-sensitive queries when // - not including all sensitive values // - there is a sensitive fq List<SolrQuery> sensitiveQ = new ArrayList<SolrQuery>(); if (!includeSensitive && dd.getSensitiveFq() != null) { sensitiveQ = splitQueries(queries, dd.getSensitiveFq(), null, null); } final String[] sensitiveFields; final String[] notSensitiveFields; if (!includeSensitive && dd.getSensitiveFq() != null) { //lookup for fields from sensitive queries sensitiveFields = org.apache.commons.lang3.ArrayUtils.addAll(fields, sensitiveCassandraHdr); //use general fields when sensitive data is not permitted notSensitiveFields = org.apache.commons.lang3.ArrayUtils.addAll(fields, notSensitiveCassandraHdr); } else { sensitiveFields = new String[0]; notSensitiveFields = fields; } for (SolrQuery q : queries) { int startIndex = 0; String[] fq = downloadParams.getFormattedFq(); if (q.getFilterQueries() != null && q.getFilterQueries().length > 0) { if (fq == null) { fq = new String[0]; } fq = org.apache.commons.lang3.ArrayUtils.addAll(fq, q.getFilterQueries()); } QueryResponse qr = runSolrQuery(q, fq, pageSize, startIndex, "_docid_", "asc"); List<String> uuids = new ArrayList<String>(); List<String[]> intersectionAll = intersectResults(dd.getRequestParams().getLayersServiceUrl(), analysisLayers, qr.getResults()); while (qr.getResults().size() > 0 && (!limit || resultsCount < MAX_DOWNLOAD_SIZE) && shouldDownload(dataResource, downloadLimit, false)) { if (logger.isDebugEnabled()) { logger.debug("Start index: " + startIndex); } Map<String, String[]> dataToInsert = new HashMap<String, String[]>(); //cycle through the results adding them to the list that will be sent to cassandra int row = 0; for (SolrDocument sd : qr.getResults()) { if (sd.getFieldValue("data_resource_uid") != null) { String druid = sd.getFieldValue("data_resource_uid").toString(); if (shouldDownload(druid, downloadLimit, true) && (!limit || resultsCount < MAX_DOWNLOAD_SIZE)) { resultsCount++; uuids.add(sd.getFieldValue("row_key").toString()); //include analysis layer intersections if (intersectionAll.size() > row + 1) dataToInsert.put(sd.getFieldValue("row_key").toString(), (String[]) ArrayUtils.subarray(intersectionAll.get(row + 1), 2, intersectionAll.get(row + 1).length)); //increment the counters.... incrementCount(uidStats, sd.getFieldValue("institution_uid")); incrementCount(uidStats, sd.getFieldValue("collection_uid")); incrementCount(uidStats, sd.getFieldValue("data_provider_uid")); incrementCount(uidStats, druid); } } row++; } String[] newMiscFields; if (sensitiveQ.contains(q)) { newMiscFields = au.org.ala.biocache.Store.writeToWriter(writer, uuids.toArray(new String[] {}), sensitiveFields, qaFields, true, (dd.getRequestParams() != null ? dd.getRequestParams().getIncludeMisc() : false), dd.getMiscFields(), dataToInsert); } else { newMiscFields = au.org.ala.biocache.Store.writeToWriter(writer, uuids.toArray(new String[] {}), notSensitiveFields, qaFields, includeSensitive, (dd.getRequestParams() != null ? dd.getRequestParams().getIncludeMisc() : false), dd.getMiscFields(), dataToInsert); } //test for errors if (writer.hasError()) { throw RecordWriterException.newRecordWriterException(dd, downloadParams, false, writer); } dd.setMiscFields(newMiscFields); startIndex += pageSize; uuids.clear(); dd.updateCounts(qr.getResults().size()); if (!limit || resultsCount < MAX_DOWNLOAD_SIZE) { //we have already set the Filter query the first time the query was constructed rerun with he same params but different startIndex qr = runSolrQuery(q, null, pageSize, startIndex, "_docid_", "asc"); } } } return resultsCount; }
From source file:au.org.ala.biocache.dao.SearchDAOImpl.java
License:Open Source License
private List<OccurrencePoint> getPoints(SpatialSearchRequestParams searchParams, PointType pointType, int max) throws Exception { List<OccurrencePoint> points = new ArrayList<OccurrencePoint>(); // new OccurrencePoint(PointType.POINT); queryFormatUtils.formatSearchQuery(searchParams); if (logger.isInfoEnabled()) { logger.info("search query: " + searchParams.getFormattedQuery()); }/* w w w .j a v a2 s . co m*/ SolrQuery solrQuery = new SolrQuery(); solrQuery.setQueryType("standard"); solrQuery.setQuery(searchParams.getFormattedQuery()); solrQuery.setRows(0); solrQuery.setFacet(true); solrQuery.addFacetField(pointType.getLabel()); solrQuery.setFacetMinCount(1); solrQuery.setFacetLimit(max); // unlimited = -1 QueryResponse qr = runSolrQuery(solrQuery, searchParams.getFormattedFq(), 1, 0, "score", "asc"); List<FacetField> facets = qr.getFacetFields(); if (facets != null) { for (FacetField facet : facets) { List<FacetField.Count> facetEntries = facet.getValues(); if (facet.getName().contains(pointType.getLabel()) && (facetEntries != null) && (facetEntries.size() > 0)) { for (FacetField.Count fcount : facetEntries) { if (StringUtils.isNotEmpty(fcount.getName()) && fcount.getCount() > 0) { OccurrencePoint point = new OccurrencePoint(pointType); point.setCount(fcount.getCount()); String[] pointsDelimited = StringUtils.split(fcount.getName(), ','); List<Float> coords = new ArrayList<Float>(); for (String coord : pointsDelimited) { try { Float decimalCoord = Float.parseFloat(coord); coords.add(decimalCoord); } catch (NumberFormatException numberFormatException) { logger.warn("Error parsing Float for Lat/Long: " + numberFormatException.getMessage(), numberFormatException); } } if (!coords.isEmpty()) { Collections.reverse(coords); // must be long, lat order point.setCoordinates(coords); points.add(point); } } } } } } return points; }