cancel
Showing results for 
Search instead for 
Did you mean: 

How to trigger Solr Update Indexing programmatically

varsha18
Explorer
0 Kudos

My requirement is to trigger solr Update indexing via code . I have a cronJob which will delete few products based on certain condition , afterwards I need to remove those products from Solr as well . Please help me how can I pass only those products in the update query and then execute Solr partial update indexing process.

Accepted Solutions (0)

Answers (1)

Answers (1)

andyfletcher
Active Contributor
0 Kudos

You should be able to do this with the `SolrIndexerHotUpdateJob`

Something like this?

List<ProductModel> productsToIndex = Arrays.asList(product1, product2, product3);
SolrIndexerHotUpdateCronJobModel cronJob = (SolrIndexerHotUpdateCronJobModel) cronJobService.getCronJob("my-preconfigured-indexer-cronjob");
cronJob.setItems(productsToIndex); modelService.save(cronJob); cronJobService.performCronJob(cronJob);
varsha18
Explorer
0 Kudos

Thanks , for your answer . When a condition is satisfied , we are marking product status as checked and want to remove those checked products from SOLR , when trying to run HotUpdateIndex from Backoffice using both "update" and "delete" option , even then those products are not removing from SOlR. Similarly , this code also did not remove the products from SOLR. Is there any other way? Please suggest

andyfletcher
Active Contributor
0 Kudos

You need to set the `indexerOperation` attribute of the cronjob to DELETE if you want to remove. It defaults to UPDATE.

Make sure you're adding the product from the correct catalogversion(s) to be removed.

I've just tested using this Groovy against the regular electronics catalog and it removed product in the staged catalog from the Solr index. Most of it is for creating the cronjob if it doesn't already exist and you can ignore the lines about the transactions, that's just so the cronjob gets saved before running it from the hac. It's up to you to convert to Java code for what you're trying to do.

de.hybris.platform.tx.Transaction.current().commit()

cv = catalogVersionService.getCatalogVersion('electronicsProductCatalog', 'Staged')

// create or find existing indexer job
search = flexibleSearchService.search(/select {pk} from {SolrIndexerHotUpdateCronJob} where {code}=?code/, [code: 'hotUpdateTest'])
if (search.count==0) {
  println 'creating new'
  cj = modelService.create("SolrIndexerHotUpdateCronJob")
  cj.code = 'hotUpdateTest'
  cj.facetSearchConfig = cv.facetSearchConfigs[0]
  cj.indexTypeName = 'Product'
  cj.job = cronJobService.getJob('solrIndexerHotUpdateJob')
  modelService.save cj
} else {
  cj = search.result[0]
}

// set product to delete and operation
cj.items = [productService.getProductForCode(cv, '1934794')]
cj.indexerOperation = de.hybris.platform.solrfacetsearch.enums.IndexerOperationValues.DELETE
modelService.save cj

// run the job
cronJobService.performCronJob(cj)

de.hybris.platform.tx.Transaction.current().begin()