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

11211 - Allow use of graph slugs for ES reindex by type #11212

Draft
wants to merge 8 commits into
base: dev/8.0.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions arches/app/utils/index_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def index_resources_by_type(
Indexes all resources of a given type(s)

Arguments:
resource_types -- array of graph ids that represent resource types
resource_types -- array of graph ids or graph slugs that represent resource types

Keyword Arguments:
clear_index -- set to True to remove all the resources of the types passed in from the index before the reindexing operation
Expand All @@ -327,31 +327,40 @@ def index_resources_by_type(

for resource_type in resource_types:
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
start = datetime.now()
try:
graph = models.GraphModel.objects.get(
graphid=str(uuid.UUID(str(resource_type)))
)
if graph is None:
raise ValueError(f"Graph ID does not exist: {resource_type}")
except ValueError:
try:
graph = models.GraphModel.objects.get(slug=str(resource_type))
except:
logger.warning(
f"Unable to resolve resource type {resource_type}. Please confirm it is a valid graph ID or slug."
)
continue

graph_name = models.GraphModel.objects.get(graphid=str(resource_type)).name
logger.info("Indexing resource type '{0}'".format(graph_name))
logger.info("Indexing resource type '{0}'".format(graph.name))

if clear_index:
tq = Query(se=se)
cards = models.CardModel.objects.filter(
graph_id=str(resource_type)
).select_related("nodegroup")
for nodegroup in [card.nodegroup for card in cards]:
term = Term(field="nodegroupid", term=str(nodegroup.nodegroupid))
tq.add_query(term)
for card in graph.cardmodel_set.all():
tq.add_query(Term(field="nodegroupid", term=str(card.nodegroup_id)))
tq.delete(index=TERMS_INDEX, refresh=True)

rq = Query(se=se)
term = Term(field="graph_id", term=str(resource_type))
term = Term(field="graph_id", term=str(graph.graphid))
rq.add_query(term)
rq.delete(index=RESOURCES_INDEX, refresh=True)

if use_multiprocessing:
resources = [
str(rid)
for rid in Resource.objects.filter(
graph_id=str(resource_type)
).values_list("resourceinstanceid", flat=True)
for rid in Resource.objects.filter(graph_id=graph.graphid).values_list(
"resourceinstanceid", flat=True
)
]
index_resources_using_multiprocessing(
resourceids=resources,
Expand All @@ -366,17 +375,17 @@ def index_resources_by_type(
SearchEngineInstance as _se,
)

resources = Resource.objects.filter(graph_id=str(resource_type))
resources = Resource.objects.filter(graph_id=str(graph.graphid))
index_resources_using_singleprocessing(
resources=resources,
batch_size=batch_size,
quiet=quiet,
title=graph_name,
title=graph.name,
recalculate_descriptors=recalculate_descriptors,
)

q = Query(se=se)
term = Term(field="graph_id", term=str(resource_type))
term = Term(field="graph_id", term=str(graph.graphid))
q.add_query(term)
result_summary = {
"database": len(resources),
Expand All @@ -390,7 +399,7 @@ def index_resources_by_type(
logger.info(
"Status: {0}, Resource Type: {1}, In Database: {2}, Indexed: {3}, Took: {4} seconds".format(
status,
graph_name,
graph.graphid,
result_summary["database"],
result_summary["indexed"],
(datetime.now() - start).seconds,
Expand Down
2 changes: 1 addition & 1 deletion arches/management/commands/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def add_arguments(self, parser):
action="store",
dest="resource_types",
default="",
help="UUID of resource_model to index resources of.",
help="Comma separated list of resource model UUIDs or slugs to index resources of.",
)

parser.add_argument(
Expand Down
1 change: 1 addition & 0 deletions releases/8.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Arches 8.0.0 Release Notes
For more, see (link to forthcoming blog post or documentation? Or arches-lingo?)

### Additional highlights
- 11211 Allow use of graph slug in ES reindex resources management command

### Performance improvements

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/resource_graphs/Resource Test Model.json
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@
"ontologyclass": null,
"sortorder": 0
},
"slug": null,
"slug": "resource_test_model",
"subtitle": "",
"template_id": "50000000-0000-0000-0000-000000000001",
"version": ""
Expand Down
5 changes: 5 additions & 0 deletions tests/models/resource_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def setUpClass(cls):
resource_graph_importer(archesfile["graph"])

cls.search_model_graphid = uuid.UUID("c9b37a14-17b3-11eb-a708-acde48001122")
cls.search_model_slug = "resource_test_model"
cls.search_model_cultural_period_nodeid = "c9b3882e-17b3-11eb-a708-acde48001122"
cls.search_model_creation_date_nodeid = "c9b38568-17b3-11eb-a708-acde48001122"
cls.search_model_destruction_date_nodeid = (
Expand Down Expand Up @@ -288,6 +289,10 @@ def test_reindex_by_resource_type(self):
[self.search_model_graphid], clear_index=True, batch_size=4000
)

result = index_resources_by_type(
[self.search_model_slug], clear_index=True, batch_size=4000
)

self.assertEqual(result, "Passed")

def test_publication_restored_on_save(self):
Expand Down
Loading