Skip to content

Commit

Permalink
Merge pull request #354 from helxplatform/Fixed_program_search
Browse files Browse the repository at this point in the history
Fix program name search
  • Loading branch information
YaphetKG authored Apr 26, 2024
2 parents 8e5f900 + c0d6098 commit 7b208ef
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
68 changes: 55 additions & 13 deletions src/dug/core/async_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ async def search_program(self, program_name=None, offset=0, size=None):
"""
Search for studies by unique_id (ID or name) and/or study_name.
"""
# Initialize the query_body with the outer structure
query_body = {
"query": {
"bool": {
Expand All @@ -476,17 +476,25 @@ async def search_program(self, program_name=None, offset=0, size=None):
"aggs": {
"unique_collection_ids": {
"terms": {
"field": "collection_id.keyword"
"field": "collection_id.keyword",
"size":1000
},
"aggs": {
"collection_details": {
"top_hits": {
"_source": ["collection_id", "collection_name", "collection_action"],
"size": 1
}
}
}
}
}
}

# specify the fields to be returned
query_body["_source"] = ["collection_id", "collection_name", "collection_action"]

# search for program_name based on uses input
# Add conditions based on user input
if program_name:
# Lowercase the program_name before adding it to the query
program_name = program_name.lower()
query_body["query"]["bool"]["must"].append({
"match": {"data_type": program_name}
})
Expand All @@ -495,25 +503,59 @@ async def search_program(self, program_name=None, offset=0, size=None):

# Prepare the query body for execution
body = query_body
#print(body)


# Execute the search query
search_results = await self.es.search(
index="variables_index",
body=body,
filter_path=['hits.hits._id', 'hits.hits._type', 'hits.hits._source', 'aggregations.unique_collection_ids.buckets'],
from_=offset,
size=size
)

# The unique collection_ids will be in the 'aggregations' field of the response
# The unique collection_ids and their details will be in the 'aggregations' field of the response
unique_collection_ids = search_results['aggregations']['unique_collection_ids']['buckets']

#print("Unique collection_ids:", unique_collection_ids)
# Prepare a list to hold the collection details
collection_details_list = []

for bucket in unique_collection_ids:
collection_details = bucket['collection_details']['hits']['hits'][0]['_source']
# Append the details to the list in the desired format
collection_details_list.append(collection_details)

return collection_details_list



async def search_program_list(self):

query_body = {
"size": 0, # We don't need the documents themselves, so set the size to 0
"aggs": {
"unique_program_names": {
"terms": {
"field": "data_type.keyword"
},
"aggs": {
"No_of_studies": {
"cardinality": {
"field": "collection_id.keyword"
}
}
}
}
}
}
# Execute the search query
search_results = await self.es.search(
index="variables_index",
body=query_body
)
# The unique data_types and their counts of unique collection_ids will be in the 'aggregations' field of the response
unique_data_types = search_results['aggregations']['unique_program_names']['buckets']

return unique_data_types

#print(search_results)
return search_results

def _get_var_query(self, concept, fuzziness, prefix_length, query):
"""Returns ES query for variable search"""
Expand Down
12 changes: 11 additions & 1 deletion src/dug/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ async def search_program( program_name: Optional[str] = None):
"status": "success"
}


@APP.get('/program_list')
async def get_program_list():
"""
Search for studies by unique_id (ID or name) and/or study_name.
"""
result = await search.search_program_list()
return {

"result": result,
"status": "success"
}
if __name__ == '__main__':
uvicorn.run(APP)

1 comment on commit 7b208ef

@yskale
Copy link
Collaborator

@yskale yskale commented on 7b208ef Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

program search with program list feature

Please sign in to comment.