Example usage for org.apache.solr.common SolrDocument SolrDocument

List of usage examples for org.apache.solr.common SolrDocument SolrDocument

Introduction

In this page you can find the example usage for org.apache.solr.common SolrDocument SolrDocument.

Prototype

public SolrDocument() 

Source Link

Usage

From source file:alba.solr.common.MySimpleFloatFunction.java

License:Apache License

@DocTransformer(name = "addchild")
public void addChild(SolrDocument doc) {
    SolrDocument child = new SolrDocument();
    child.setField("field", "child doc value");

    doc.addChildDocument(child);/*from  ww w  .  j  a va 2 s  . c  o  m*/
}

From source file:alba.solr.common.MySimpleFloatFunction.java

License:Apache License

@SolrRequestHandler(name = "mydocs", value = "/prova2")
@firstComponents({ "prova", "functions", "test" })
@lastComponents({ "last1", "last2", "last3" })
public List<SolrDocument> requestHandlerList2(SolrQueryRequest req, SolrQueryResponse rsp) {

    List<SolrDocument> list = new ArrayList<SolrDocument>();

    SolrDocument doc1 = new SolrDocument();
    doc1.setField("testo", "ciao");

    SolrDocument doc2 = new SolrDocument();
    doc2.setField("abc", "prova");

    list.add(doc1);/*  w w  w.  ja  v a 2  s .c o m*/
    list.add(doc2);

    return list;

}

From source file:at.pagu.soldockr.core.convert.MappingSolrConverterTest.java

License:Apache License

@Test
public void testRead() {
    SolrDocument document = new SolrDocument();
    document.addField("stringProperty", "christoph");
    document.addField("intProperty", 32);

    ConvertableBean convertable = converter.read(ConvertableBean.class, (Map<String, Object>) document);

    Assert.assertEquals(document.getFieldValue("stringProperty"), convertable.getStringProperty());
    Assert.assertEquals(document.getFieldValue("intProperty"), convertable.getIntProperty());
}

From source file:au.org.aekos.shared.api.model.dataset.SharedSearchResultFactoryTest.java

License:Creative Commons License

/**
 * Can we tell when we ARE under embargo because the date is in the future?
 *///  ww w  .  ja v  a 2s .c  om
@Test
public void testDetermineIsEmbargoedOnDate01() {
    Date today = new Date();
    Calendar calendar = new GregorianCalendar();
    calendar.add(Calendar.YEAR, 1);
    Date oneYearInTheFuture = calendar.getTime();
    SolrDocument solrDoc = new SolrDocument();
    solrDoc.addField(prop("index-names.embargo-date"), oneYearInTheFuture);
    boolean result = objectUnderTest.determineIsEmbargoedOnDate(solrDoc, today);
    assertThat(result, is(true));
}

From source file:au.org.aekos.shared.api.model.dataset.SharedSearchResultFactoryTest.java

License:Creative Commons License

/**
 * Can we tell when we are NOT under embargo because the date is in the past?
 *///  ww  w.ja va 2 s  . co m
@Test
public void testDetermineIsEmbargoedOnDate02() {
    Date today = new Date();
    Calendar calendar = new GregorianCalendar();
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    Date yesterday = calendar.getTime();
    SolrDocument solrDoc = new SolrDocument();
    solrDoc.addField(prop("index-names.embargo-date"), yesterday);
    boolean result = objectUnderTest.determineIsEmbargoedOnDate(solrDoc, today);
    assertThat(result, is(false));
}

From source file:au.org.aekos.shared.api.model.dataset.SharedSearchResultFactoryTest.java

License:Creative Commons License

/**
 * Can we tell when we ARE under embargo because the date is today?
 *///from  w  w w . j  a  va  2  s  .co m
@Test
public void testDetermineIsEmbargoedOnDate03() {
    Date today = new Date();
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(today);
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 999);
    Date justBeforeMidnightToday = calendar.getTime();
    SolrDocument solrDoc = new SolrDocument();
    solrDoc.addField(prop("index-names.embargo-date"), justBeforeMidnightToday);
    boolean result = objectUnderTest.determineIsEmbargoedOnDate(solrDoc, today);
    assertThat(result, is(true));
}

From source file:com.basho.yokozuna.handler.EntropyData.java

License:Open Source License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
        throws Exception, InstantiationException, IllegalAccessException {

    String contParam = req.getParams().get("continue");
    BytesRef cont = contParam != null ? decodeCont(contParam) : DEFAULT_CONT;

    // TODO: Make before required in handler config
    String before = req.getParams().get("before");
    if (before == null) {
        throw new Exception("Parameter 'before' is required");
    }/*w w w. ja v a2  s.  co m*/
    int n = req.getParams().getInt("n", DEFAULT_N);
    SolrDocumentList docs = new SolrDocumentList();

    // Add docs here and modify object inline in code
    rsp.add("response", docs);

    try {
        SolrIndexSearcher searcher = req.getSearcher();
        AtomicReader rdr = searcher.getAtomicReader();
        BytesRef tmp = null;
        Terms terms = rdr.terms(ENTROPY_DATA_FIELD);
        TermsEnum te = terms.iterator(null);

        if (isContinue(cont)) {
            log.debug("continue from " + cont);

            TermsEnum.SeekStatus status = te.seekCeil(cont, true);

            if (status == TermsEnum.SeekStatus.END) {
                rsp.add("more", false);
                return;
            } else if (status == TermsEnum.SeekStatus.FOUND) {
                // If this term has already been seen then skip it.
                tmp = te.next();

                if (endOfItr(tmp)) {
                    rsp.add("more", false);
                    return;
                }
            } else if (status == TermsEnum.SeekStatus.NOT_FOUND) {
                tmp = te.next();
            }
        } else {
            tmp = te.next();
        }

        String text = null;
        String[] vals = null;
        String ts = null;
        String docId = null;
        String vectorClock = null;
        int count = 0;
        BytesRef current = null;

        while (!endOfItr(tmp) && count < n) {
            current = BytesRef.deepCopyOf(tmp);
            text = tmp.utf8ToString();
            log.debug("text: " + text);
            vals = text.split(" ");
            ts = vals[0];

            // TODO: what if null?
            if (!(ts.compareTo(before) < 0)) {
                rsp.add("more", false);
                docs.setNumFound(count);
                return;
            }

            docId = vals[1];
            vectorClock = vals[2];
            SolrDocument tmpDoc = new SolrDocument();
            tmpDoc.addField("doc_id", docId);
            tmpDoc.addField("base64_vclock", Base64.encodeBase64String(sha(vectorClock)));
            docs.add(tmpDoc);
            count++;
            tmp = te.next();
        }

        if (count < n) {
            rsp.add("more", false);
        } else {
            rsp.add("more", true);
            String newCont = Base64.encodeBase64URLSafeString(current.bytes);
            // The continue context for next req to start where
            // this one finished.
            rsp.add("continuation", newCont);
        }

        docs.setNumFound(count);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.doculibre.constellio.opensearch.OpenSearchSolrServer.java

License:Open Source License

@SuppressWarnings("unchecked")
private static SolrDocumentList parse(Element rootElement) throws IOException {
    SolrDocumentList solrDocumentList = new SolrDocumentList();
    Element channelElement = rootElement.element("channel");
    String totalResultsStr = channelElement.elementText(new QName("totalResults", NS_OPENSEARCH));
    String startIndexStr = channelElement.elementText(new QName("startIndex", NS_OPENSEARCH));
    long numFound = Long.parseLong(totalResultsStr);
    long start = Long.parseLong(startIndexStr);
    solrDocumentList.setNumFound(numFound);
    solrDocumentList.setStart(start);/*from  w w w  .j av a 2 s .  com*/

    for (Iterator<Element> it = channelElement.elementIterator("item"); it.hasNext();) {
        Element itemElement = it.next();
        String title = itemElement.elementText("title");
        String description = itemElement.elementText("description");
        String link = itemElement.elementText("link");

        title = CharSetUtils.convert(title, CharSetUtils.UTF_8, CharSetUtils.ISO_8859_1);
        description = CharSetUtils.convert(description, CharSetUtils.UTF_8, CharSetUtils.ISO_8859_1);
        link = CharSetUtils.convert(link, CharSetUtils.UTF_8, CharSetUtils.ISO_8859_1);

        SolrDocument solrDocument = new SolrDocument();
        solrDocument.addField("title", title);
        solrDocument.addField("description", description);
        solrDocument.addField("link", link);
        solrDocumentList.add(solrDocument);
    }
    return solrDocumentList;
}

From source file:com.doculibre.constellio.utils.NamedListUtilsTest.java

License:Open Source License

/**
 * The conversion of a SolrDocument and his NamedList form should be equal.
 * Servlets use the NamedList form/*from w  w w.j  a  va2  s.  c o m*/
 * 
 * @throws IOException
 */
@Test
public void testSolrDocumentListVSNamedList() throws IOException {
    NamedList<Object> l1 = new NamedList<Object>();
    SolrDocumentList l = new SolrDocumentList();
    SolrDocument d = new SolrDocument();
    d.setField("a", 123);
    d.setField("myArray", Arrays.asList(new String[] { "A", "B", "C" }));
    d.setField("title", "");
    l.add(d);
    l.setStart(22);
    l.setNumFound(1);
    l1.add(ServletsConstants.RESPONSE, l);

    NamedList<Object> l2 = new NamedList<Object>();
    NamedList<Object> nl = new NamedList<Object>();
    NamedList<Object> attr = new NamedList<Object>();
    attr.add("numFound", 1);
    attr.add("start", 22);
    nl.add("attr", attr);
    l2.add(ServletsConstants.RESPONSE, nl);
    NamedList<Object> nlDoc = new NamedList<Object>();
    nlDoc.add("a", 123);
    nlDoc.add("myArray", Arrays.asList(new String[] { "A", "B", "C" }));
    nlDoc.add("title", "");
    nl.add("doc", nlDoc);

    File tempFile1 = File.createTempFile("temp", ".xml");

    NamedListUtils.convertResponseNamedListToXML(l1, new FileOutputStream(tempFile1));
    String xml1 = readFileAsString(tempFile1);

    File tempFile2 = File.createTempFile("temp", ".xml");

    NamedListUtils.convertResponseNamedListToXML(l2, new FileOutputStream(tempFile2));
    String xml2 = readFileAsString(tempFile2);
    TestCase.assertEquals(xml1, xml2);
}

From source file:com.francelabs.datafari.statistics.StatsProcessor.java

License:Apache License

public static void processStatsResponse(final QueryResponse queryResponse) throws Exception {
    final NamedList responseHeader = queryResponse.getResponseHeader();
    final FacetField QFacet = queryResponse.getFacetField("q");

    final Long numTot = queryResponse.getResults().getNumFound();

    final SolrDocumentList solrDocumentList = new SolrDocumentList();
    solrDocumentList.setNumFound(QFacet.getValueCount());
    solrDocumentList.setStart(0);//from w ww .  ja  v a2 s .co  m

    if (numTot != 0) {
        final Map<String, FieldStatsInfo> stats = queryResponse.getFieldStatsInfo();
        final List<FieldStatsInfo> noHitsStats = stats.get("noHits").getFacets().get("q");
        final List<FieldStatsInfo> QTimeStats = stats.get("QTime").getFacets().get("q");
        List<FieldStatsInfo> positionClickTotStats = null;
        try {
            positionClickTotStats = stats.get("positionClickTot").getFacets().get("q");
        } catch (final Exception e) {

        }
        final List<FieldStatsInfo> clickStats = stats.get("click").getFacets().get("q");
        final List<FieldStatsInfo> numClicksStats = stats.get("numClicks").getFacets().get("q");
        final List<FieldStatsInfo> numFoundStats = stats.get("numFound").getFacets().get("q");

        final List<Count> QFacetValues = QFacet.getValues();

        final Map<String, SolrDocument> mapDocuments = new HashMap<String, SolrDocument>();

        for (int i = 0; i < QFacetValues.size(); i++) {
            final SolrDocument doc = new SolrDocument();
            final String query = QFacetValues.get(i).getName();

            final double count = QFacetValues.get(i).getCount();
            final double frequency = StatsUtils.round(count * 100 / numTot, 2, BigDecimal.ROUND_HALF_UP);

            doc.addField("query", query);

            doc.addField("count", count);
            doc.addField("frequency", frequency);
            mapDocuments.put(query, doc);
            solrDocumentList.add(doc);
        }

        for (int i = 0; i < QTimeStats.size(); i++) {
            final String query = QTimeStats.get(i).getName();
            final SolrDocument doc = mapDocuments.get(query);

            final int AVGHits = new Double((Double) numFoundStats.get(i).getMean()).intValue();
            final Double noHits = new Double((Double) noHitsStats.get(i).getSum());
            final int AVGQTime = new Double((Double) QTimeStats.get(i).getMean()).intValue();
            final int MAXQTime = new Double((Double) QTimeStats.get(i).getMax()).intValue();
            final double click = new Double((Double) clickStats.get(i).getSum());
            final double clickRatio = StatsUtils.round(click * 100 / (Double) doc.getFirstValue("count"), 2,
                    BigDecimal.ROUND_HALF_UP);
            if (click > 0) {
                final double AVGClickPosition = new Double((Double) positionClickTotStats.get(i).getSum()
                        / (Double) numClicksStats.get(i).getSum()).intValue();

                doc.addField("AVGClickPosition", AVGClickPosition);

            } else {
                doc.addField("AVGClickPosition", "-");
            }

            doc.addField("withClickRatio", clickRatio);
            doc.addField("AVGHits", AVGHits);
            doc.addField("numNoHits", noHits);
            doc.addField("withClick", click);
            doc.addField("AVGQTime", AVGQTime);
            doc.addField("MaxQTime", MAXQTime);
        }

    }

    final NamedList<Object> response = new SimpleOrderedMap<Object>();
    response.add("responseHeader", responseHeader);
    response.add("response", solrDocumentList);
    queryResponse.setResponse(response);
}