OxbridgeEconomics
commited on
Update entity.py
Browse files- app/controllers/entity.py +33 -18
app/controllers/entity.py
CHANGED
@@ -2,26 +2,41 @@
|
|
2 |
from models.database import entity_collection
|
3 |
|
4 |
def retrieve_hot_entity():
|
5 |
-
"""Retrieves the top 200
|
6 |
-
sorted by the occurrence field in descending order.
|
7 |
-
The function returns a dict containing the count of documents and a list of documents
|
8 |
-
Parameters
|
9 |
-
None
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
entityType: The value of the entityType field from the document.
|
19 |
-
total_occurrence: The value of the occurrence field from the document.
|
20 |
"""
|
21 |
-
|
22 |
-
{
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
res = {
|
26 |
"Count": len(result),
|
27 |
"Items": result
|
|
|
2 |
from models.database import entity_collection
|
3 |
|
4 |
def retrieve_hot_entity():
|
5 |
+
"""Retrieves the top 200 unique entities from a MongoDB collection,
|
6 |
+
sorted by the total occurrence field in descending order.
|
7 |
+
The function returns a dict containing the count of documents and a list of documents.
|
|
|
|
|
8 |
|
9 |
+
Returns:
|
10 |
+
dict: Contains:
|
11 |
+
- Count (int): The number of unique entities retrieved (up to 200).
|
12 |
+
- Items (list): A list of dictionaries, each containing:
|
13 |
+
- entity: The value of the entity field.
|
14 |
+
- entityType: The value of the entityType field.
|
15 |
+
- total_occurrence: The summed value of the occurrence field for the entity.
|
|
|
|
|
16 |
"""
|
17 |
+
pipeline = [
|
18 |
+
{
|
19 |
+
"$group": {
|
20 |
+
"_id": {
|
21 |
+
"entity": "$entity",
|
22 |
+
"entityType": "$entityType"
|
23 |
+
},
|
24 |
+
"total_occurrence": {"$sum": "$occurrence"}
|
25 |
+
}
|
26 |
+
},
|
27 |
+
{
|
28 |
+
"$project": {
|
29 |
+
"_id": 0,
|
30 |
+
"entity": "$_id.entity",
|
31 |
+
"entityType": "$_id.entityType",
|
32 |
+
"total_occurrence": 1
|
33 |
+
}
|
34 |
+
},
|
35 |
+
{"$sort": {"total_occurrence": -1}},
|
36 |
+
{"$limit": 200}
|
37 |
+
]
|
38 |
+
|
39 |
+
result = list(entity_collection.aggregate(pipeline))
|
40 |
res = {
|
41 |
"Count": len(result),
|
42 |
"Items": result
|