Skip to content

Commit 934e5db

Browse files
committed
update to new search index schema
1 parent f050c32 commit 934e5db

File tree

3 files changed

+26
-37
lines changed

3 files changed

+26
-37
lines changed

brain_cli/notes_commands.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
PREVIEW_LENGTH = 80
1717
TABLE_COL_WIDTH_INDEX = 3
1818
TABLE_COL_WIDTH_CATEGORY = 12
19-
TABLE_COL_WIDTH_WORDS = 7
2019
TABLE_COL_WIDTH_SCORE = 7
2120

2221

@@ -59,17 +58,13 @@ def search(
5958
top: int = typer.Option(
6059
DEFAULT_TOP_RESULTS, "--top", "-n", help="Maximum number of results to return"
6160
),
62-
semantic: bool = typer.Option(
63-
False, "--semantic", "-s", help="Use semantic search (slower but more relevant)"
64-
),
6561
detailed: bool = typer.Option(False, "--detailed", "-d", help="Show full content of results"),
6662
):
6763
"""Search book notes using Azure AI Search.
6864
6965
Examples:
7066
brain notes search "discipline and habits"
7167
brain notes search "productivity tips" --top 5
72-
brain notes search "notes on learning" --semantic
7368
brain notes search "deep work" --detailed
7469
"""
7570
try:
@@ -82,21 +77,14 @@ def search(
8277

8378
# Perform search
8479
with console.status(f"[bold cyan]Searching for '{query}'...", spinner="dots"):
85-
if semantic:
86-
results = search_client.semantic_search(query, top=top)
87-
search_type = "Semantic"
88-
else:
89-
results = search_client.search(query, top=top)
90-
search_type = "Text"
80+
results = search_client.search(query, top=top)
9181

9282
if not results:
9383
console.print(f"\n[yellow]No results found for '{query}'[/yellow]")
9484
return
9585

9686
# Display results
97-
console.print(
98-
f"\n[bold cyan]{search_type} Search Results[/bold cyan] for: [bold]'{query}'[/bold]"
99-
)
87+
console.print(f"\n[bold cyan]Search Results[/bold cyan] for: [bold]'{query}'[/bold]")
10088
console.print(f"[dim]Found {len(results)} result(s)[/dim]\n")
10189

10290
if detailed:
@@ -106,9 +94,8 @@ def search(
10694
panel_content = f"**Book:** {result.title}\n"
10795
panel_content += f"**Category:** {result.category}\n"
10896
panel_content += f"**Source:** {result.source}\n"
109-
panel_content += (
110-
f"**Score:** {result.score:.2f} | **Words:** {result.word_count}\n\n"
111-
)
97+
panel_content += f"**File:** {result.file_path}\n"
98+
panel_content += f"**Score:** {result.score:.2f}\n\n"
11299
panel_content += "---\n\n"
113100
panel_content += result.content
114101

@@ -126,7 +113,6 @@ def search(
126113
table.add_column("#", style="dim", width=TABLE_COL_WIDTH_INDEX, justify="right")
127114
table.add_column("Book", style="bold")
128115
table.add_column("Category", style="yellow", width=TABLE_COL_WIDTH_CATEGORY)
129-
table.add_column("Words", justify="right", width=TABLE_COL_WIDTH_WORDS)
130116
table.add_column("Score", justify="right", width=TABLE_COL_WIDTH_SCORE)
131117
table.add_column("Preview", style="dim")
132118

@@ -137,7 +123,6 @@ def search(
137123
str(i),
138124
result.title,
139125
result.category,
140-
str(result.word_count),
141126
f"{result.score:.2f}",
142127
preview,
143128
)

brain_core/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@
7070
"output": 0.002 / 1000,
7171
},
7272
}
73+
74+
# Azure AI Search - Vector embeddings
75+
EMBEDDING_MODEL = "text-embedding-3-small" # Azure OpenAI embedding model
76+
EMBEDDING_DIMENSIONS = 384 # Dimensions for text-embedding-3-small

brain_core/notes_search_client.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from azure.core.credentials import AzureKeyCredential
66
from azure.search.documents import SearchClient
7+
from azure.search.documents.models import VectorizedQuery
78

89

910
@dataclass
@@ -15,10 +16,8 @@ class SearchResult:
1516
content: str
1617
source: str
1718
category: str
18-
file_name: str
19-
word_count: int
19+
file_path: str
2020
score: float
21-
created_at: str
2221
metadata: dict
2322

2423

@@ -63,32 +62,35 @@ def search(self, query: str, top: int = 10, search_mode: str = "any") -> list[Se
6362
content=result.get("content", ""),
6463
source=result.get("source", "Unknown"),
6564
category=result.get("category", ""),
66-
file_name=result.get("file_name", ""),
67-
word_count=result.get("word_count", 0),
65+
file_path=result.get("file_path", ""),
6866
score=result.get("@search.score", 0.0),
69-
created_at=result.get("created_at", ""),
7067
metadata={k: v for k, v in result.items() if not k.startswith("@")},
7168
)
7269
)
7370

7471
return search_results
7572

76-
def semantic_search(self, query: str, top: int = 10) -> list[SearchResult]:
77-
"""Perform a semantic search using Azure's semantic ranking.
73+
def vector_search(
74+
self, query_vector: list[float], top: int = 10, text_query: str | None = None
75+
) -> list[SearchResult]:
76+
"""Perform a vector search using embeddings.
7877
7978
Args:
80-
query: Search query text
79+
query_vector: Query embedding vector (384 dimensions for text-embedding-3-small)
8180
top: Maximum number of results to return
81+
text_query: Optional text query for hybrid search
8282
8383
Returns:
84-
List of SearchResult objects with semantic relevance
84+
List of SearchResult objects with vector similarity
8585
"""
86+
vector_query = VectorizedQuery(
87+
vector=query_vector, k_nearest_neighbors=top, fields="content_vector"
88+
)
89+
8690
results = self.client.search(
87-
search_text=query,
91+
search_text=text_query,
92+
vector_queries=[vector_query],
8893
top=top,
89-
query_type="semantic",
90-
semantic_configuration_name="default",
91-
include_total_count=True,
9294
)
9395

9496
search_results = []
@@ -100,10 +102,8 @@ def semantic_search(self, query: str, top: int = 10) -> list[SearchResult]:
100102
content=result.get("content", ""),
101103
source=result.get("source", "Unknown"),
102104
category=result.get("category", ""),
103-
file_name=result.get("file_name", ""),
104-
word_count=result.get("word_count", 0),
105-
score=result.get("@search.reranker_score", result.get("@search.score", 0.0)),
106-
created_at=result.get("created_at", ""),
105+
file_path=result.get("file_path", ""),
106+
score=result.get("@search.score", 0.0),
107107
metadata={k: v for k, v in result.items() if not k.startswith("@")},
108108
)
109109
)

0 commit comments

Comments
 (0)