From b87817e42922959f5a9a63028dc90c85857b03f3 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 27 Feb 2026 12:14:17 -0600 Subject: [PATCH] feat: add search_q.sh script with chronological sorting - Search memories by keyword/phrase - Automatically sorts results by timestamp (newest first) - Shows formatted output with date, role, and content - Supports environment variables for configuration - Limits results to avoid information overload - Handles errors gracefully --- scripts/search_q.sh | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 scripts/search_q.sh diff --git a/scripts/search_q.sh b/scripts/search_q.sh new file mode 100755 index 0000000..d04a204 --- /dev/null +++ b/scripts/search_q.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +# search_q.sh - Search memories with chronological sorting +# Usage: ./search_q.sh "search query" +# Returns: Results sorted by timestamp (newest first) + +set -e + +QDRANT_URL="${QDRANT_URL:-http://localhost:6333}" +COLLECTION="${QDRANT_COLLECTION:-memories_tr}" +LIMIT="${SEARCH_LIMIT:-10}" + +if [ -z "$1" ]; then + echo "Usage: ./search_q.sh 'your search query'" + echo "" + echo "Environment variables:" + echo " QDRANT_URL - Qdrant endpoint (default: http://localhost:6333)" + echo " SEARCH_LIMIT - Number of results (default: 10)" + exit 1 +fi + +QUERY="$1" + +echo "==========================================" +echo "Searching: '$QUERY'" +echo "==========================================" +echo "" + +# Search with scroll to get all results, then sort by timestamp +# Using scroll API to handle large result sets +SCROLL_ID="null" +ALL_RESULTS="[]" + +while true; do + if [ "$SCROLL_ID" = "null" ]; then + RESPONSE=$(curl -s -X POST "$QDRANT_URL/collections/$COLLECTION/points/scroll" \ + -H "Content-Type: application/json" \ + -d "{ + \"limit\": $LIMIT, + \"with_payload\": true, + \"filter\": { + \"must\": [ + { + \"key\": \"content\", + \"match\": { + \"text\": \"$QUERY\" + } + } + ] + } + }") 2>/dev/null || echo '{"result": {"points": []}}' + else + break # For text search, we get results in first call + fi + + # Extract results + POINTS=$(echo "$RESPONSE" | jq -r '.result.points // []') + + if [ "$POINTS" = "[]" ] || [ "$POINTS" = "null" ]; then + break + fi + + ALL_RESULTS="$POINTS" + break +done + +# Sort by timestamp (newest first) and format output +echo "$ALL_RESULTS" | jq -r ' + sort_by(.payload.timestamp) | reverse | + .[] | + "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n" + + "📅 " + (.payload.timestamp | split("T") | join(" ")) + "\n" + + "👤 " + .payload.role + "\n" + + "📝 " + (.payload.content | if length > 200 then .[0:200] + "..." else . end) + "\n" +' 2>/dev/null || echo "No results found for '$QUERY'" + +echo "" +echo "==========================================" +echo "Search complete. Most recent results shown first." +echo "=========================================="