0

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.

eran
  • 6,731
  • 6
  • 35
  • 52
  • Check this http://stackoverflow.com/questions/12183798/solrj-api-for-partial-document-update it is written for Java, but should help. – cheffe May 22 '16 at 11:45

1 Answers1

0

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.

freedev
  • 25,946
  • 8
  • 108
  • 125