Example usage for org.apache.solr.search SolrIndexSearcher search

List of usage examples for org.apache.solr.search SolrIndexSearcher search

Introduction

In this page you can find the example usage for org.apache.solr.search SolrIndexSearcher search.

Prototype

public QueryResult search(QueryResult qr, QueryCommand cmd) throws IOException 

Source Link

Usage

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java

License:Apache License

@Test
public void testBasics() throws Exception {
    System.err.println(cc.getAllCoreNames());
    populate();//from   w ww .  j a va 2  s  . com
    SolrCore target = cc.getCore("target");
    SolrIndexSearcher searcher = target.getSearcher().get();
    Query q = new MatchAllDocsQuery();
    try {
        // verify the stored parts
        TopDocs td = searcher.search(q, 101);
        assertNotNull(td);
        for (ScoreDoc sd : td.scoreDocs) {
            Document doc = searcher.doc(sd.doc);
            String[] vals = doc.getValues("id");
            assertNotNull(vals);
            assertEquals(1, vals.length);
            String id = vals[0];
            vals = doc.getValues("text");
            assertNotNull(vals);
            assertEquals(1, vals.length);
            if (!id.equals("id100")) {
                // should have also the sidecar fields
                vals = doc.getValues("side1_t");
                assertNotNull(vals);
                assertEquals(1, vals.length);
                vals = doc.getValues("side2_t");
                assertNotNull(vals);
                assertEquals(1, vals.length);
            } else {
                // should not have the sidecar fields
                vals = doc.getValues("side1_t");
                assertTrue(vals == null || vals.length == 0);
                vals = doc.getValues("side2_t");
                assertTrue(vals == null || vals.length == 0);
            }
        }
        // verify the inverted parts
        q = new TermQuery(new Term("side1_t", "foo"));
        td = searcher.search(q, 101);
        assertEquals(100, td.totalHits);
        q = new TermQuery(new Term("side2_t", "blah"));
        td = searcher.search(q, 101);
        assertEquals(100, td.totalHits);
    } finally {
        searcher.close();
        target.close();
    }
}

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java

License:Apache License

@Test
public void testChanges() throws Exception {
    populate();//  www.  j  a  va  2s . co m
    // add some docs, overwriting some of the existing ones
    SolrCore target = cc.getCore("target");
    try {
        for (int i = 50; i < 150; i++) {
            AddUpdateCommand cmd = new AddUpdateCommand(makeReq(target));
            cmd.overwrite = true;
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", "id" + i);
            doc.addField("text", "new document " + i);
            cmd.solrDoc = doc;
            target.getUpdateHandler().addDoc(cmd);
        }
        target.getUpdateHandler().commit(new CommitUpdateCommand(makeReq(target), false));
    } finally {
        target.close();
    }
    target = cc.getCore("target");
    SolrIndexSearcher searcher = target.getSearcher().get();
    Query q = new MatchAllDocsQuery();
    try {
        // verify the stored parts
        TopDocs td = searcher.search(q, 151);
        assertNotNull(td);
        for (ScoreDoc sd : td.scoreDocs) {
            Document doc = searcher.doc(sd.doc);
            System.err.println(doc);
        }
    } finally {
        searcher.close();
        target.close();
    }
}

From source file:examples.adsabs.BlackBoxFailingRecords.java

License:Apache License

public void testImport() throws Exception {

    WaitingDataImportHandler handler = (WaitingDataImportHandler) h.getCore()
            .getRequestHandler("/invenio/import");
    SolrCore core = h.getCore();/*from w ww. ja va2 s.c  o m*/

    SolrQueryRequest req = req("command", "full-import", "commit", "true", "url",
            "python://search?p=" + URLEncoder.encode("recid:1->104", "UTF-8"));
    SolrQueryResponse rsp = new SolrQueryResponse();
    core.execute(handler, req, rsp);

    assertU(commit());
    assertQ(req("q", "*:*", "fl", "recid,title"), "//*[@numFound='22']");
    assertQ(req("q", "id:84"), "//*[@numFound='1']");
    assertQ(req("q", "id:78"), "//*[@numFound='1']");
    assertQ(req("q", "abstract:\"Hubbard-Stratonovich\""), "//*[@numFound='1']");

    // clean the slate
    assertU(delQ("*:*"));
    assertU(commit());
    assertQ(req("q", "*:*"), "//*[@numFound='0']");

    failThis.put("78", true);
    failThis.put("84", true);

    req = req("command", "full-import", "commit", "true", "writerImpl", TestFailingWriter.class.getName(),
            "url", "python://search?p=" + URLEncoder.encode("recid:1->60 OR recid:61->104", "UTF-8"));
    rsp = new SolrQueryResponse();
    core.execute(handler, req, rsp);

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='3']",
            "//str[@name='failedRecs'][.='0']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='0']", "//str[@name='registeredRequests'][.='3']",
            "//str[@name='restartedRequests'][.='0']", "//str[@name='docsToCheck'][.='103']",
            "//str[@name='status'][.='idle']");
    assertQ(req("qt", "/invenio-doctor", "command", "detailed-info"), "//str[@name='queueSize'][.='3']",
            "//str[@name='failedRecs'][.='0']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='0']", "//str[@name='registeredRequests'][.='3']",
            "//str[@name='restartedRequests'][.='0']", "//str[@name='docsToCheck'][.='103']",
            "//str[@name='status'][.='idle']", "*[count(//arr[@name='toBeDone']/str)=3]",
            "*[count(//arr[@name='failedBatches']/str)=0]");

    InvenioDoctor doctor = (InvenioDoctor) h.getCore().getRequestHandler("/invenio-doctor");
    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='status'][.='busy']");
        Thread.sleep(300);
    }

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='status'][.='idle']");

    assertQ(req("q", "*:*"), "//*[@numFound='20']");
    assertQ(req("q", "id:84"), "//*[@numFound='0']");
    assertQ(req("q", "id:83"), "//*[@numFound='1']");
    assertQ(req("q", "id:85"), "//*[@numFound='1']");

    assertQ(req("q", "id:78"), "//*[@numFound='0']");
    assertQ(req("q", "id:77"), "//*[@numFound='1']");
    assertQ(req("q", "id:79"), "//*[@numFound='1']");

    String response = h.query("/invenio-doctor", req("qt", "/invenio-doctor", "command", "detailed-info"));

    //System.out.println(response);

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='2']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='2']", "//str[@name='registeredRequests'][.='21']",
            "//str[@name='restartedRequests'][.='21']", "//str[@name='docsToCheck'][.='0']",
            "//str[@name='status'][.='idle']");

    assertQ(req("qt", "/invenio-doctor", "command", "reset"));
    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='0']");

    // now we expect the least possible number of restarts
    req = req("command", "full-import", "commit", "true", "writerImpl", TestFailingWriter.class.getName(),
            "url", "python://search?p=" + URLEncoder.encode("recid:82->86", "UTF-8"));
    rsp = new SolrQueryResponse();
    core.execute(handler, req, rsp);

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='3']",
            "//str[@name='failedRecs'][.='0']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='0']", "//str[@name='registeredRequests'][.='3']",
            "//str[@name='restartedRequests'][.='0']", "//str[@name='docsToCheck'][.='3']",
            "//str[@name='status'][.='idle']");

    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='1']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='1']", "//str[@name='registeredRequests'][.='3']",
            "//str[@name='restartedRequests'][.='3']", "//str[@name='docsToCheck'][.='0']",
            "//str[@name='status'][.='idle']");

    // now test the new component which looks inside the invenio db
    // discovers the missing recs and calls indexing on them

    assertU(commit());
    assertQ(req("q", "recid:77 OR recid:80"), "//*[@numFound='2']");
    assertU(delQ("recid:77 OR recid:80"));

    // this is necessary, otherwise the results may be wrong because
    // lucene cache is used to compare records (and the cache may
    // contain deleted records even after the commit)
    assertU(commit("expungeDeletes", "true"));
    assertQ(req("q", "recid:77 OR recid:80"), "//*[@numFound='0']");

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='1']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='1']", "//str[@name='registeredRequests'][.='3']",
            "//str[@name='restartedRequests'][.='3']", "//str[@name='docsToCheck'][.='0']",
            "//str[@name='status'][.='idle']");

    req = req("command", "discover", "params", "batchSize=50");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertU(commit());

    assertQ(req("q", "recid:77 OR recid:80"), "//*[@numFound='2']");

    assertQ(req("qt", "/invenio-doctor", "command", "info"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='1']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='1']", "//str[@name='registeredRequests'][.='7']",
            "//str[@name='restartedRequests'][.='7']", "//str[@name='docsToCheck'][.='0']",
            "//str[@name='status'][.='idle']");

    assertQ(req("qt", "/invenio-doctor", "command", "show-missing"), "//str[@name='queueSize'][.='0']",
            "//str[@name='failedRecs'][.='1']", "//str[@name='failedBatches'][.='0']",
            "//str[@name='failedTotal'][.='1']", "//str[@name='registeredRequests'][.='7']",
            "//str[@name='restartedRequests'][.='7']", "//str[@name='docsToCheck'][.='0']",
            "//str[@name='status'][.='idle']", "//arr[@name='missingRecs']/int[.='77']",
            "//arr[@name='missingRecs']/int[.='80']");

    assertQ(req("qt", "/invenio-doctor", "command", "reset"), null);
    assertQ(req("qt", "/invenio-doctor", "command", "discover", "params",
            "batchSize=1&fetch_size=2&max_records=1"), null);
    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertU(commit());
    assertQ(req("q", "recid:77 OR recid:80"), "//*[@numFound='2']");

    /*
    assertQ(req("qt", "/invenio-doctor", "command", "detailed-info"), 
      "//str[@name='registeredRequests'][.='9']",
      "//str[@name='restartedRequests'][.='9']",
      "//str[@name='status'][.='idle']"
    );
     */

    // verify that deleted recs are discovered
    //MontySolrVM.INSTANCE.evalCommand("sys.stderr.write(str(self._handler._db['*:create_record'].__module__))");
    //MontySolrVM.INSTANCE.evalCommand("sys.stderr.write(sys.modules['monty_invenio.tests.demotest_updating'].__file__ + '\\n')");
    PythonMessage message = MontySolrVM.INSTANCE.createMessage("create_record").setParam("diff", 5)
            .setParam("data", "970:a:bibcodeXXXXXXXXXXXX");
    MontySolrVM.INSTANCE.sendMessage(message);
    Integer added = (Integer) message.getResults();
    tempRecids.add(added);

    assertQ(req("qt", "/invenio-doctor", "command", "discover"), null);
    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);
    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertU(commit());
    assertQ(req("q", "recid:" + added), "//*[@numFound='1']");

    message = MontySolrVM.INSTANCE.createMessage("delete_record").setParam("recid", added).setParam("diff", 7);
    MontySolrVM.INSTANCE.sendMessage(message);

    assertQ(req("qt", "/invenio-doctor", "command", "discover"), null);
    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);
    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertU(commit());
    assertQ(req("q", "recid:" + added), "//*[@numFound='0']");

    // now delete records inside solr and see whether the doctor can
    // discover them and recover them

    assertU(delQ("*:*"));
    assertU(commit());
    assertQ(req("q", "*:*"), "//*[@numFound='0']");

    failThis.clear();

    req = req("command", "discover");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    assertU(commit());

    assertQ(req("q", "*:*"), "//*[@numFound='22']");

    // check that force-reindexing will update recs
    RefCounted<SolrIndexSearcher> searcher = h.getCore().getSearcher();
    SolrIndexSearcher s = searcher.get();
    Document doc77 = s.doc(s.search(new TermQuery(new Term("recid", "77")), 1).scoreDocs[0].doc);
    String is = doc77.get("indexstamp");

    req = req("command", "force-reindexing");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }

    req = req("command", "start");
    rsp = new SolrQueryResponse();
    core.execute(doctor, req, rsp);

    while (doctor.isBusy()) {
        Thread.sleep(300);
    }
    assertU(commit());

    Document doc77b = s.doc(s.search(new TermQuery(new Term("recid", "77")), 1).scoreDocs[0].doc);
    String is2 = doc77b.get("indexstamp");

    assertQ(req("q", "*:*"), "//*[@numFound='22']");
    assertTrue("Docs were not re-indexed", !is.equals(is2));

    s.close();
    searcher.decref();

}

From source file:in.geocoder.component.GeocodingComponent.java

License:Apache License

private List<Permutation> validate(SolrIndexSearcher searcher, List<Permutation> permutations) {
    List<Permutation> validPerms = new ArrayList<Permutation>();

    for (Permutation p : permutations) {

        BooleanQuery bq = new BooleanQuery();
        int level = -1;
        for (Classification c : p.classifications) {
            if (c != null) {
                String fld = c.classification;
                bq.add(new TermQuery(new Term(fld, c.text)), Occur.MUST);
                level = Math.max(level, hierarchyList.indexOf(c.symbol));
            }//from   w  w  w.ja  v a  2 s .  c  o m
        }
        if (level != -1)
            bq.add(new TermQuery(new Term(levelField, Character.toString(hierarchyList.get(level)))),
                    Occur.MUST);

        try {
            if (searcher.search(bq, 1).totalHits > 0) {
                p.query = bq;
                validPerms.add(p);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return validPerms;
}

From source file:in.geocoder.component.GeocodingComponent.java

License:Apache License

private List<GeocoderResult> getDocuments(SolrIndexSearcher searcher, List<Permutation> permutations) {
    List<GeocoderResult> gcResults = new ArrayList<GeocoderResult>();

    for (Permutation p : permutations) {
        if (p.query == null)
            continue;
        try {//from  www .j a  v  a  2s  .  co  m
            TopDocs results = searcher.search(p.query, 1);
            if (results.totalHits > 0) {
                Document doc = searcher.doc(results.scoreDocs[0].doc);
                GeocoderResult gcRes = new GeocoderResult();

                gcRes.setPermutation(p);
                StringBuilder unmatched = new StringBuilder();
                for (int i = 0; i < p.fullAnnotation.length(); i++)
                    if (p.fullAnnotation.charAt(i) == '.')
                        unmatched.append(p.queryTokens.get(i) + " ");

                gcRes.addField("unmatched", unmatched.toString().trim());
                for (String field : hierarchicalFields.values())
                    if (doc.get(field) != null)
                        gcRes.addField(field, doc.get(field));
                for (String field : otherFields.values())
                    if (doc.get(field) != null)
                        gcRes.addField(field, doc.get(field));

                gcRes.setUnmatched(unmatched.toString().trim());
                gcRes.addField(geoField, doc.get(geoField));
                gcResults.add(gcRes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return gcResults;
}

From source file:net.semanticmetadata.lire.solr.FastLireRequestHandler.java

License:Open Source License

/**
 * Handles the get parameters id, field and rows.
 *
 * @param req/*w  w w  .jav a 2 s  .  c o  m*/
 * @param rsp
 * @throws java.io.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);
        String paramField = "cl_ha";
        if (req.getParams().get("field") != null)
            paramField = req.getParams().get("field");
        LireFeature queryFeature = (LireFeature) FeatureRegistry.getClassForHashField(paramField).newInstance();
        rsp.add("QueryField", paramField);
        rsp.add("QueryFeature", queryFeature.getClass().getName());
        numberOfQueryTerms = req.getParams().getDouble("accuracy", DEFAULT_NUMBER_OF_QUERY_TERMS);
        numberOfCandidateResults = req.getParams().getInt("candidates", DEFAULT_NUMBER_OF_CANDIDATES);
        if (hits.scoreDocs.length > 0) {

            // Using DocValues to get the actual data from the index.
            //                BinaryDocValues binaryValues = MultiDocValues.getBinaryValues(searcher.getIndexReader(), FeatureRegistry.getFeatureFieldName(paramField)); // ***  #
            //                if (binaryValues == null)
            //                    System.err.println("Could not find the DocValues of the query document. Are they in the index?");
            //                BytesRef bytesRef = new BytesRef();
            SortedSetDocValues sortedSetValues = MultiDocValues.getSortedSetValues(searcher.getIndexReader(),
                    paramField);
            long valueCount = sortedSetValues.getValueCount();
            List<Term> termFilter = Lists.newArrayList();
            for (int i = 0; i < valueCount; i++) {
                BytesRef v = new BytesRef();
                sortedSetValues.lookupOrd(i, v);
                termFilter.add(new Term(paramField, v));
            }
            int[] hashes = null;
            int paramRows = defaultNumberOfResults;
            if (req.getParams().getInt("rows") != null)
                paramRows = req.getParams().getInt("rows");
            /**
             //                bytesRef = binaryValues.get(hits.scoreDocs[0].doc);
             binaryValues.get(hits.scoreDocs[0].doc,bytesRef);
             //                Document d = searcher.getIndexReader().document(hits.scoreDocs[0].doc);
             //                String histogramFieldName = paramField.replace("_ha", "_hi");
             queryFeature.setByteArrayRepresentation(bytesRef.bytes, bytesRef.offset, bytesRef.length);
             int paramRows = defaultNumberOfResults;
             if (req.getParams().getInt("rows") != null)
             paramRows = req.getParams().getInt("rows");
             // Re-generating the hashes to save sgenerateHashespace (instead of storing them in the index)
             int[] hashes = BitSampling.generateHashes(queryFeature.getDoubleHistogram());
             List<Term> termFilter = createTermFilter(hashes, paramField);
             **/
            doSearch(req, rsp, searcher, paramField, paramRows, termFilter,
                    createQuery(hashes, paramField, numberOfQueryTerms), 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());
    }
}

From source file:net.semanticmetadata.lire.solr.FastLireRequestHandler.java

License:Open Source License

/**
 * Actual search implementation based on (i) hash based retrieval and (ii) feature based re-ranking.
 *
 * @param rsp/*from   ww w  . j  a  va 2 s .  c  o  m*/
 * @param searcher
 * @param hashFieldName the hash field name
 * @param maximumHits
 * @param terms
 * @param queryFeature
 * @throws java.io.IOException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
private void doSearch(SolrQueryRequest req, SolrQueryResponse rsp, SolrIndexSearcher searcher,
        String hashFieldName, int maximumHits, List<Term> terms, Query query, LireFeature queryFeature)
        throws IOException, IllegalAccessException, InstantiationException {
    // temp feature instance
    LireFeature tmpFeature = queryFeature.getClass().newInstance();
    // Taking the time of search for statistical purposes.
    time = System.currentTimeMillis();

    Filter filter = null;
    // if the request contains a filter:
    if (req.getParams().get("fq") != null) {
        // only filters with [<field>:<value> ]+ are supported
        StringTokenizer st = new StringTokenizer(req.getParams().get("fq"), " ");
        LinkedList<Term> filterTerms = new LinkedList<Term>();
        while (st.hasMoreElements()) {
            String[] tmpToken = st.nextToken().split(":");
            if (tmpToken.length > 1) {
                filterTerms.add(new Term(tmpToken[0], tmpToken[1]));
            }
        }
        if (filterTerms.size() > 0)
            filter = new TermsFilter(filterTerms);
    }

    TopDocs docs; // with query only.
    if (filter == null) {
        docs = searcher.search(query, numberOfCandidateResults);
    } else {
        docs = searcher.search(query, filter, numberOfCandidateResults);
    }
    //        TopDocs docs = searcher.search(query, new TermsFilter(terms), numberOfCandidateResults);   // with TermsFilter and boosting by simple query
    //        TopDocs docs = searcher.search(new ConstantScoreQuery(new TermsFilter(terms)), numberOfCandidateResults); // just with TermsFilter
    time = System.currentTimeMillis() - time;
    rsp.add("RawDocsCount", docs.scoreDocs.length + "");
    rsp.add("RawDocsSearchTime", time + "");
    // re-rank
    time = System.currentTimeMillis();
    TreeSet<SimpleResult> resultScoreDocs = new TreeSet<SimpleResult>();
    float maxDistance = -1f;
    float tmpScore;

    String featureFieldName = FeatureRegistry.getFeatureFieldName(hashFieldName);
    // iterating and re-ranking the documents.
    BinaryDocValues binaryValues = MultiDocValues.getBinaryValues(searcher.getIndexReader(), featureFieldName); // ***  #
    BytesRef bytesRef = new BytesRef();
    for (int i = 0; i < docs.scoreDocs.length; i++) {
        // using DocValues to retrieve the field values ...
        binaryValues.get(docs.scoreDocs[i].doc, bytesRef);
        tmpFeature.setByteArrayRepresentation(bytesRef.bytes, bytesRef.offset, bytesRef.length);
        // Getting the document from the index.
        // This is the slow step based on the field compression of stored fields.
        //            tmpFeature.setByteArrayRepresentation(d.getBinaryValue(name).bytes, d.getBinaryValue(name).offset, d.getBinaryValue(name).length);
        tmpScore = queryFeature.getDistance(tmpFeature);
        if (resultScoreDocs.size() < maximumHits) { // todo: There's potential here for a memory saver, think of a clever data structure that can do the trick without creating a new SimpleResult for each result.
            resultScoreDocs.add(
                    new SimpleResult(tmpScore, searcher.doc(docs.scoreDocs[i].doc), docs.scoreDocs[i].doc));
            maxDistance = resultScoreDocs.last().getDistance();
        } else if (tmpScore < maxDistance) {
            //                if it is nearer to the sample than at least one of the current set:
            //                remove the last one ...
            resultScoreDocs.remove(resultScoreDocs.last());
            //                add the new one ...
            resultScoreDocs.add(
                    new SimpleResult(tmpScore, searcher.doc(docs.scoreDocs[i].doc), docs.scoreDocs[i].doc));
            //                and set our new distance border ...
            maxDistance = resultScoreDocs.last().getDistance();
        }
    }
    //        System.out.println("** Creating response.");
    time = System.currentTimeMillis() - time;
    rsp.add("ReRankSearchTime", time + "");
    LinkedList list = new LinkedList();
    for (Iterator<SimpleResult> it = resultScoreDocs.iterator(); it.hasNext();) {
        SimpleResult result = it.next();
        HashMap m = new HashMap(2);
        m.put("d", result.getDistance());
        // add fields as requested:
        if (req.getParams().get("fl") == null) {
            m.put("id", result.getDocument().get("id"));
            if (result.getDocument().get("title") != null)
                m.put("title", result.getDocument().get("title"));
        } else {
            String fieldsRequested = req.getParams().get("fl");
            if (fieldsRequested.contains("score")) {
                m.put("score", result.getDistance());
            }
            if (fieldsRequested.contains("*")) {
                // all fields
                for (IndexableField field : result.getDocument().getFields()) {
                    String tmpField = field.name();
                    if (result.getDocument().getFields(tmpField).length > 1) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getValues(tmpField));
                    } else if (result.getDocument().getFields(tmpField).length > 0) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getFields(tmpField)[0].stringValue());
                    }
                }
            } else {
                StringTokenizer st;
                if (fieldsRequested.contains(","))
                    st = new StringTokenizer(fieldsRequested, ",");
                else
                    st = new StringTokenizer(fieldsRequested, " ");
                while (st.hasMoreElements()) {
                    String tmpField = st.nextToken();
                    if (result.getDocument().getFields(tmpField).length > 1) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getValues(tmpField));
                    } else if (result.getDocument().getFields(tmpField).length > 0) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getFields(tmpField)[0].stringValue());
                    }
                }
            }
        }
        //            m.put(field, result.getDocument().get(field));
        //            m.put(field.replace("_ha", "_hi"), result.getDocument().getBinaryValue(field));
        list.add(m);
    }
    rsp.add("docs", list);
    // rsp.add("Test-name", "Test-val");
}

From source file:net.semanticmetadata.lire.solr.LireRequestHandler.java

License:Open Source License

/**
 * Actual search implementation based on (i) hash based retrieval and (ii) feature based re-ranking.
 *
 * @param req           the SolrQueryRequest
 * @param rsp           the response to write the data to
 * @param searcher      the actual index searcher object to search the index
 * @param hashFieldName the name of the field the hashes can be found
 * @param maximumHits   the maximum number of hits, the smaller the faster
 * @param filterQuery   can be null//  ww w.j  a  v  a2 s . c om
 * @param query         the (Boolean) query for querying the candidates from the IndexSearcher
 * @param queryFeature  the image feature used for re-ranking the results
 * @throws IOException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
private void doSearch(SolrQueryRequest req, SolrQueryResponse rsp, SolrIndexSearcher searcher,
        String hashFieldName, int maximumHits, Query filterQuery, Query query, GlobalFeature queryFeature)
        throws IOException, IllegalAccessException, InstantiationException {
    // temp feature instance
    GlobalFeature tmpFeature = queryFeature.getClass().newInstance();
    // Taking the time of search for statistical purposes.
    time = System.currentTimeMillis();

    String featureFieldName = FeatureRegistry.getFeatureFieldName(hashFieldName);
    BinaryDocValues binaryValues = MultiDocValues.getBinaryValues(searcher.getIndexReader(), featureFieldName);

    time = System.currentTimeMillis() - time;
    rsp.add("DocValuesOpenTime", time + "");

    Iterator<Integer> docIterator;
    int numberOfResults = 0;
    time = System.currentTimeMillis();
    if (filterQuery != null) {
        DocList docList = searcher.getDocList(query, filterQuery, Sort.RELEVANCE, 0, numberOfCandidateResults);
        numberOfResults = docList.size();
        docIterator = docList.iterator();
    } else {
        TopDocs docs = searcher.search(query, numberOfCandidateResults);
        numberOfResults = docs.totalHits;
        docIterator = new TopDocsIterator(docs);
    }
    time = System.currentTimeMillis() - time;
    rsp.add("RawDocsCount", numberOfResults + "");
    rsp.add("RawDocsSearchTime", time + "");
    time = System.currentTimeMillis();
    TreeSet<CachingSimpleResult> resultScoreDocs = getReRankedResults(docIterator, binaryValues, queryFeature,
            tmpFeature, maximumHits, searcher);

    // Creating response ...
    time = System.currentTimeMillis() - time;
    rsp.add("ReRankSearchTime", time + "");
    LinkedList list = new LinkedList();
    for (Iterator<CachingSimpleResult> it = resultScoreDocs.iterator(); it.hasNext();) {
        CachingSimpleResult result = it.next();
        HashMap m = new HashMap(2);
        m.put("d", result.getDistance());
        // add fields as requested:
        if (req.getParams().get("fl") == null) {
            m.put("id", result.getDocument().get("id"));
            if (result.getDocument().get("title") != null)
                m.put("title", result.getDocument().get("title"));
        } else {
            String fieldsRequested = req.getParams().get("fl");
            if (fieldsRequested.contains("score")) {
                m.put("score", result.getDistance());
            }
            if (fieldsRequested.contains("*")) {
                // all fields
                for (IndexableField field : result.getDocument().getFields()) {
                    String tmpField = field.name();

                    if (result.getDocument().getFields(tmpField).length > 1) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getValues(tmpField));
                    } else if (result.getDocument().getFields(tmpField).length > 0) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getFields(tmpField)[0].stringValue());
                    }
                }
            } else {
                StringTokenizer st;
                if (fieldsRequested.contains(","))
                    st = new StringTokenizer(fieldsRequested, ",");
                else
                    st = new StringTokenizer(fieldsRequested, " ");
                while (st.hasMoreElements()) {
                    String tmpField = st.nextToken();
                    if (result.getDocument().getFields(tmpField).length > 1) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getValues(tmpField));
                    } else if (result.getDocument().getFields(tmpField).length > 0) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getFields(tmpField)[0].stringValue());
                    }
                }
            }
        }
        //            m.put(field, result.getDocument().get(field));
        //            m.put(field.replace("_ha", "_hi"), result.getDocument().getBinaryValue(field));
        list.add(m);
    }
    rsp.add("docs", list);
    // rsp.add("Test-name", "Test-val");
}

From source file:org.alfresco.solr.AbstractAlfrescoSolrTests.java

License:Open Source License

public static void waitForDocCount(Query query, long expectedNumFound, long waitMillis) throws Exception {
    Date date = new Date();
    long timeout = (long) date.getTime() + waitMillis;

    RefCounted<SolrIndexSearcher> ref = null;
    int totalHits = 0;
    while (new Date().getTime() < timeout) {
        try {/* w  w  w  . j  a v  a 2s.  c o  m*/
            ref = getCore().getSearcher();
            SolrIndexSearcher searcher = ref.get();
            TopDocs topDocs = searcher.search(query, 10);
            totalHits = topDocs.totalHits;
            if (topDocs.totalHits == expectedNumFound) {
                return;
            } else {
                Thread.sleep(2000);
            }
        } finally {
            ref.decref();
        }
    }
    throw new Exception(
            "Wait error expected " + expectedNumFound + " found " + totalHits + " : " + query.toString());
}

From source file:org.alfresco.solr.AbstractAlfrescoSolrTests.java

License:Open Source License

protected void assertAQuery(String queryString, int count, Locale locale, String[] textAttributes,
        String[] allAttributes, String... name) {
    RefCounted<SolrIndexSearcher> refCounted = null;
    try (SolrServletRequest solrQueryRequest = new SolrServletRequest(getCore(), null)) {
        refCounted = getCore().getSearcher();
        SolrIndexSearcher solrIndexSearcher = refCounted.get();
        SearchParameters searchParameters = new SearchParameters();
        searchParameters.setQuery(queryString);
        if (locale != null) {
            searchParameters.addLocale(locale);
        }//from www  .  ja  v  a 2 s  .c  o m

        if (textAttributes != null) {
            for (String textAttribute : textAttributes) {
                searchParameters.addTextAttribute(textAttribute);
            }
        }
        if (allAttributes != null) {
            for (String allAttribute : allAttributes) {
                searchParameters.addAllAttribute(allAttribute);
            }
        }

        Query query = dataModel.getLuceneQueryParser(searchParameters, solrQueryRequest,
                FTSQueryParser.RerankPhase.SINGLE_PASS).parse(queryString);
        LOG.debug("####### Query ######:" + query);
        TopDocs docs = solrIndexSearcher.search(query, count * 2 + 10);

        assertEquals(fixQueryString(queryString, name), count, docs.totalHits);
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    } finally {
        ofNullable(refCounted).ifPresent(RefCounted::decref);
    }
}