internal/gaby: minor search fixes

If the search text is too large, then the g.vector.Get will fail
(and crash the program) due to a Firestore error about the
purported key being invalid. Cut off searches for "keys"
that are far too long to be real keys.

Also trim spaces from the query so that IDs can be searched
for using the -search command line mode.

Change-Id: I02aab7cd7c337c7666ab079fc3c6874b76b79afb
Reviewed-on: https://go-review.googlesource.com/c/oscar/+/708896
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/internal/gaby/search.go b/internal/gaby/search.go
index acc03bf..62effe1 100644
--- a/internal/gaby/search.go
+++ b/internal/gaby/search.go
@@ -74,22 +74,33 @@
 //
 // It returns an error if search fails.
 func (g *Gaby) search(ctx context.Context, q string, opts search.Options) (results []search.Result, err error) {
+	q = strings.TrimSpace(q)
 	if q == "" {
 		return nil, nil
 	}
 
-	if vec, ok := g.vector.Get(q); ok {
-		results = search.Vector(g.vector, g.docs,
-			&search.VectorRequest{
+	// If a document ID is too large, then the g.vector.Get will fail
+	// (and crash the program) due to a Firestore error about the
+	// purported ID being invalid.
+	// Don't try to use large queries as document IDs.
+	const maxKeyForIDLookup = 700
+
+	matchID := false
+	if len(q) < maxKeyForIDLookup {
+		if vec, ok := g.vector.Get(q); ok {
+			matchID = true
+			results = search.Vector(g.vector, g.docs, &search.VectorRequest{
 				Options: opts,
 				Vector:  vec,
 			})
-	} else {
-		if results, err = search.Query(ctx, g.vector, g.docs, g.embed,
-			&search.QueryRequest{
-				EmbedDoc: llm.EmbedDoc{Text: q},
-				Options:  opts,
-			}); err != nil {
+		}
+	}
+	if !matchID {
+		results, err = search.Query(ctx, g.vector, g.docs, g.embed, &search.QueryRequest{
+			EmbedDoc: llm.EmbedDoc{Text: q},
+			Options:  opts,
+		})
+		if err != nil {
 			return nil, err
 		}
 	}