List of usage examples for org.apache.solr.search SolrIndexSearcher getFirstMatch
public int getFirstMatch(Term t) throws IOException
t Returns -1 if no document was found. From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactory.java
License:Apache License
private Document lookup(SolrIndexSearcher lookup, String id, BytesRef idRef, Set<String> fields) throws IOException { idRef.copyChars(id);/*from w w w. j a v a2 s.c o m*/ Term t = new Term("id", idRef); if (lookup.docFreq(t) == 0) { return null; } int docId = lookup.getFirstMatch(t); if (docId == -1) { return null; } Document doc = lookup.doc(docId, fields); if (doc == null) { return null; } doc.removeFields("id"); return doc; }
From source file:net.semanticmetadata.lire.solr.LireRequestHandler.java
License:Open Source License
/** * Handles the get parameters id, field and rows. * * @param req/*from www .j a v a2s .c o m*/ * @param rsp * @throws IOException * @throws InstantiationException * @throws IllegalAccessException */ private void handleIdSearch(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException, InstantiationException, IllegalAccessException { SolrIndexSearcher searcher = req.getSearcher(); try { // TopDocs hits = searcher.search(new TermQuery(new Term("id", req.getParams().get("id"))), 1); int queryDocId = searcher.getFirstMatch(new Term("id", req.getParams().get("id"))); // get the parameters String paramField = req.getParams().get("field", "cl_ha"); if (!paramField.endsWith("_ha")) paramField += "_ha"; numberOfQueryTerms = req.getParams().getDouble("accuracy", DEFAULT_NUMBER_OF_QUERY_TERMS); numberOfCandidateResults = req.getParams().getInt("candidates", DEFAULT_NUMBER_OF_CANDIDATES); useMetricSpaces = req.getParams().getBool("ms", DEFAULT_USE_METRIC_SPACES); int paramRows = req.getParams().getInt("rows", defaultNumberOfResults); GlobalFeature queryFeature = (GlobalFeature) FeatureRegistry.getClassForHashField(paramField) .newInstance(); rsp.add("QueryField", paramField); rsp.add("QueryFeature", queryFeature.getClass().getName()); if (queryDocId > -1) { // Using DocValues to get the actual data from the index. BinaryDocValues binaryValues = MultiDocValues.getBinaryValues(searcher.getIndexReader(), FeatureRegistry.getFeatureFieldName(paramField)); if (binaryValues == null) { rsp.add("Error", "Could not find the DocValues of the query document. Are they in the index? Id: " + req.getParams().get("id")); // System.err.println("Could not find the DocValues of the query document. Are they in the index?"); } queryFeature.setByteArrayRepresentation(binaryValues.get(queryDocId).bytes, binaryValues.get(queryDocId).offset, binaryValues.get(queryDocId).length); Query query = null; if (!useMetricSpaces) { // check singleton cache if the term stats can be cached. HashTermStatistics.addToStatistics(searcher, paramField); // Re-generating the hashes to save space (instead of storing them in the index) int[] hashes = BitSampling.generateHashes(queryFeature.getFeatureVector()); query = createQuery(hashes, paramField, numberOfQueryTerms); } else if (MetricSpaces.supportsFeature(queryFeature)) { // ----< Metric Spaces >----- int queryLength = (int) StatsUtils.clamp( numberOfQueryTerms * MetricSpaces.getPostingListLength(queryFeature), 3, MetricSpaces.getPostingListLength(queryFeature)); String msQuery = MetricSpaces.generateBoostedQuery(queryFeature, queryLength); QueryParser qp = new QueryParser(paramField.replace("_ha", "_ms"), new WhitespaceAnalyzer()); query = qp.parse(msQuery); } else { query = new MatchAllDocsQuery(); rsp.add("Error", "Feature not supported by MetricSpaces: " + queryFeature.getClass().getSimpleName()); } doSearch(req, rsp, searcher, paramField, paramRows, getFilterQuery(req.getParams().get("fq")), query, queryFeature); } else { rsp.add("Error", "Did not find an image with the given id " + req.getParams().get("id")); } } catch (Exception e) { rsp.add("Error", "There was an error with your search for the image with the id " + req.getParams().get("id") + ": " + e.getMessage()); } }