简体   繁体   中英

how to update document using solrj

Using scala + solrj. I need to read document from solr, add field to it and then to put it back in the server.

I've used something like the following code (as said, code is in scala):

val doc = client.getById(docId) // can be as result of query also
val inputDoc = ClientUtils.toSolrInputDocument(doc)
inputDoc.setField("_version_",0)
inputDoc.setField("new_field","new value")
client.add(inputDoc)
client.commit

Since solr 5.5 ClientUnits.toSolrInputDocument is deprecated. How should I do this now?

Many Thanks.

I suggest to create a pojo that map your document structure (if you can, take a look at your schema.xml):

import org.apache.solr.client.solrj.beans.Field;

public class MyDocument {

    @Field("id")
    private Long id;

    @Field("site")
    private Integer site;

    @Field("key")
    private String key;

    @Field("suggestions") // Multivalue field
    private List<String> suggestions;

    @Field("rank")
    private Float rankBoost;
    ... // add getter and setter

then create a list of List<MyDocument> docQueue where you put all your data.

    MyDocument doc = new MyDocument();
    doc.setKey("test");
    docQueue.add(doc);
    client.addBeans(docQueue);
    docQueue.clear();
    client.commit();

If you have many documents I also suggest to add in group of 500/1000 at time. Choose the size appropriate to your environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM