Skip to content

09. Search Functionality

Search Functionality

Searching is a very common feature with almost all the applications. For example, Amazon, Netflix, and many more. Even the browser we use has search functionality. In this step we will add the search functionality to our application. In large scale application search involves a lot of complex logic. Like making request to multiple servers, filtering the results, etc. We will implement search on the dashboard of each type of user and the search functionality will only help to search the products.

Backend Implementation


py filename=main.py
...
@app.route('/search/for', methods=['POST'])
def search():
    # Get the search query parameters from the request
    data = request.get_json()
    query = data.get('query')

    # Search for products and categories based on the query
    products = Product.query.filter(or_(
        Product.name.ilike(f'%{query}%'),
        Product.rpu.ilike(f'%{query}%'),
        Product.manufacture.ilike(f'%{query}%'),
        Product.description.ilike(f'%{query}%'),
        Product.expiry.ilike(f'%{query}%'),
        Product.category.has(Category.name.ilike(f'%{query}%'))
    )).all()
    product_list = []
    categories = []
    for new_product in products:
        prod_data = {
            'id': new_product.id,
            'quantity': new_product.quantity,
            'name': new_product.name,
            'manufacture': new_product.manufacture,
            'expiry': new_product.expiry,
            'description': new_product.description,
            'rpu': new_product.rpu,
            'unit': new_product.unit,
            # Assuming image is stored as a base64-encoded string
            'image': base64.b64encode(new_product.image).decode('utf-8')
        }
        if new_product.category not in categories:
            categories.append(new_product.category)
        product_list.append(prod_data)
    categories_list = []
    for category in categories:
        cat = {
            'id': category.id,
            'name': category.name,
        }
        categories_list.append(cat)
    return jsonify({"cat": categories_list, 'pro': product_list}), 200class SearchResource(Resource):
    def post(self):
        data = request.get_json()
        query = data.get('query')

        # Search for products and categories based on the query
        products = Product.query.filter(or_(
            Product.name.ilike(f'%{query}%'),
            Product.rpu.ilike(f'%{query}%'),
            Product.manufacture.ilike(f'%{query}%'),
            Product.description.ilike(f'%{query}%'),
            Product.expiry.ilike(f'%{query}%'),
            Product.category.has(Category.name.ilike(f'%{query}%'))
        )).all()
        product_list = []
        categories = []
        for new_product in products:
            prod_data = {
                'id': new_product.id,
                'quantity': new_product.quantity,
                'name': new_product.name,
                'manufacture': new_product.manufacture.strftime("%Y-%m-%d"),
                'expiry': new_product.expiry.strftime("%Y-%m-%d"),
                'description': new_product.description,
                'rpu': new_product.rpu,
                'unit': new_product.unit,
                # Assuming image is stored as a base64-encoded string
                'image': base64.b64encode(new_product.image).decode('utf-8')
            }
            if new_product.category not in categories:
                categories.append(new_product.category)
            product_list.append(prod_data)
        categories_list = []
        for category in categories:
            cat = {
                'id': category.id,
                'name': category.name,
            }
            categories_list.append(cat)
        return {"cat": categories_list, 'pro': product_list}, 200

Note We are using or_ function from SQLAlchemy to combine multiple queries. ilike function is used to perform case-insensitive search. It will match based on each character of the query string, so if the query string is "apple", it will match with "app", "ap", "al", etc.

Filter by Category

We will also add filter by category feature on the category sidebar of each type of users` dashboard.

This search by category will filter the products based on the selected category.

Testing it out

Now we can start the app and test all the feature implemented on this page.

Run the app.



python3 app.py

Commit the changes to the local git.



git add .
git commit -m "added admin features"

Continue to start creating Chirps...