-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moderation:added percolator and refactor
- Loading branch information
1 parent
1e7a087
commit e4a30b7
Showing
5 changed files
with
111 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# -*- coding: utf-8 -*- | ||
Check failure on line 1 in site/zenodo_rdm/moderation/percolator.py GitHub Actions / Python (site, 3.9, postgresql14, opensearch2)
|
||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2017-2024 CERN. | ||
# Copyright (C) 2022 Graz University of Technology. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Percolator.""" | ||
|
||
|
||
from flask import current_app | ||
from invenio_search import current_search_client | ||
from invenio_search.utils import build_alias_name, build_index_name | ||
|
||
|
||
def create_percolator_index(index_name, record): | ||
"""Create mappings with the percolator field for moderation queries. | ||
This function creates a new Elasticsearch index for percolator queries by copying | ||
the settings and mappings from an existing record index and adding specific | ||
percolator mappings. | ||
""" | ||
# Retrieve the alias name for the record index | ||
record_index = build_alias_name(record.index._name) | ||
|
||
# Build the name for the new percolator index, using a prefix and the record's index name | ||
percolator_index = build_index_name( | ||
index_name, suffix=record.index._name, app=current_app | ||
) | ||
|
||
# Get the current mapping for the record index to copy its structure | ||
record_mapping = current_search_client.indices.get_mapping(index=record_index) | ||
# Extract the mappings from the record index and store in `percolator_mappings` | ||
percolator_mappings = list(record_mapping.values())[0]["mappings"] | ||
|
||
# Add specific properties for percolator fields from the app configuration | ||
percolator_mappings["properties"].update( | ||
current_app.config.get("MODERATION_PERCOLATOR_MAPPING")["properties"] | ||
) | ||
|
||
# Retrieve the current settings of the record index to copy them to the percolator index | ||
record_settings = list( | ||
current_search_client.indices.get_settings(index=record_index).values() | ||
)[0]["settings"]["index"] | ||
|
||
percolator_settings = { | ||
"index": { | ||
"query": { | ||
"default_field": record_settings.get("query", {}).get( | ||
"default_field", [] | ||
) | ||
} | ||
}, | ||
"analysis": record_settings.get("analysis", {}), | ||
} | ||
|
||
if not current_search_client.indices.exists(percolator_index): | ||
try: | ||
current_search_client.indices.create( | ||
index=percolator_index, | ||
body={ | ||
"settings": percolator_settings, | ||
"mappings": {**percolator_mappings}, | ||
}, | ||
) | ||
current_app.logger.info(f"Index {percolator_index} created successfully.") | ||
except Exception as e: | ||
current_app.logger.exception(e) | ||
|
||
|
||
|
||
|
||
|
||
def add_percolate_query(query_string, active=True, score=1.0, notes=None): | ||
"""Adds a percolate query to the moderation-queries index.""" | ||
try: | ||
current_search_client.index( | ||
index="moderation-queries", | ||
body={ | ||
"query": {"query_string": {"query": query_string}}, | ||
"active": active, | ||
"score": score, | ||
"notes": notes, | ||
}, | ||
) | ||
except Exception as e: | ||
current_app.logger.exception(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters