How to update data in SOLR?

2019-08-20 07:25发布

问题:

Hi I have a SOLR collection named AssetLocalUS which contains this data.

[
{
  "assetId": "1-213Z-9335",
  "availability": 1
},
{
   "assetId": "1-213Z-1789",
     "availability": 2
    },
    {
        "assetId": "1-213Z-3452",
        "availability": 3
     }

      ]

I want to update availability based on AssetId '1-213Z-9335' in SOLR.

This is my JAVA code.

 private SolrCrudRepository<Asset, String> assetRepository;

@Autowired
private AssetEntityRepository assetEntityRep;

@Autowired
public AssetRequestServiceImpl(SolrCrudRepository<Asset, String> assetRepository) {
    this.assetRepository = assetRepository;
}


@Override
public ServiceResponse<?> processAssetRequests(Asset asset) {
    System.out.println("Value of availability is==================:" + asset.getAvailability());
    Asset asset2 = null;
    try {
        logger.info("performing maintdb save for asset id: " + asset.getAssetId());
        assetEntityRep.save(asset.getAvailability());
        // TODO: Add Error handling
        logger.info("performing solr save for asset id: " + asset.getAssetId());
        **asset2 = assetRepository.save(asset);**
        // TODO: Add Error handling
        return ServiceResponse.success(asset2);
    } catch (Exception exception) {
        logger.error(String.format("An error occured while processing asset request due to : %s", exception),
                exception);
        return ServiceResponse.error(Asset.class);
    }
}

I am trying update SOLR collection data using SOLRCrudRepository.But everytime i am hitting API to update Collection its not updating data its insering new data into collection.

  {
    "assetId": "1-213Z-9335",
   "availability": 10000

   }

Earlier collection was having 3 records now it will contain 4 records. As it will not update availability based on assetId. It will add new to collection.

What change i need to make to update data in collection based on assetId.

回答1:

If assetId isn't defined as the uniqueKey for the collection, duplicate will be allowed (.. or if you say I WANT THIS TO BE A DUPLICATE). If assetId is the unique key for your document, define it as such and let Solr update the already stored document.

The field should also be defined as a string field (not text) so you don't perform tokenization etc. on it.



标签: solr