Skip to content

feat: Add key suggestions endpoint for barcode and category tags#320

Draft
teolemon with Copilot wants to merge 3 commits into
mainfrom
copilot/provide-key-suggestions-for-barcode
Draft

feat: Add key suggestions endpoint for barcode and category tags#320
teolemon with Copilot wants to merge 3 commits into
mainfrom
copilot/provide-key-suggestions-for-barcode

Conversation

Copilot AI commented Oct 23, 2025

Copy link
Copy Markdown

Overview

This PR implements a new endpoint that provides intelligent key suggestions to help users add properties to products in the Open Food Facts Folksonomy Engine. When users are adding key-value pairs to a product, they can now get suggestions based on the most commonly used keys in the database.

What's New

Endpoint: GET /keys/suggestions

The new endpoint accepts the following parameters:

  • barcode (optional): Product barcode to get suggestions for
  • category (optional): Category tag (e.g., en:lasagne) for future category-based filtering
  • owner (optional): Filter by owner (empty for public tags, or user_id for private)
  • limit (optional): Number of suggestions to return (default: 10, max: 100)

Note: At least one of barcode or category must be provided.

Smart Suggestions

The endpoint returns the most commonly used keys from the folksonomy database, intelligently ordered by frequency. When a barcode is provided, it excludes keys that are already present on that product, ensuring suggestions are always relevant and non-redundant.

Example Response:

[
  {
    "k": "color",
    "count": 15
  },
  {
    "k": "size",
    "count": 12
  },
  {
    "k": "material",
    "count": 8
  }
]

Example Usage

# Get suggestions for a specific product (excludes keys already on that product)
curl "https://api.folksonomy.openfoodfacts.org/keys/suggestions?barcode=3701027900001"

# Get suggestions for a category (for future use)
curl "https://api.folksonomy.openfoodfacts.org/keys/suggestions?category=en:lasagne&limit=5"

# Get more suggestions
curl "https://api.folksonomy.openfoodfacts.org/keys/suggestions?barcode=123456&limit=20"

Implementation Details

  • New Model: Added KeySuggestion model with k (key name) and count (usage frequency)
  • Database Query: Efficiently aggregates key usage counts using PostgreSQL's JSON aggregation
  • Smart Filtering: When a barcode is provided, uses a subquery to exclude keys already present on that product
  • OpenAPI Documentation: Fully documented in the API's OpenAPI specification

Testing

Added comprehensive test coverage:

  • ✅ Validates error when no parameters are provided
  • ✅ Tests barcode parameter with proper key exclusion
  • ✅ Tests category parameter
  • ✅ Tests custom limit parameter
  • ✅ Tests empty database edge case

All 41 tests pass (36 existing + 5 new).

Future Enhancements

The implementation includes comments documenting potential future improvements:

  1. Fetch category for a barcode from Open Food Facts API
  2. Find products with the same category using Open Food Facts or Open Prices API
  3. Return keys most commonly used for products in that specific category

This provides a solid foundation that can be extended when external API integration is needed.

Related Issue

Closes #[issue_number] - Provide 10 suggestions of keys for a given Barcode or category tag

Original prompt

This section details on the original issue you should resolve

<issue_title>Provide 10 suggestions of keys for a given Barcode or category tag (eg. en:lasagne)</issue_title>
<issue_description>### Story

  • Provide 10 suggestions of keys for a given category tag (eg. en:lasagne) so that users have inspirations of key-value Paris to add
  • use as needed the folksonomy engine and the open prices and or open food facts api (which have the list of barcodes and an associates category)

</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #318


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 23, 2025 15:36
Co-authored-by: teolemon <1689815+teolemon@users.noreply.github.com>
Co-authored-by: teolemon <1689815+teolemon@users.noreply.github.com>
Copilot AI changed the title [WIP] Provide 10 key suggestions for category tags Add key suggestions endpoint for barcode and category tags Oct 23, 2025
Copilot AI requested a review from teolemon October 23, 2025 15:47
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.30%. Comparing base (b9cda65) to head (e263bc2).
⚠️ Report is 130 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #320       +/-   ##
===========================================
- Coverage   95.06%   79.30%   -15.76%     
===========================================
  Files           5        4        -1     
  Lines         324      575      +251     
===========================================
+ Hits          308      456      +148     
- Misses         16      119      +103     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@teolemon teolemon moved this from Todo to In Progress in 🏷️ Folksonomy Engine Feb 13, 2026
@teolemon teolemon changed the title Add key suggestions endpoint for barcode and category tags feat: Add key suggestions endpoint for barcode and category tags Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Provide 10 suggestions of keys for a given Barcode or category tag (eg. en:lasagne)

4 participants