diff --git a/beacon/db/extract_filtering_terms.py b/beacon/db/extract_filtering_terms.py index a2eb0c1b..16d2b277 100644 --- a/beacon/db/extract_filtering_terms.py +++ b/beacon/db/extract_filtering_terms.py @@ -200,10 +200,11 @@ def insert_all_ontology_terms_used(): collections.remove('filtering_terms') print("Collections:", collections) for c_name in collections: - terms_ids = find_ontology_terms_used(c_name) - terms = get_filtering_object(terms_ids, c_name) - if len(terms) > 0: - client.beacon.filtering_terms.insert_many(terms) + if c_name not in ['counts', 'similarities', 'synonyms']: + terms_ids = find_ontology_terms_used(c_name) + terms = get_filtering_object(terms_ids, c_name) + if len(terms) > 0: + client.beacon.filtering_terms.insert_many(terms) def find_ontology_terms_used(collection_name: str) -> List[Dict]: print(collection_name) diff --git a/beacon/db/filters.py b/beacon/db/filters.py index 9f625831..bf8c44c6 100644 --- a/beacon/db/filters.py +++ b/beacon/db/filters.py @@ -388,7 +388,27 @@ def apply_filters(query: dict, filters: List[dict], collection: str, query_param def apply_ontology_filter(query: dict, filter: OntologyFilter, collection: str, request_parameters: dict) -> dict: - scope = filter.scope + query_synonyms={} + query_synonyms['id']=filter.id + synonyms=get_documents( + client.beacon.synonyms, + query_synonyms, + 0, + 1 + ) + + try: + synonym_id=synonyms[0]['synonym'] + except Exception: + synonym_id=None + LOG.debug(synonym_id) + if synonym_id is not None: + filter.id=synonym_id + + try: + scope = filter.scope + except Exception: + scope = collection[0:-1] is_filter_id_required = True # Search similar if filter.similarity != Similarity.EXACT: @@ -407,6 +427,8 @@ def apply_ontology_filter(query: dict, filter: OntologyFilter, collection: str, ontology_dict=client.beacon.similarities.find({"id": filter.id}) final_term_list = ontology_dict[0]["similarity_low"] + + final_term_list.append(filter.id) query_filtering={} query_filtering['$and']=[] @@ -609,7 +631,10 @@ def format_operator(operator: Operator) -> str: def apply_alphanumeric_filter(query: dict, filter: AlphanumericFilter, collection: str) -> dict: #LOG.debug(filter.value) - scope = filter.scope + try: + scope = filter.scope + except Exception: + scope = collection[0:-1] formatted_value = format_value(filter.value) formatted_operator = format_operator(filter.operator) #LOG.debug(collection) @@ -768,7 +793,10 @@ def apply_alphanumeric_filter(query: dict, filter: AlphanumericFilter, collectio def apply_custom_filter(query: dict, filter: CustomFilter, collection:str) -> dict: #LOG.debug(query) - scope=filter.scope + try: + scope = filter.scope + except Exception: + scope = collection[0:-1] value_splitted = filter.id.split(':') if value_splitted[0] in conf.alphanumeric_terms: query_term = value_splitted[0] diff --git a/beacon/reindex.py b/beacon/reindex.py index 9437fc6f..5e335813 100644 --- a/beacon/reindex.py +++ b/beacon/reindex.py @@ -12,6 +12,14 @@ conf.database_auth_source, ) ) +try: + client.beacon.drop_collection("synonyms") +except Exception: + client.beacon.create_collection(name="synonyms") +try: + client.beacon.validate_collection("synonyms") +except Exception: + db=client.beacon.create_collection(name="synonyms") try: client.beacon.drop_collection("counts") except Exception: