Skip to content

Commit

Permalink
[#136] get search matching record count for filter based search
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdash committed Aug 28, 2024
1 parent 93d0f3a commit 001f663
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions dspback/routers/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,8 @@ async def search(
}
},
)

if term:
stages[0]['$search']['highlight'] = {'path': search_paths}
# get only results which meet minimum relevance score threshold
score_threshold = get_settings().search_relevance_score_threshold
stages.append({'$match': {'score': {'$gt': score_threshold}}})
stages[0]['$search']['count'] = {'type': 'total'}

stages.append({'$project': {'meta': "$$SEARCH_META",
stages.append({'$project': {'meta': "$$SEARCH_META",
'_id': 0,
'@context': 1,
'repository_identifier': 1,
Expand All @@ -112,19 +105,27 @@ async def search(
'relations': 1,
'legacy': 1,
'clusters': 1,
'score': 1,
'highlights': {'$meta': 'searchHighlights'}
'score': 1
}
})

if term:
stages[0]['$search']['highlight'] = {'path': search_paths}
# get only results which meet minimum relevance score threshold
score_threshold = get_settings().search_relevance_score_threshold
stages.append({'$match': {'score': {'$gt': score_threshold}}})
for stage in stages:
if '$project' in stage:
stage['$project']['highlights'] = {'$meta': 'searchHighlights'}
break

results = await request.app.db[get_settings().mongo_database]["discovery"].aggregate(stages).to_list(pageSize)

if term:
meta = None
for result in results:
meta = result.pop('meta', None)
if meta:
results = {"meta": meta, "docs": results}
meta = None
for result in results:
meta = result.pop('meta', None)
if meta:
results = {"meta": meta, "docs": results}
else:
results = {"docs": results}
return results
Expand Down

0 comments on commit 001f663

Please sign in to comment.