From 2fec90b4a9040cde79774f61e4e19fff30f2a916 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 18 Jun 2024 15:50:30 +0700 Subject: add some field in recommendation solr --- indoteknik_custom/models/solr/apache_solr.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'indoteknik_custom/models/solr') diff --git a/indoteknik_custom/models/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py index 397db53b..7fc9dd5f 100644 --- a/indoteknik_custom/models/solr/apache_solr.py +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -294,23 +294,23 @@ class ApacheSolr(models.Model): return False def _solr_sync_recommendation(self, limit=100): - solr_model = self.env['apache.solr'] variants = self.env['product.product'].search([('solr_flag', '=', 2)], limit=limit) - # documents = [] + counter = 0 for variant in variants: - # document = solr_model.get_doc('recommendation', variant.id) + template_time = time.time() + counter += 1 document = {} document.update({ - 'id': variant.id, - 'display_name_s': variant.display_name, - 'name_s': variant.name, + 'id': variant.id or 0, + 'display_name_s': variant.display_name or '', + 'name_s': variant.name or '', 'default_code_s': variant.default_code or '', - 'product_rating_f': variant.product_tmpl_id.virtual_rating, - 'template_id_i': variant.product_tmpl_id.id, - 'active_s': variant.active, + 'product_rating_f': variant.product_tmpl_id.virtual_rating or 0, + 'template_id_i': variant.product_tmpl_id.id or 0, + 'active_s': str(variant.active) or 'false', + 'type_s': variant.product_tmpl_id.type or '' }) - # self.solr().add(docs=[document], softCommit=True) - # documents.append(document) variant.solr_flag = 1 _recommendation_solr.add(document) - # _recommendation_solr.add(documents) + _logger.info('[SYNC_VARIANTS_TO_SOLR] {}/{} {:.6f}'.format(counter, limit, time.time() - template_time)) + _logger.info('[SYNC_VARIANTS_TO_SOLR] Success add to solr variants %s' % variant.id) -- cgit v1.2.3 From 938ebeb3e785da9d0f40504932ae574d5f8eb27c Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 18 Jun 2024 16:07:00 +0700 Subject: add try catch while sync solr recommendation --- indoteknik_custom/models/solr/apache_solr.py | 35 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'indoteknik_custom/models/solr') diff --git a/indoteknik_custom/models/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py index 7fc9dd5f..2e275698 100644 --- a/indoteknik_custom/models/solr/apache_solr.py +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -295,22 +295,21 @@ class ApacheSolr(models.Model): def _solr_sync_recommendation(self, limit=100): variants = self.env['product.product'].search([('solr_flag', '=', 2)], limit=limit) - counter = 0 for variant in variants: - template_time = time.time() - counter += 1 - document = {} - document.update({ - 'id': variant.id or 0, - 'display_name_s': variant.display_name or '', - 'name_s': variant.name or '', - 'default_code_s': variant.default_code or '', - 'product_rating_f': variant.product_tmpl_id.virtual_rating or 0, - 'template_id_i': variant.product_tmpl_id.id or 0, - 'active_s': str(variant.active) or 'false', - 'type_s': variant.product_tmpl_id.type or '' - }) - variant.solr_flag = 1 - _recommendation_solr.add(document) - _logger.info('[SYNC_VARIANTS_TO_SOLR] {}/{} {:.6f}'.format(counter, limit, time.time() - template_time)) - _logger.info('[SYNC_VARIANTS_TO_SOLR] Success add to solr variants %s' % variant.id) + if variant.product_tmpl_id: # Check if product_tmpl_id exists + try: + document = { + 'id': variant.id or 0, + 'display_name_s': variant.display_name or '', + 'name_s': variant.name or '', + 'default_code_s': variant.default_code or '', + 'product_rating_f': variant.product_tmpl_id.virtual_rating or 0, + 'template_id_i': variant.product_tmpl_id.id or 0, + 'active_s': str(variant.active) or 'false', + 'type_s': variant.product_tmpl_id.type or '' + } + variant.write({'solr_flag': 1}) # Ensure the flag is updated correctly + _recommendation_solr.add(document) + except Exception as e: + _logger.error("Failed to add document to Solr: %s", e) + _logger.debug("Document data: %s", document) -- cgit v1.2.3 From 4349b52b8221276a19104a21b2c2cd057e7ee725 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 18 Jun 2024 16:15:10 +0700 Subject: try to fix error --- indoteknik_custom/models/solr/apache_solr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'indoteknik_custom/models/solr') diff --git a/indoteknik_custom/models/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py index 2e275698..3d6eeeca 100644 --- a/indoteknik_custom/models/solr/apache_solr.py +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -305,11 +305,11 @@ class ApacheSolr(models.Model): 'default_code_s': variant.default_code or '', 'product_rating_f': variant.product_tmpl_id.virtual_rating or 0, 'template_id_i': variant.product_tmpl_id.id or 0, - 'active_s': str(variant.active) or 'false', + 'active': variant.active, 'type_s': variant.product_tmpl_id.type or '' } variant.write({'solr_flag': 1}) # Ensure the flag is updated correctly _recommendation_solr.add(document) except Exception as e: _logger.error("Failed to add document to Solr: %s", e) - _logger.debug("Document data: %s", document) + _logger.error("Document data: %s", document) -- cgit v1.2.3 From acca01df9c8f8ac22551d02d80d93dd00362f993 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 18 Jun 2024 16:17:38 +0700 Subject: bf --- indoteknik_custom/models/solr/apache_solr.py | 1 + 1 file changed, 1 insertion(+) (limited to 'indoteknik_custom/models/solr') diff --git a/indoteknik_custom/models/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py index 3d6eeeca..d25b5fd6 100644 --- a/indoteknik_custom/models/solr/apache_solr.py +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -295,6 +295,7 @@ class ApacheSolr(models.Model): def _solr_sync_recommendation(self, limit=100): variants = self.env['product.product'].search([('solr_flag', '=', 2)], limit=limit) + document = {} for variant in variants: if variant.product_tmpl_id: # Check if product_tmpl_id exists try: -- cgit v1.2.3 From 2ca257f21bd5087604e3d0110eff16e40b0d8f83 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 18 Jun 2024 16:27:52 +0700 Subject: bf --- indoteknik_custom/models/solr/apache_solr.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'indoteknik_custom/models/solr') diff --git a/indoteknik_custom/models/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py index d25b5fd6..0b1bd821 100644 --- a/indoteknik_custom/models/solr/apache_solr.py +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -295,6 +295,7 @@ class ApacheSolr(models.Model): def _solr_sync_recommendation(self, limit=100): variants = self.env['product.product'].search([('solr_flag', '=', 2)], limit=limit) + documents = [] document = {} for variant in variants: if variant.product_tmpl_id: # Check if product_tmpl_id exists @@ -310,7 +311,9 @@ class ApacheSolr(models.Model): 'type_s': variant.product_tmpl_id.type or '' } variant.write({'solr_flag': 1}) # Ensure the flag is updated correctly - _recommendation_solr.add(document) + documents.append(document) except Exception as e: _logger.error("Failed to add document to Solr: %s", e) _logger.error("Document data: %s", document) + _recommendation_solr.add(documents) + return True -- cgit v1.2.3 From 4b75a370a2b16d21eeb3c71007885eae4dd4112c Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 18 Jun 2024 16:47:30 +0700 Subject: bf --- indoteknik_custom/models/solr/apache_solr.py | 36 ++++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'indoteknik_custom/models/solr') diff --git a/indoteknik_custom/models/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py index 0b1bd821..1c4b7114 100644 --- a/indoteknik_custom/models/solr/apache_solr.py +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -296,24 +296,24 @@ class ApacheSolr(models.Model): def _solr_sync_recommendation(self, limit=100): variants = self.env['product.product'].search([('solr_flag', '=', 2)], limit=limit) documents = [] - document = {} + catch = {} for variant in variants: - if variant.product_tmpl_id: # Check if product_tmpl_id exists - try: - document = { - 'id': variant.id or 0, - 'display_name_s': variant.display_name or '', - 'name_s': variant.name or '', - 'default_code_s': variant.default_code or '', - 'product_rating_f': variant.product_tmpl_id.virtual_rating or 0, - 'template_id_i': variant.product_tmpl_id.id or 0, - 'active': variant.active, - 'type_s': variant.product_tmpl_id.type or '' - } - variant.write({'solr_flag': 1}) # Ensure the flag is updated correctly - documents.append(document) - except Exception as e: - _logger.error("Failed to add document to Solr: %s", e) - _logger.error("Document data: %s", document) + try: + document = { + 'id': variant.id or 0, + 'display_name_s': variant.display_name or '', + 'name_s': variant.name or '', + 'default_code_s': variant.default_code or '', + 'product_rating_f': variant.product_tmpl_id.virtual_rating or 0, + 'template_id_i': variant.product_tmpl_id.id or 0, + 'active': variant.active or False, + 'type_s': variant.product_tmpl_id.type or '', + } + # variant.write({'solr_flag': 1}) # Ensure the flag is updated correctly + documents.append(document) + catch = document + except Exception as e: + _logger.error("Failed to add document to Solr: %s", e) + _logger.error("Document data: %s", catch) _recommendation_solr.add(documents) return True -- cgit v1.2.3 From 20ae84eb89409a75b6ba024a9d9e06bdc593a480 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 18 Jun 2024 16:51:40 +0700 Subject: bf --- indoteknik_custom/models/solr/apache_solr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'indoteknik_custom/models/solr') diff --git a/indoteknik_custom/models/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py index 1c4b7114..6560c9b5 100644 --- a/indoteknik_custom/models/solr/apache_solr.py +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -306,10 +306,10 @@ class ApacheSolr(models.Model): 'default_code_s': variant.default_code or '', 'product_rating_f': variant.product_tmpl_id.virtual_rating or 0, 'template_id_i': variant.product_tmpl_id.id or 0, - 'active': variant.active or False, + 'active_s': str(variant.active) or 'false', 'type_s': variant.product_tmpl_id.type or '', } - # variant.write({'solr_flag': 1}) # Ensure the flag is updated correctly + variant.write({'solr_flag': 1}) # Ensure the flag is updated correctly documents.append(document) catch = document except Exception as e: -- cgit v1.2.3