Skip to content

Commit

Permalink
feat: add fully indexed nutriments field
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Sep 25, 2023
1 parent dfa952a commit 946e4e1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ class FieldType(StrEnum):
double = auto()
float = auto()
integer = auto()
bool = auto()
text = auto()
text_lang = auto()
taxonomy = auto()
# if the field is not enabled (=not indexed and not parsed), see:
# https://www.elastic.co/guide/en/elasticsearch/reference/current/enabled.html
disabled = auto()
object = auto()

def is_numeric(self):
return self in (FieldType.integer, FieldType.float, FieldType.double)
Expand Down Expand Up @@ -92,7 +94,7 @@ def multi_should_be_used_for_selected_type_only(self):
with specific types."""
if (
not (
self.type in (FieldType.keyword, FieldType.text)
self.type in (FieldType.keyword, FieldType.text, FieldType.bool)
or self.type.is_numeric()
)
and self.multi
Expand Down Expand Up @@ -231,6 +233,7 @@ def get_supported_langs(self) -> set[str]:
),
fields=[
FieldConfig(name="code", type=FieldType.keyword, required=True),
FieldConfig(name="obsolete", type=FieldType.bool, required=True),
FieldConfig(
name="product_name", type=FieldType.text_lang, include_multi_match=True
),
Expand Down Expand Up @@ -270,6 +273,7 @@ def get_supported_langs(self) -> set[str]:
FieldConfig(name="countries_tags", type=FieldType.keyword, multi=True),
FieldConfig(name="states_tags", type=FieldType.keyword, multi=True),
FieldConfig(name="origins_tags", type=FieldType.keyword, multi=True),
FieldConfig(name="ingredients_tags", type=FieldType.keyword, multi=True),
FieldConfig(name="unique_scans_n", type=FieldType.integer),
FieldConfig(name="scans_n", type=FieldType.integer),
FieldConfig(name="nutrition_grades", type=FieldType.keyword),
Expand All @@ -290,7 +294,7 @@ def get_supported_langs(self) -> set[str]:
FieldConfig(name="ingredients_n", type=FieldType.integer),
FieldConfig(name="nova_group", type=FieldType.integer),
FieldConfig(name="nutrient_levels", type=FieldType.disabled),
FieldConfig(name="nutriments", type=FieldType.disabled),
FieldConfig(name="nutriments", type=FieldType.object),
FieldConfig(name="nutriscore_data", type=FieldType.disabled),
FieldConfig(name="nutriscore_grade", type=FieldType.keyword),
FieldConfig(name="traces_tags", type=FieldType.keyword, multi=True),
Expand Down
5 changes: 5 additions & 0 deletions app/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Iterable

from elasticsearch_dsl import (
Boolean,
Date,
Double,
Float,
Expand All @@ -30,6 +31,8 @@ def generate_dsl_field(field: FieldConfig, supported_langs: Iterable[str]):
for lang in supported_langs
}
return Object(required=field.required, dynamic=False, properties=properties)
elif field.type == FieldType.object:
return Object(required=field.required, dynamic=True)
elif field.type == FieldType.keyword:
return Keyword(required=field.required, multi=field.multi)
elif field.type == FieldType.text:
Expand All @@ -40,6 +43,8 @@ def generate_dsl_field(field: FieldConfig, supported_langs: Iterable[str]):
return Double(required=field.required, multi=field.multi)
elif field.type == FieldType.integer:
return Integer(required=field.required, multi=field.multi)
elif field.type == FieldType.bool:
return Boolean(required=field.required, multi=field.multi)
elif field.type == FieldType.date:
return Date(required=field.required, multi=field.multi)
elif field.type == FieldType.disabled:
Expand Down

0 comments on commit 946e4e1

Please sign in to comment.