Skip to content

Commit

Permalink
adding synonyms
Browse files Browse the repository at this point in the history
  • Loading branch information
costero-e committed Mar 28, 2024
1 parent 7e44629 commit faa4bbb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
9 changes: 5 additions & 4 deletions beacon/db/extract_filtering_terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 31 additions & 3 deletions beacon/db/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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']=[]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions beacon/reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit faa4bbb

Please sign in to comment.