Skip to content

Commit 4cebcde

Browse files
author
Dylan Huang
committed
Enhance ElasticsearchClient to support sorting in search_by_match method and update LogsServer to utilize this feature for retrieving logs sorted by timestamp.
1 parent 9fe3f7b commit 4cebcde

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

eval_protocol/log_utils/elasticsearch_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,22 @@ def search_by_term(self, field: str, value: Any, size: int = 10) -> Optional[Dic
216216
query = {"term": {field: value}}
217217
return self.search(query, size=size)
218218

219-
def search_by_match(self, field: str, value: str, size: int = 10) -> Optional[Dict[str, Any]]:
219+
def search_by_match(
220+
self, field: str, value: str, size: int = 10, sort: Optional[List[Dict[str, Any]]] = None
221+
) -> Optional[Dict[str, Any]]:
220222
"""Search documents by text match.
221223
222224
Args:
223225
field: Field name to search
224226
value: Text to match
225227
size: Number of results to return
228+
sort: Sort specification (e.g., [{"@timestamp": {"order": "desc"}}])
226229
227230
Returns:
228231
Dict containing search results, or None if failed
229232
"""
230233
query = {"match": {field: value}}
231-
return self.search(query, size=size)
234+
return self.search(query, size=size, sort=sort)
232235

233236
def search_by_match_phrase_prefix(self, field: str, value: str, size: int = 10) -> Optional[Dict[str, Any]]:
234237
"""Search documents by phrase prefix match.

eval_protocol/utils/logs_server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,11 @@ async def get_logs(
351351
raise HTTPException(status_code=503, detail="Elasticsearch is not configured for this logs server")
352352

353353
try:
354-
# Search for logs by rollout_id
355-
search_results = self.elasticsearch_client.search_by_match("rollout_id", rollout_id, size=limit)
354+
# Search for logs by rollout_id, sorted by timestamp in descending order (newest first)
355+
sort_spec = [{"@timestamp": {"order": "desc"}}]
356+
search_results = self.elasticsearch_client.search_by_match(
357+
"rollout_id", rollout_id, size=limit, sort=sort_spec
358+
)
356359

357360
if not search_results or "hits" not in search_results:
358361
# Return empty response using Pydantic model

0 commit comments

Comments
 (0)