mtitek.com  MTI TEK
 Home |Big Data |Samples |Install |Tutorials |References |Books |Contact

Apache Solr ▸ SolrJ: Update documents (stream.body) using the request handler /update
  1. Notes
  2. Example

  1. Notes
    In order for the code bellow to work:
    Make sure to enable Streaming in "solrconfig.xml":
    <requestParsers enableStreamBody="true" />
    
    Please visit this page for more details: http://lucene.apache.org/solr/guide/7_6/requestdispatcher-in-solrconfig.html

    ▸ Make sure to update the variables ("solrUrl", "collectionName", ...) with your information.

    Make sure to add the fields "id" and "field1" to your SchemaXml.

    To force the commit, make sure to set the property "openSearcher" to true (SolrConfigXml -> autoCommit -> updateHandler)

    Note: You can also force the commit by running the URL: http://localhost:8983/solr/COLLECTION-NAME/update?commit=true
  2. Example
    Here's an example of how to use SolrJ to update a document:
    final String UPDATE_REQUEST_PATH = "/update";
    
    final String[] solrUrl = { "http://localhost:8983/solr" };
    
    final String collectionName = "collection1";
    
    // org.apache.solr.common.params.CommonParams.STREAM_BODY = "stream.body";
    
    // org.apache.solr.common.params.CollectionAdminParams.COLLECTION = "collection";
    
    final CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder(Arrays.asList(solrUrl)).build();
    
    cloudSolrClient.setDefaultCollection(collectionName);
    
    // updating documents [/update] [ModifiableSolrParams] [stream.body]
    {
        final String streamBody = "<add><doc><field name=\"id\">1</field><field name=\"dynamicField1_s\" update=\"set\">dynamicField1_s 1-1</field><field name=\"field1\" update=\"set\">field1 1-1</field></doc></add>";
    
        final ModifiableSolrParams modifiableSolrParams = new ModifiableSolrParams();
    
        modifiableSolrParams.add(CommonParams.STREAM_BODY, streamBody);
        modifiableSolrParams.add(CollectionAdminParams.COLLECTION, collectionName);
    
        final UpdateRequest updateRequest = new UpdateRequest();
    
        updateRequest.setParams(modifiableSolrParams);
        updateRequest.setPath(UPDATE_REQUEST_PATH);
        updateRequest.setMethod(METHOD.POST);
    
        final NamedList<Object> response = cloudSolrClient.request(updateRequest);
    
        System.out.println(response);
    }
    
    cloudSolrClient.close();
    


© mtitek.com