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

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

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public void setField(String name, Object value) 

Source Link

Document

Set a field with the given object.

Usage

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

License:Apache License

@DocTransformer(name = "reverse")
public void reverse(SolrDocument doc) {
    String label = doc.getFieldValue("label").toString();
    StringBuffer sb = new StringBuffer(label);
    doc.setField("label", sb.reverse().toString());
}

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   w w w.jav  a2 s .  c o  m
}

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

License:Apache License

@DocTransformer(name = "ucase")
public void ucase(SolrDocument doc, @Param(name = "field", description = "the field") String field) {
    doc.setField(field, doc.getFieldValue(field).toString().toUpperCase());
}

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);/*from  w w  w  . j a  v  a  2s.c  om*/
    list.add(doc2);

    return list;

}

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

License:Apache License

@DocTransformer(name = "transform")
public void test(SolrDocument doc) {
    doc.setField("abc", 12);
}

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 av a  2  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.github.fengtan.sophie.tables.DocumentsTable.java

License:Open Source License

/**
 * Update a document locally.//  w w  w. j a  v  a 2s.  c o m
 * 
 * @param item
 *            Row containing the document to update.
 * @param columnIndex
 *            Column index of the field to update.
 * @param newValue
 *            New value.
 */
private void updateDocument(TableItem item, int columnIndex, Object newValue) {
    SolrDocument document = (SolrDocument) item.getData("document");
    // The row may not contain any document (e.g. the first row, which
    // contains the filters).
    if (document == null) {
        return;
    }

    String fieldName = (String) table.getColumn(columnIndex).getData("fieldName");
    // We reduce by 1 since the first column is used for row ID.
    document.setField(fieldName, newValue);
    item.setText(columnIndex, Objects.toString(newValue, StringUtils.EMPTY));
    changeListener.changed();

    // If document was locally added, then leave it in documentsAdded and
    // return so it remains green.
    if (documentsAdded.contains(document)) {
        return;
    }

    // If document was locally deleted, then remove it from documentsDeleted
    // and let it go into documentsUpdated.
    if (documentsDeleted.contains(document)) {
        documentsDeleted.remove(document);
    }

    // Add document to documentsUpdated if it is not already there.
    if (!documentsUpdated.contains(document)) {
        documentsUpdated.add(document);
    }
    item.setBackground(YELLOW);
}

From source file:com.sindicetech.siren.solr.response.SirenProjectionTransformer.java

License:Open Source License

@Override
public void transform(SolrDocument doc, int docid) throws IOException {
    Query query = context.query;/*from   w w w.jav  a2  s. com*/

    SimpleJsonByQueryExtractor extractor = new SimpleJsonByQueryExtractor();

    try {
        IndexSchema schema = context.req.getSchema();

        for (String fieldName : doc.getFieldNames()) {
            FieldType ft = schema.getFieldOrNull(fieldName).getType();
            if (ft instanceof ExtendedJsonField) {
                String sirenField = (String) doc.getFieldValue(fieldName);
                String json = extractor.extractAsString(sirenField, query);
                if (json == null) {
                    // query doesn't contain variables, no transformation is necessary
                    continue;
                }

                doc.setField(fieldName, json);
            }
        }
    } catch (ProjectionException e) {
        throw new IOException(
                String.format("Problem while projecting (extracting variables from matched document id %s",
                        doc.getFieldValue("id")),
                e);
    }
}

From source file:com.villemos.ispace.consolidator.SynonymConsolidator.java

License:Open Source License

public void consolidate(CamelContext context) {

    ProducerTemplate retriever = context.createProducerTemplate();

    /** Iterate through all synonyms to REMOVE. */
    for (String synonym : removeSynonyms.keySet()) {
        for (String field : discreteFields) {

            /** Retrieve all documents with this synonym in one of the discrete fields. */
            Exchange exchange = new DefaultExchange(context);
            exchange.getIn().setHeader("ispace.query", field + ":" + synonym);
            retriever.send("direct:storage", exchange);

            if (exchange.getOut().getBody() != null) {
                /** Iterate through results and replace */
                for (SolrDocument document : (SolrDocumentList) exchange.getOut().getBody()) {
                    document.setField(field, "");

                    Exchange newExchange = new DefaultExchange(context);
                    configureExchange(newExchange, document);
                    retriever.send("direct:storage", newExchange);
                }//from  w w  w .j a v a 2  s.  c  o  m
            }
        }
    }

    /** Iterate through all synonyms to REMOVE. */
    for (String synonym : acceptedSynonyms.keySet()) {
        for (String field : discreteFields) {

            /** Retrieve all documents with this synonym in one of the discrete fields. */
            Exchange exchange = new DefaultExchange(context);
            exchange.getIn().setHeader("ispace.query", field + ":" + synonym);
            retriever.send("direct:storage", exchange);

            if (exchange.getOut().getBody() != null) {
                /** Iterate through results and replace */
                for (SolrDocument document : (SolrDocumentList) exchange.getOut().getBody()) {
                    document.setField(field, acceptedSynonyms.get(synonym));

                    Exchange newExchange = new DefaultExchange(context);
                    configureExchange(newExchange, document);
                    retriever.send("direct:storage", newExchange);
                }
            }
        }
    }

}

From source file:com.zb.app.external.lucene.solr.client.SolrClient.java

License:Open Source License

/**
 * solr//from   w w w.j a v  a2 s  . c  om
 * 
 * @param corename ?????
 * @param returnType Class
 * @param solrQuery {@link SolrQueryConvert}
 * @return
 */
@SuppressWarnings("unchecked")
public <T> List<T> querys(String corename, final Class<T> returnType, final SolrQuery solrQuery) {
    // 
    solrQuery.setParam("hl", "true"); // highlighting
    solrQuery.setParam("hl.fl", "lTile", "lMode", "lYesItem", "lNoItem", "lChildren", "lShop", "lExpenseItem",
            "lPreseItem", "rContent", "rCar");
    solrQuery.setHighlightSimplePre("<font color=\"red\">");
    solrQuery.setHighlightSimplePost("</font>");
    final SolrDocumentList list = new SolrDocumentList();
    final HttpSolrServer server = getOrCreateSolrServer(corename);
    exec(new Executor() {

        public Result exec() throws SolrServerException, IOException {
            QueryResponse query = null;
            if (solrQuery.toString().length() > MAX_URL_LENGTH) {
                query = server.query(solrQuery, SolrRequest.METHOD.POST);
            } else {
                query = server.query(solrQuery, SolrRequest.METHOD.GET);
            }
            SolrDocumentList documents = query.getResults();
            Map<String, Map<String, List<String>>> map = query.getHighlighting();
            for (SolrDocument document : documents) {
                document.setField("lTile", map.get(document.getFieldValue("id")).get("lTile"));
                list.add(document);
            }
            return Result.success();
        }
    });
    return (List<T>) toBeanList(list, returnType);
}