Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for product quantity and packaging weights in search graphs #8963

Merged
merged 9 commits into from
Sep 7, 2023
Merged
47 changes: 31 additions & 16 deletions cgi/search.pl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
}

my @search_fields
= qw(brands categories packaging labels origins manufacturing_places emb_codes purchase_places stores countries ingredients additives allergens traces nutrition_grades nova_groups languages creator editors states);
= qw(brands categories packaging labels origins manufacturing_places emb_codes purchase_places stores countries
ingredients additives allergens traces nutrition_grades nova_groups ecoscore languages creator editors states);

$admin and push @search_fields, "lang";

Expand All @@ -109,6 +110,8 @@
allergens => 1,
traces => 1,
nutrition_grades => 1,
nova_groups => 1,
eco_score => 1,
purchase_places => 1,
stores => 1,
countries => 1,
Expand Down Expand Up @@ -322,16 +325,28 @@
$axis_labels{$nid} = display_taxonomy_tag($lc, "nutrients", "zz:$nid");
$log->debug("nutriments", {nid => $nid, value => $axis_labels{$nid}}) if $log->is_debug();
}
push @axis_values, "additives_n", "ingredients_n", "known_ingredients_n", "unknown_ingredients_n";
push @axis_values, "fruits-vegetables-nuts-estimate-from-ingredients";
push @axis_values, "forest_footprint";
$axis_labels{additives_n} = lang("number_of_additives");
$axis_labels{ingredients_n} = lang("ingredients_n_s");
$axis_labels{known_ingredients_n} = lang("known_ingredients_n_s");
$axis_labels{unknown_ingredients_n} = lang("unknown_ingredients_n_s");

my @other_search_fields = (
"additives_n", "ingredients_n", "known_ingredients_n", "unknown_ingredients_n",
"fruits-vegetables-nuts-estimate-from-ingredients",
"forest_footprint", "product_quantity", "nova_group", 'ecoscore_score',
);

# Add the fields related to packaging
foreach my $material ("all", "en:plastic", "en:glass", "en:metal", "en:paper-or-cardboard", "en:unknown") {
foreach my $subfield ("weight", "weight_100g", "weight_percent") {
push @other_search_fields, "packagings_materials.$material.$subfield";
}
}

$axis_labels{search_nutriment} = lang("search_nutriment");
$axis_labels{products_n} = lang("number_of_products");
$axis_labels{forest_footprint} = lang("forest_footprint");

foreach my $field (@other_search_fields) {
my ($title, $unit, $unit2, $allow_decimals) = get_search_field_title_and_details($field);
push @axis_values, $field;
$axis_labels{$field} = $title;
}

my @sorted_axis_values = ("", sort({lc($axis_labels{$a}) cmp lc($axis_labels{$b})} @axis_values));

Expand Down Expand Up @@ -738,13 +753,13 @@

# We want existing values for axis fields
foreach my $axis ('x', 'y') {
if ( ($graph_ref->{"axis_$axis"} ne "")
and ($graph_ref->{"axis_$axis"} ne "forest_footprint")
and ($graph_ref->{"axis_$axis"} !~ /_n$/))
{
(defined $query_ref->{"nutriments." . $graph_ref->{"axis_$axis"} . "_100g"})
or $query_ref->{"nutriments." . $graph_ref->{"axis_$axis"} . "_100g"} = {};
$query_ref->{"nutriments." . $graph_ref->{"axis_$axis"} . "_100g"}{'$exists'} = 1;

if ($graph_ref->{"axis_$axis"} ne "") {
my $field = $graph_ref->{"axis_$axis"};
# Get the field path components
my @fields = get_search_field_path_components($field);
# Convert to dot notation to get the MongoDB field
$query_ref->{join(".", @fields)} = {'$exists' => 1};
}
}

Expand Down
Loading
Loading