List of usage examples for org.apache.solr.common SolrDocumentList getNumFound
public long getNumFound()
From source file:datacite.oai.provider.service.MDSSearchServiceSolrImpl.java
License:Open Source License
@Override public Pair<List<DatasetRecordBean>, Integer> getDatasets(Date updateDateFrom, Date updateDateTo, String setspec, int offset, int length) throws ServiceException { SolrQuery query = constructSolrQuery(updateDateFrom, updateDateTo, setspec, offset, length); logger.debug(query);/*from w w w .ja va 2 s .c o m*/ try { QueryResponse response = solrServer.query(query); SolrDocumentList results = response.getResults(); List<DatasetRecordBean> records = convertToRecords(results); int count = (int) results.getNumFound(); return new Pair<List<DatasetRecordBean>, Integer>(records, count); } catch (Exception e) { throw new ServiceException(e); } }
From source file:de.hybris.platform.solrfacetsearch.integration.IndexFullProductTest.java
License:Open Source License
@Test public void testVerifyNumberOfIndexedRecords() throws Exception { final int numberOfProductsToIndex = searchResult.getTotalCount(); LOG.info("Number of products to index = " + numberOfProductsToIndex); final QueryResponse solrResponse = solrServer.query(new SolrQuery("*:*")); final SolrDocumentList solrDocumentList = solrResponse.getResults(); assertNotNull(solrDocumentList);/* w w w .ja v a 2s .co m*/ LOG.info("Number of indexed documents = " + solrDocumentList.getNumFound()); assertEquals(numberOfProductsToIndex, solrDocumentList.getNumFound()); }
From source file:de.hybris.platform.solrfacetsearch.integration.IndexFullProductTest.java
License:Open Source License
private void checkPriceForAllProducts(final String currency) throws SolrServerException { final List<ProductModel> productModels = searchResult.getResult(); final CurrencyModel currencyModel = commonI18NService.getCurrency(currency); commonI18NService.setCurrentCurrency(currencyModel); for (final ProductModel productModel : productModels) { final String solrDocId = prepareProductSolrId(productModel); final List<PriceInformation> prices = priceService.getPriceInformationsForProduct(productModel); assertNotNull(prices);/* ww w .ja va 2 s. c om*/ assertFalse(prices.isEmpty()); final Double value = Double.valueOf(prices.get(0).getPriceValue().getValue()); if (value != null && value.doubleValue() > 0) { final String rangeStr = getPriceRangeForValue(value.doubleValue()); final IndexedProperty priceProperty = indexedType.getIndexedProperties().get("price"); final String priceField = fieldNameProvider.getFieldName(priceProperty, currency, FieldType.INDEX); final String solrQuery = priceField + ":\"" + rangeStr + "\" AND id:\"" + solrDocId + "\""; final QueryResponse solrResponse = solrServer.query(new SolrQuery(solrQuery)); final SolrDocumentList solrDocumentList = solrResponse.getResults(); assertNotNull(solrDocumentList); assertEquals("Price range " + rangeStr + " was not find for currency " + currency + ", solrDocId: " + solrDocId, 1, solrDocumentList.getNumFound()); } } }
From source file:de.kp.ames.web.core.search.SearcherImpl.java
License:Open Source License
public String search(String request, String start, String limit) throws Exception { /*// ww w . j av a2s.c o m * Build Apache Solr query */ JSONObject jQuery = new JSONObject(request); String term = jQuery.has(JsonConstants.J_TERM) ? jQuery.getString(JsonConstants.J_TERM) : "*"; String fp = setFacets(jQuery); SolrQuery query = new SolrQuery(); if (fp != null) query.addFilterQuery(fp); /* * Paging support from Apache Solr */ int s = Integer.valueOf(start); int r = Integer.valueOf(limit); query.setStart(s); query.setRows(r); String qs = term + " OR " + SearchConstants.TERMS_FIELD + ":" + term; query.setQuery(qs); QueryResponse response = solrProxy.executeQuery(query); SolrDocumentList docs = response.getResults(); long total = docs.getNumFound(); /* * Sort search result */ StringCollector collector = new StringCollector(); Iterator<SolrDocument> iter = docs.iterator(); while (iter.hasNext()) { int pos = -1; SolrDocument doc = iter.next(); JSONObject jDoc = new JSONObject(); /* * Identifier */ String id = (String) doc.getFieldValue(SearchConstants.S_ID); jDoc.put(JsonConstants.J_ID, id); /* * Name */ String name = (String) doc.getFieldValue(SearchConstants.S_NAME); jDoc.put(JsonConstants.J_NAME, name); /* * Source */ String source = (String) doc.getFieldValue(SearchConstants.S_FACET); pos = source.lastIndexOf(":"); jDoc.put(JsonConstants.J_FACET, source.substring(pos + 1)); /* * Description */ String desc = (String) doc.getFieldValue(SearchConstants.S_DESC); desc = (desc == null) ? FncMessages.NO_DESCRIPTION_DESC : desc; jDoc.put(JsonConstants.J_DESC, desc); collector.put(name, jDoc); } /* * Render result */ JSONArray jArray = new JSONArray(collector.values()); return renderer.createGrid(jArray, total); }
From source file:ec.edu.ucuenca.dcc.sld.SolrConnection.java
public void update(String uri, String[] var, String[] val) throws SolrServerException, IOException { assert var.length == val.length; NamedList params = new NamedList(); String qry = "uri:\"" + uri + "\""; params.add("q", qry); params.add("fl", "*"); SolrParams toSolrParams = SolrParams.toSolrParams(params); QueryResponse query = Solr.query(toSolrParams, SolrRequest.METHOD.POST); SolrDocumentList results = query.getResults(); assert results.getNumFound() != 0; SolrDocument get = results.get(0);/* w w w . j a va 2 s . c o m*/ SolrInputDocument document = new SolrInputDocument(); Set<Map.Entry<String, Object>> entrySet = get.entrySet(); for (Map.Entry<String, Object> en : entrySet) { document.setField(en.getKey(), en.getValue()); } for (int i = 0; i < var.length; i++) { document.remove(var[i]); document.setField(var[i], val[i]); } Solr.deleteByQuery(qry); Solr.commit(); UpdateResponse add = Solr.add(document); Solr.commit(); }
From source file:edu.cmu.lti.oaqa.annographix.apps.SolrQueryApp.java
License:Apache License
public static void main(String[] args) { Options options = new Options(); options.addOption("u", null, true, "Solr URI"); options.addOption("q", null, true, "Query"); options.addOption("n", null, true, "Max # of results"); options.addOption("o", null, true, "An optional TREC-style output file"); options.addOption("w", null, false, "Do a warm-up query call, before each query"); CommandLineParser parser = new org.apache.commons.cli.GnuParser(); BufferedWriter trecOutFile = null; try {/*from w w w. j av a 2 s.c o m*/ CommandLine cmd = parser.parse(options, args); String queryFile = null, solrURI = null; if (cmd.hasOption("u")) { solrURI = cmd.getOptionValue("u"); } else { Usage("Specify Solr URI"); } SolrServerWrapper solr = new SolrServerWrapper(solrURI); if (cmd.hasOption("q")) { queryFile = cmd.getOptionValue("q"); } else { Usage("Specify Query file"); } int numRet = 100; if (cmd.hasOption("n")) { numRet = Integer.parseInt(cmd.getOptionValue("n")); } if (cmd.hasOption("o")) { trecOutFile = new BufferedWriter(new FileWriter(new File(cmd.getOptionValue("o")))); } List<String> fieldList = new ArrayList<String>(); fieldList.add(UtilConst.ID_FIELD); fieldList.add(UtilConst.SCORE_FIELD); double totalTime = 0; double retQty = 0; ArrayList<Double> queryTimes = new ArrayList<Double>(); boolean bDoWarmUp = cmd.hasOption("w"); if (bDoWarmUp) { System.out.println("Using a warmup step!"); } int queryQty = 0; for (String t : FileUtils.readLines(new File(queryFile))) { t = t.trim(); if (t.isEmpty()) continue; int ind = t.indexOf('|'); if (ind < 0) throw new Exception("Wrong format, line: '" + t + "'"); String qID = t.substring(0, ind); String q = t.substring(ind + 1); SolrDocumentList res = null; if (bDoWarmUp) { res = solr.runQuery(q, fieldList, numRet); } Long tm1 = System.currentTimeMillis(); res = solr.runQuery(q, fieldList, numRet); Long tm2 = System.currentTimeMillis(); retQty += res.getNumFound(); System.out.println(qID + " Obtained: " + res.getNumFound() + " entries in " + (tm2 - tm1) + " ms"); double delta = (tm2 - tm1); totalTime += delta; queryTimes.add(delta); ++queryQty; if (trecOutFile != null) { ArrayList<SolrRes> resArr = new ArrayList<SolrRes>(); for (SolrDocument doc : res) { String id = (String) doc.getFieldValue(UtilConst.ID_FIELD); float score = (Float) doc.getFieldValue(UtilConst.SCORE_FIELD); resArr.add(new SolrRes(id, "", score)); } SolrRes[] results = resArr.toArray(new SolrRes[resArr.size()]); Arrays.sort(results); SolrEvalUtils.saveTrecResults(qID, results, trecOutFile, TREC_RUN, results.length); } } double devTime = 0, meanTime = totalTime / queryQty; for (int i = 0; i < queryQty; ++i) { double d = queryTimes.get(i) - meanTime; devTime += d * d; } devTime = Math.sqrt(devTime / (queryQty - 1)); System.out.println(String.format("Query time, mean/standard dev: %.2f/%.2f (ms)", meanTime, devTime)); System.out.println(String.format("Avg # of docs returned: %.2f", retQty / queryQty)); solr.close(); trecOutFile.close(); } catch (ParseException e) { Usage("Cannot parse arguments"); } catch (Exception e) { System.err.println("Terminating due to an exception: " + e); System.exit(1); } }
From source file:edu.cmu.lti.oaqa.bio.index.medline.annotated.query.SimpleQueryApp.java
License:Apache License
public static void main(String[] args) { Options options = new Options(); options.addOption("u", null, true, "Solr URI"); options.addOption("n", null, true, "Max # of results"); CommandLineParser parser = new org.apache.commons.cli.GnuParser(); try {//from w ww. j a v a2 s . co m CommandLine cmd = parser.parse(options, args); String solrURI = null; solrURI = cmd.getOptionValue("u"); if (solrURI == null) { Usage("Specify Solr URI"); } SolrServerWrapper solr = new SolrServerWrapper(solrURI); int numRet = 10; if (cmd.hasOption("n")) { numRet = Integer.parseInt(cmd.getOptionValue("n")); } List<String> fieldList = new ArrayList<String>(); fieldList.add(UtilConstMedline.ID_FIELD); fieldList.add(UtilConstMedline.SCORE_FIELD); fieldList.add(UtilConstMedline.ARTICLE_TITLE_FIELD); fieldList.add(UtilConstMedline.ENTITIES_DESC_FIELD); fieldList.add(UtilConstMedline.ABSTRACT_TEXT_FIELD); BufferedReader sysInReader = new BufferedReader(new InputStreamReader(System.in)); Joiner commaJoiner = Joiner.on(','); while (true) { System.out.println("Input query: "); String query = sysInReader.readLine(); if (null == query) break; QueryTransformer qt = new QueryTransformer(query); String tranQuery = qt.getQuery(); System.out.println("Translated query:"); System.out.println(tranQuery); System.out.println("========================="); SolrDocumentList res = solr.runQuery(tranQuery, fieldList, numRet); System.out.println("Found " + res.getNumFound() + " entries"); for (SolrDocument doc : res) { String id = (String) doc.getFieldValue(UtilConstMedline.ID_FIELD); float score = (Float) doc.getFieldValue(UtilConstMedline.SCORE_FIELD); String title = (String) doc.getFieldValue(UtilConstMedline.ARTICLE_TITLE_FIELD); String titleAbstract = (String) doc.getFieldValue(UtilConstMedline.ABSTRACT_TEXT_FIELD); System.out.println(score + " PMID=" + id + " " + titleAbstract); String entityDesc = (String) doc.getFieldValue(UtilConstMedline.ENTITIES_DESC_FIELD); System.out.println("Entities:"); for (EntityEntry e : EntityEntry.parseEntityDesc(entityDesc)) { System.out.println(String.format("[%d %d] concept=%s concept_ids=%s", e.mStart, e.mEnd, e.mConcept, commaJoiner.join(e.mConceptIds))); } } } solr.close(); } catch (ParseException e) { Usage("Cannot parse arguments"); } catch (Exception e) { System.err.println("Terminating due to an exception: " + e); System.exit(1); } }
From source file:edu.cmu.lti.oaqa.knn4qa.cand_providers.SolrCandidateProvider.java
License:Apache License
@Override public CandidateInfo getCandidates(int queryNum, Map<String, String> queryData, int maxQty) throws Exception { ArrayList<CandidateEntry> resArr = new ArrayList<CandidateEntry>(); String queryID = queryData.get(ID_FIELD_NAME); if (null == queryID) { throw new Exception( String.format("Query id (%s) is undefined for query # %d", ID_FIELD_NAME, queryNum)); }/*from w w w . java2 s. com*/ String text = queryData.get(TEXT_FIELD_NAME); if (null == text) { throw new Exception(String.format("Query (%s) is undefined for query # %d", TEXT_FIELD_NAME, queryNum)); } String query = text; if (mMatchPCT > Float.MIN_NORMAL) { int qty = text.split(" +").length; // Rounding one half up int matchNum = (qty * mMatchPCT + 50) / 100; query = String.format("_query_: \"{!edismax mm=%d} %s \"", matchNum, text); } //System.out.println("SOLR Query: " + query); List<String> fieldList = new ArrayList<String>(); fieldList.add(ID_FIELD_NAME); fieldList.add(UtilConst.SCORE_FIELD); SolrDocumentList solrRes = mSolr.runQuery(query, TEXT_FIELD_NAME, fieldList, null, maxQty); for (SolrDocument doc : solrRes) { String id = (String) doc.getFieldValue(ID_FIELD_NAME); float score = (Float) doc.getFieldValue(UtilConst.SCORE_FIELD); resArr.add(new CandidateEntry(id, score)); } CandidateEntry[] results = resArr.toArray(new CandidateEntry[resArr.size()]); Arrays.sort(results); return new CandidateInfo((int) solrRes.getNumFound(), results); }
From source file:edu.cornell.mannlib.vitro.utilities.solrtest.SolrConfigTester.java
License:Open Source License
private void assertQueryResults(String message, String query, String... expectedDocIds) throws SolrServerException { ModifiableSolrParams params = new ModifiableSolrParams(); params.set("q", query); QueryResponse qResp = server.query(params); SolrDocumentList docList = qResp.getResults(); List<String> expected = Arrays.asList(expectedDocIds); List<String> actual = new ArrayList<>(); for (int i = 0; i < docList.getNumFound(); i++) { actual.add(String.valueOf(docList.get(i).getFirstValue("DocId"))); }//from w w w. j a v a 2 s . c o m assertEquals(message + " : '" + query + "'", expected, actual); }
From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListQueryResults.java
License:Open Source License
public static IndividualListQueryResults runQuery(SolrQuery query, IndividualDao indDao, ServletContext context) throws SolrServerException { SolrServer solr = SolrSetup.getSolrServer(context); QueryResponse response = null;//from ww w.j a va 2s .c o m response = solr.query(query); if (response == null) { log.debug("response from search query was null"); return EMPTY_RESULT; } SolrDocumentList docs = response.getResults(); if (docs == null) { log.debug("results from search query response was null"); return EMPTY_RESULT; } // get list of individuals for the search results long hitCount = docs.getNumFound(); log.debug("Number of search results: " + hitCount); List<Individual> individuals = new ArrayList<Individual>(docs.size()); for (SolrDocument doc : docs) { String uri = doc.get(VitroSearchTermNames.URI).toString(); Individual individual = indDao.getIndividualByURI(uri); if (individual == null) { log.debug("No individual for search document with uri = " + uri); } else { individuals.add(individual); log.debug("Adding individual " + uri + " to individual list"); } } return new IndividualListQueryResults((int) hitCount, individuals); }