0

I have written really hardcoded solution for my problem but it should be ok. I need to edit some values of concrete fields for all docs in my index. I have 55500 docs in my index.

I am trying to edit fields for every document and commit by 500 docs. I think this solution should work (I tried to remove one document by id from my index and it was deleted) but there is no change in my index... there are still fields with bad values (with #0; which I want to remove)

    for (int i = 0; i < 55501; i = i + 500) {
        SolrQuery query = new SolrQuery();    
        query.setParam("start", i+"");
        query.setParam("q", "*:*");
        query.setParam("rows", "500");

        String url = "http://MYINDEX";
        HttpSolrServer solrServer = new HttpSolrServer(url);            

        QueryResponse qResponse;
        try {
            qResponse = solrServer.query(query);
            SolrDocumentList docs=qResponse.getResults();
            for (SolrDocument solrDocument : docs) {
                String parseTime = (String) solrDocument.getFieldValue("parse_time");
                String parseTimeUnix = (String) solrDocument.getFieldValue("parse_time_unix_timestamp");
                parseTime = parseTime.replaceAll("#0;", "");
                parseTimeUnix = parseTimeUnix.replaceAll("#0;", "");                
                solrDocument.setField("parse_time", parseTime);
                solrDocument.setField("parse_time_unix_timestamp", parseTimeUnix);
            }

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

        try {
            solrServer.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }           
    }
Jan Bouchner
  • 875
  • 1
  • 14
  • 38

1 Answers1

1

You need to send the updated documents back to Solr for them to be updated.

Use the Add the command solrServer.add(docs); before you call commit to have send them back. See updated snippet below.

 ...
 try {
     solrServer.add(docs);
     solrServer.commit();
 }
 ...
Paige Cook
  • 22,415
  • 3
  • 57
  • 68
  • Thank you for useful advice. solrServer.add() should be there and there must be SolrInputDocument sid = org.apache.solr.client.solrj.util.ClientUtils.toSolrInputDocument(solrDocument); yet... to convert SolrDocument to SolrInputDocument – Jan Bouchner Jul 18 '13 at 10:17
  • Yes, I forgot that you need to pass a SolrInputDocument for updates and glad that you found the ClientUtils.toSolrInputDocument method. – Paige Cook Jul 18 '13 at 11:54