Skip to content

Commit

Permalink
Merge pull request #69 from django-oscar/index-all-records
Browse files Browse the repository at this point in the history
[FEAT] Index all records of products and categories
  • Loading branch information
viggo-devries authored Oct 24, 2024
2 parents 4175c8f + 88e516f commit 0e06fe9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class Command(BaseCommand):
def handle(self, *args, **options):
categories = Category.objects.browsable()
categories = Category.objects.all()

with CategoryElasticsearchIndex().reindex() as index:
for chunk in chunked(categories, settings.INDEXING_CHUNK_SIZE):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def handle(self, *args, **options):
if options["debug"]:
return self.handle_debug()

products = Product.objects.browsable()
products = Product.objects.all()
products_total = products.count()

with ProductElasticsearchIndex().reindex() as index:
Expand All @@ -39,7 +39,7 @@ def handle_debug(self):
This is useful when debugging the performance of the indexing process.
"""
overall_start_time = time.time()
products = Product.objects.browsable()
products = Product.objects.all()
products_total = products.count()
total_chunks = products_total / settings.INDEXING_CHUNK_SIZE
processed_chunks = 0
Expand Down
23 changes: 21 additions & 2 deletions oscar_elasticsearch/search/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,31 @@ def create_parent_child_products():
create_parent_child_products()
self.assertEqual(Product.objects.count(), 64) # 4 inside the fixtures

with self.assertNumQueries(19):
with self.assertNumQueries(23):
call_command("update_index_products")

# create 10 extra product with each 5 childs
create_parent_child_products()

# The amount of queries should not change.
with self.assertNumQueries(19):
with self.assertNumQueries(23):
call_command("update_index_products")


class TestBrowsableItems(TestCase):

def test_child_products_hidden_in_category_view(self):
for _ in range(2):
parent = ProductFactory(structure="parent", stockrecords=[])
for _ in range(5):
ProductFactory(structure="child", parent=parent, categories=[])

call_command("update_index_products")
sleep(3)

url = reverse("catalogue:index")
response = self.client.get(url)
products = response.context_data["page_obj"].object_list

self.assertEqual(len(products), 2)
self.assertFalse(any([product.structure == "child" for product in products]))
5 changes: 4 additions & 1 deletion oscar_elasticsearch/search/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def get_scoring_functions(self):
return self.scoring_functions if self.scoring_functions else None

def get_default_filters(self):
filters = [{"term": {"is_public": True}}]
filters = [
{"term": {"is_public": True}},
{"terms": {"structure": ["parent", "standalone"]}},
]

if settings.FILTER_AVAILABLE:
filters.append({"term": {"is_available": True}})
Expand Down

0 comments on commit 0e06fe9

Please sign in to comment.