Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
frankbaele committed Nov 8, 2023
1 parent f9f8e41 commit 585c2e7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/cli/perform_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def gen_taxonomies(
"taxonomy_name": taxonomy_source_config.name.replace(" ", "_")
}
for key, values in node.synonyms.items():
taxonomy_dict["labels_" + key] = values
taxonomy_dict["names_" + key] = values
document_dict = get_document_dict(processor, taxonomy_dict, next_index)
if not document_dict:
continue
Expand Down
15 changes: 0 additions & 15 deletions app/openfoodfacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,3 @@ def build_image_fields(product: JSONType):
return fields


class CompletionProcessor(BaseResultProcessor):
def process(self, response: Response, projection: set[str] | None) -> JSONType:
output = {
"took": response.took,
"timed_out": response.timed_out,
"count": response.hits.total["value"]
}
options = []
for field in response.suggest:
suggestion = response.suggest[field][0]
for option in suggestion.options:
result = {"text": option.text, "code": option._source["code"]}
options.append(result)
output["options"] = options
return output
19 changes: 18 additions & 1 deletion app/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,21 @@ def load_result_processor(config: Config) -> BaseResultProcessor | None:
if config.result_processor
else BaseResultProcessor
)
return result_processor_cls(config)
return result_processor_cls(config)


class CompletionProcessor(BaseResultProcessor):
def process(self, response: Response, projection: set[str] | None) -> JSONType:
output = {
"took": response.took,
"timed_out": response.timed_out,
"count": response.hits.total["value"]
}
options = []
for field in response.suggest:
suggestion = response.suggest[field][0]
for option in suggestion.options:
result = {"text": option.text, "code": option._source["code"]}
options.append(result)
output["options"] = options
return output
5 changes: 3 additions & 2 deletions app/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,17 @@ def build_completion_query(
config: Config
):
"""Build an elasticsearch_dsl Query.
:param q: the user raw query
:param taxonomy_name: the taxonomy we want to search in
:param lang: the languages we want to support
:param lang: the language we want search in
:param size: number of results to return
:param config: configuration to use
:return: the built Query
"""

query = Search(index=config.index.name)
query = query.suggest('labels', q, completion={'field': f"labels.{lang}", 'size':size})
query = query.suggest('names', q, completion={'field': f"names.{lang}", 'size':size})
query = query.query('bool', filter=[Q('term', taxonomy_name=taxonomy_name)])
return query

Expand Down
4 changes: 2 additions & 2 deletions data/config/taxonomy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ fields:
type: date
taxonomy_name:
type: text
labels:
names:
full_text_search: true
type: text_lang_completion
lang_separator: _
match_phrase_boost: 2.0
preprocessor: app.openfoodfacts.DocumentPreprocessor
result_processor: app.openfoodfacts.CompletionProcessor
result_processor: app.postprocessing.CompletionProcessor
split_separator: ','
taxonomy:
sources:
Expand Down

0 comments on commit 585c2e7

Please sign in to comment.