How to update data in SOLR?

你说的曾经没有我的故事 提交于 2019-12-12 04:09:50

问题


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.



来源:https://stackoverflow.com/questions/45753129/how-to-update-data-in-solr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!