You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@zeppelin.apache.org by "Copilot (via GitHub)" <gi...@apache.org> on 2026/04/20 01:06:53 UTC

Re: [PR] [ZEPPELIN-6411] Semantic search for Zeppelin [zeppelin]

Copilot commented on code in PR #5218:
URL: https://github.com/apache/zeppelin/pull/5218#discussion_r3107816028


##########
zeppelin-web-angular/src/app/pages/workspace/notebook-search/result-item/result-item.component.ts:
##########
@@ -75,110 +47,37 @@ export class NotebookSearchResultItemComponent implements OnChanges, OnDestroy {
       this.queryParams = {};
     } else {
       this.routerLink = ['/', 'notebook', noteId];
-      this.queryParams = {
-        paragraph,
-        term
-      };
+      this.queryParams = { paragraph, term };
     }
     this.displayName = this.result.name ? this.result.name : `Note ${noteId}`;
-  }
-
-  setHighlightKeyword(): void {
-    let mergedStr = this.result.header ? `${this.result.header}\n\n${this.result.snippet}` : this.result.snippet;
-
-    const regexp = /<B>(.+?)<\/B>/g;
-    const matches = [];
-    let match = regexp.exec(mergedStr);
-
-    while (match !== null) {
-      if (match[1]) {
-        matches.push(match[1].toLocaleLowerCase());
-      }
-      match = regexp.exec(mergedStr);
-    }
-
-    mergedStr = mergedStr.replace(regexp, '$1');
-    this.mergedStr = mergedStr;
-    const keywords = [...new Set(matches)];
-    this.highlightPositions = getKeywordPositions(keywords, mergedStr);
-  }
 
-  applyHighlight() {
-    if (this.editor) {
-      this.decorations = this.editor.deltaDecorations(
-        this.decorations,
-        this.highlightPositions.map(highlight => {
-          const line = highlight.line + 1;
-          const character = highlight.character + 1;
-          return {
-            range: new Range(line, character, line, character + highlight.length),
-            options: {
-              className: 'mark',
-              stickiness: 1
-            }
-          };
-        })
-      );
-      this.cdr.markForCheck();
-    }
-  }
+    // snippet = SQL/code, header = tables + output
+    this.codeText = this.result.snippet || '';
+    this.interpreter = this.detectInterpreter(this.codeText);
 
-  setLanguage() {
-    const model = this.editor?.getModel();
-    if (!model) {
-      throw new Error('Editor model is not defined.');
-    }
-    const editorModes = {
-      scala: /^%(\w*\.)?(spark|flink)/,
-      python: /^%(\w*\.)?(pyspark|python)/,
-      html: /^%(\w*\.)?(angular|ng)/,
-      r: /^%(\w*\.)?(r|sparkr|knitr)/,
-      sql: /^%(\w*\.)?\wql/,
-      yaml: /^%(\w*\.)?\wconf/,
-      markdown: /^%md/,
-      shell: /^%sh/
-    };
-    let mode = 'text';
-    for (const [modeOption, regex] of Object.entries(editorModes)) {
-      if (regex.test(this.result.snippet)) {
-        mode = modeOption;
-        break;
+    // Parse header: lines with 📊 are tables, rest is output
+    const header = this.result.header || '';
+    const lines = header.split('\n');
+    const tableParts: string[] = [];
+    const outputParts: string[] = [];
+    for (const line of lines) {
+      if (line.startsWith('📊')) {
+        tableParts.push(line.substring(2).trim());
+      } else if (line.trim()) {
+        outputParts.push(line);
       }
     }
-    editor.setModelLanguage(model, mode);
-  }
-
-  autoAdjustEditorHeight() {
-    this.ngZone.run(() => {
-      setTimeout(() => {
-        const model = this.editor?.getModel();
-        if (model) {
-          this.height = this.editor!.getOption(monaco.editor.EditorOption.lineHeight) * (model.getLineCount() + 2);
-          this.editor!.layout();
-          this.cdr.markForCheck();
-        }
-      });
-    });
-  }
-
-  initializedEditor(editorInstance: IEditor) {
-    this.editor = editorInstance as IStandaloneCodeEditor;
-    this.editor.setValue(this.mergedStr ?? '');
-    this.setLanguage();
-    this.autoAdjustEditorHeight();
-    this.applyHighlight();
-  }
-
-  ngOnChanges(changes: SimpleChanges): void {
-    if (changes.result) {
-      this.setDisplayNameAndRouterLink();
-      this.setHighlightKeyword();
-      this.autoAdjustEditorHeight();
-      this.applyHighlight();
-    }
+    this.tablesText = tableParts.join(', ');
+    this.outputText = outputParts.join('\n');
   }
 
-  ngOnDestroy(): void {
-    this.editor?.dispose();
+  private detectInterpreter(text: string): string {
+    if (!text) { return ''; }
+    if (/select|insert|create|from|where/i.test(text)) { return 'sql'; }
+    if (/^%(\w*\.)?py/i.test(text)) { return 'python'; }
+    if (/^%md/i.test(text)) { return 'md'; }
+    if (/^%sh/i.test(text)) { return 'sh'; }
+    if (/import |def |class /i.test(text)) { return 'python'; }
+    return 'text';

Review Comment:
   `detectInterpreter` returns `'text'` for unknown content, and the template displays the badge whenever `interpreter` is truthy. This will show a “text” badge for most results. Consider returning an empty string for “unknown” (to hide the badge) or adjusting the template condition to hide the default.
   



##########
zeppelin-web-angular/projects/zeppelin-sdk/tsconfig.json:
##########
@@ -5,6 +5,8 @@
     "target": "es2015",
     "declaration": true,
     "inlineSources": true,
+    "skipLibCheck": true,
+    "noImplicitAny": false,

Review Comment:
   Same concern as the app tsconfig: turning off `noImplicitAny` and enabling `skipLibCheck` for the SDK reduces type guarantees for consumers and can hide breaking typing problems. Prefer fixing the underlying typings or scoping these relaxations to the minimal surface area.
   



##########
zeppelin-web-angular/src/app/pages/workspace/notebook-search/result-item/result-item.component.ts:
##########
@@ -75,110 +47,37 @@ export class NotebookSearchResultItemComponent implements OnChanges, OnDestroy {
       this.queryParams = {};
     } else {
       this.routerLink = ['/', 'notebook', noteId];
-      this.queryParams = {
-        paragraph,
-        term
-      };
+      this.queryParams = { paragraph, term };
     }
     this.displayName = this.result.name ? this.result.name : `Note ${noteId}`;
-  }
-
-  setHighlightKeyword(): void {
-    let mergedStr = this.result.header ? `${this.result.header}\n\n${this.result.snippet}` : this.result.snippet;
-
-    const regexp = /<B>(.+?)<\/B>/g;
-    const matches = [];
-    let match = regexp.exec(mergedStr);
-
-    while (match !== null) {
-      if (match[1]) {
-        matches.push(match[1].toLocaleLowerCase());
-      }
-      match = regexp.exec(mergedStr);
-    }
-
-    mergedStr = mergedStr.replace(regexp, '$1');
-    this.mergedStr = mergedStr;
-    const keywords = [...new Set(matches)];
-    this.highlightPositions = getKeywordPositions(keywords, mergedStr);
-  }
 
-  applyHighlight() {
-    if (this.editor) {
-      this.decorations = this.editor.deltaDecorations(
-        this.decorations,
-        this.highlightPositions.map(highlight => {
-          const line = highlight.line + 1;
-          const character = highlight.character + 1;
-          return {
-            range: new Range(line, character, line, character + highlight.length),
-            options: {
-              className: 'mark',
-              stickiness: 1
-            }
-          };
-        })
-      );
-      this.cdr.markForCheck();
-    }
-  }
+    // snippet = SQL/code, header = tables + output
+    this.codeText = this.result.snippet || '';
+    this.interpreter = this.detectInterpreter(this.codeText);

Review Comment:
   `result.snippet` from `LuceneSearch` contains `<B>...</B>` highlight tags. Assigning it directly to `codeText` means the UI will render/show those tags literally in the `<pre>` block. Consider stripping the highlight markup (or reintroducing highlighting via styling) before assigning `codeText`.



##########
zeppelin-zengine/src/main/java/org/apache/zeppelin/search/EmbeddingSearch.java:
##########
@@ -0,0 +1,724 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zeppelin.search;
+
+import ai.djl.huggingface.tokenizers.Encoding;
+import ai.djl.huggingface.tokenizers.HuggingFaceTokenizer;
+import com.google.common.collect.ImmutableMap;
+import ai.onnxruntime.OnnxTensor;
+import ai.onnxruntime.OrtEnvironment;
+import ai.onnxruntime.OrtException;
+import ai.onnxruntime.OrtSession;
+
+import java.io.BufferedInputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.LongBuffer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.annotation.PreDestroy;
+import jakarta.inject.Inject;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.zeppelin.conf.ZeppelinConfiguration;
+import org.apache.zeppelin.interpreter.InterpreterResult;
+import org.apache.zeppelin.interpreter.InterpreterResultMessage;
+import org.apache.zeppelin.notebook.Note;
+import org.apache.zeppelin.notebook.Notebook;
+import org.apache.zeppelin.notebook.Paragraph;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Semantic search for Zeppelin notebooks using ONNX-based sentence embeddings.
+ *
+ * <p>Uses the all-MiniLM-L6-v2 model to generate 384-dimensional embeddings for each
+ * paragraph's text, title, and output. Queries are embedded with the same model and
+ * matched via cosine similarity, enabling natural language search like
+ * "yesterday's spend query" to find {@code WHERE date = current_date - 1}.
+ *
+ * <p>The embedding index is held in memory (float[][] + metadata) and persisted to a
+ * single binary file on disk. For typical Zeppelin deployments (< 50K paragraphs),
+ * brute-force cosine similarity completes in under 50ms.
+ *
+ * <p>Model files are downloaded on first use to {@code zeppelin.search.index.path}
+ * and cached for subsequent starts.
+ */
+public class EmbeddingSearch extends SearchService {
+  private static final Logger LOGGER = LoggerFactory.getLogger(EmbeddingSearch.class);
+
+  private static final String MODEL_NAME = "all-MiniLM-L6-v2";
+  private static final String MODEL_URL =
+      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx";
+  private static final String TOKENIZER_URL =
+      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/tokenizer.json";
+  private static final int EMBEDDING_DIM = 384;
+  private static final int MAX_SEQ_LENGTH = 256;
+  private static final int MAX_RESULTS = 20;
+  private static final float MIN_SIMILARITY = 0.25f;
+  private static final int MAX_TEXT_LENGTH = 1500;
+  private static final int SNIPPET_LENGTH = 150;
+
+  static final String ID_FIELD = "id";
+  private static final String PARAGRAPH = "paragraph";
+  /** Regex to extract qualified table names from SQL (e.g. schema.table). */
+  private static final Pattern TABLE_RE =
+      Pattern.compile("(?:FROM|JOIN)\\s+([a-zA-Z_]\\w*\\.[a-zA-Z_]\\w*)", Pattern.CASE_INSENSITIVE);
+  private static final float TABLE_BOOST = 0.05f;
+
+  private final Notebook notebook;
+  private final Path indexPath;
+
+  // ONNX inference
+  private OrtEnvironment ortEnv;
+  private OrtSession ortSession;
+  private HuggingFaceTokenizer tokenizer;
+
+  // In-memory vector index: docId -> (embedding, metadata)
+  private final ConcurrentHashMap<String, IndexEntry> index = new ConcurrentHashMap<>();
+  private final ReadWriteLock indexLock = new ReentrantReadWriteLock();
+
+  /** A single indexed document (paragraph or note name). */
+  private static class IndexEntry {
+    final float[] embedding;
+    final String noteName;
+    final String text;
+    final String title;
+    final String tables;
+    final String output;
+
+    IndexEntry(float[] embedding, String noteName, String text, String title,
+               String tables, String output) {
+      this.embedding = embedding;
+      this.noteName = noteName;
+      this.text = text;
+      this.title = title;
+      this.tables = tables;
+      this.output = output;
+    }
+  }
+
+  @Inject
+  public EmbeddingSearch(ZeppelinConfiguration zConf, Notebook notebook) throws IOException {
+    super("EmbeddingSearch");
+    this.notebook = notebook;
+    this.indexPath = Paths.get(zConf.getZeppelinSearchIndexPath());
+    Files.createDirectories(indexPath);
+
+    try {
+      initModel();
+    } catch (Exception e) {
+      throw new IOException("Failed to initialize embedding model", e);
+    }
+
+    if (zConf.isIndexRebuild()) {
+      notebook.addInitConsumer(this::addNoteIndex);
+    }
+    loadIndex();
+    this.notebook.addNotebookEventListener(this);
+  }
+
+  /** Package-private constructor for testing without DI. */
+  EmbeddingSearch(ZeppelinConfiguration zConf, Notebook notebook, boolean skipModel)
+      throws IOException {
+    super("EmbeddingSearch");
+    this.notebook = notebook;
+    this.indexPath = Paths.get(zConf.getZeppelinSearchIndexPath());
+    Files.createDirectories(indexPath);
+    if (!skipModel) {
+      try {
+        initModel();
+      } catch (Exception e) {
+        throw new IOException("Failed to initialize embedding model", e);
+      }
+    }
+    if (zConf.isIndexRebuild()) {
+      notebook.addInitConsumer(this::addNoteIndex);
+    }
+    this.notebook.addNotebookEventListener(this);
+  }
+
+  // ---- Model initialization ----
+
+  private void initModel() throws OrtException, IOException {
+    Path modelDir = indexPath.resolve("models").resolve(MODEL_NAME);
+    Files.createDirectories(modelDir);
+
+    Path modelFile = modelDir.resolve("model.onnx");
+    Path tokenizerFile = modelDir.resolve("tokenizer.json");
+
+    if (!Files.exists(modelFile)) {
+      LOGGER.info("Downloading embedding model {} ...", MODEL_NAME);
+      downloadFile(MODEL_URL, modelFile);
+      LOGGER.info("Model downloaded to {}", modelFile);
+    }
+    if (!Files.exists(tokenizerFile)) {
+      LOGGER.info("Downloading tokenizer for {} ...", MODEL_NAME);
+      downloadFile(TOKENIZER_URL, tokenizerFile);
+    }
+
+    ortEnv = OrtEnvironment.getEnvironment();
+    OrtSession.SessionOptions opts = new OrtSession.SessionOptions();
+    opts.setIntraOpNumThreads(Runtime.getRuntime().availableProcessors());
+    ortSession = ortEnv.createSession(modelFile.toString(), opts);
+    tokenizer = HuggingFaceTokenizer.newInstance(tokenizerFile);
+    LOGGER.info("Embedding model loaded: {}, dim={}", MODEL_NAME, EMBEDDING_DIM);
+  }
+
+  private static void downloadFile(String urlStr, Path dest) throws IOException {
+    URL url = new URL(urlStr);
+    try (InputStream in = new BufferedInputStream(url.openStream());
+         FileOutputStream out = new FileOutputStream(dest.toFile())) {
+      byte[] buf = new byte[8192];
+      int n;
+      while ((n = in.read(buf)) != -1) {
+        out.write(buf, 0, n);
+      }
+    }
+  }
+
+  // ---- Embedding computation ----
+
+  /**
+   * Compute a normalized embedding for the given text.
+   * Uses mean pooling over token embeddings with attention mask.
+   */
+  float[] embed(String text) {
+    if (ortSession == null || tokenizer == null) {
+      return new float[EMBEDDING_DIM];
+    }
+    try {
+      Encoding encoding = tokenizer.encode(text, true, true);
+      long[] inputIds = encoding.getIds();
+      long[] attentionMask = encoding.getAttentionMask();
+
+      // Truncate to max sequence length
+      int seqLen = Math.min(inputIds.length, MAX_SEQ_LENGTH);
+      long[] ids = new long[seqLen];
+      long[] mask = new long[seqLen];
+      long[] tokenTypeIds = new long[seqLen];
+      System.arraycopy(inputIds, 0, ids, 0, seqLen);
+      System.arraycopy(attentionMask, 0, mask, 0, seqLen);
+
+      long[] shape = {1, seqLen};
+      OnnxTensor idsTensor = OnnxTensor.createTensor(ortEnv, LongBuffer.wrap(ids), shape);
+      OnnxTensor maskTensor = OnnxTensor.createTensor(ortEnv, LongBuffer.wrap(mask), shape);
+      OnnxTensor typeTensor = OnnxTensor.createTensor(ortEnv, LongBuffer.wrap(tokenTypeIds), shape);
+
+      Map<String, OnnxTensor> inputs = new HashMap<>();
+      inputs.put("input_ids", idsTensor);
+      inputs.put("attention_mask", maskTensor);
+      inputs.put("token_type_ids", typeTensor);
+
+      try (OrtSession.Result result = ortSession.run(inputs)) {
+        // Output shape: [1, seqLen, 384] — mean pool over sequence dim
+        float[][][] output = (float[][][]) result.get(0).getValue();
+        float[] pooled = meanPool(output[0], mask, seqLen);
+        normalize(pooled);
+        return pooled;
+      } finally {
+        idsTensor.close();
+        maskTensor.close();
+        typeTensor.close();
+      }
+    } catch (OrtException e) {
+      LOGGER.error("Embedding failed for text length {}", text.length(), e);
+      return new float[EMBEDDING_DIM];
+    }
+  }
+
+  /** Mean pooling: average token embeddings weighted by attention mask. */
+  private static float[] meanPool(float[][] tokenEmbeddings, long[] mask, int seqLen) {
+    float[] result = new float[EMBEDDING_DIM];
+    float maskSum = 0;
+    for (int i = 0; i < seqLen; i++) {
+      if (mask[i] == 1) {
+        maskSum++;
+        for (int j = 0; j < EMBEDDING_DIM; j++) {
+          result[j] += tokenEmbeddings[i][j];
+        }
+      }
+    }
+    if (maskSum > 0) {
+      for (int j = 0; j < EMBEDDING_DIM; j++) {
+        result[j] /= maskSum;
+      }
+    }
+    return result;
+  }
+
+  /** L2-normalize in place. */
+  private static void normalize(float[] vec) {
+    float norm = 0;
+    for (float v : vec) {
+      norm += v * v;
+    }
+    norm = (float) Math.sqrt(norm);
+    if (norm > 0) {
+      for (int i = 0; i < vec.length; i++) {
+        vec[i] /= norm;
+      }
+    }
+  }
+
+  /** Cosine similarity between two normalized vectors (= dot product). */
+  private static float cosineSimilarity(float[] a, float[] b) {
+    float dot = 0;
+    for (int i = 0; i < a.length; i++) {
+      dot += a[i] * b[i];
+    }
+    return dot;
+  }
+
+  // ---- Text extraction ----
+
+  /**
+   * Strip interpreter prefix like {@code %spark.sql}, {@code %athena} from paragraph text.
+   * Handles both {@code %name\ncode} and {@code %name code} formats.
+   */
+  static String stripInterpreterPrefix(String text) {
+    if (text == null || !text.startsWith("%")) {
+      return text;
+    }
+    // Find end of interpreter directive: first newline or first space after %word
+    int newlineIdx = text.indexOf('\n');
+    if (newlineIdx >= 0) {
+      return text.substring(newlineIdx + 1);
+    }
+    // Single-line: "%interpreter some code" — strip up to first space
+    int spaceIdx = text.indexOf(' ');
+    if (spaceIdx >= 0) {
+      return text.substring(spaceIdx + 1);
+    }
+    // Just "%interpreter" with no content
+    return "";
+  }
+
+  /**
+   * Extract qualified table names (schema.table) from SQL text.
+   */
+  static String extractTables(String text) {
+    if (text == null) {
+      return "";
+    }
+    Set<String> tables = new HashSet<>();
+    Matcher m = TABLE_RE.matcher(text);
+    while (m.find()) {
+      tables.add(m.group(1).toLowerCase());
+    }
+    return String.join(" ", tables);
+  }
+
+  /**
+   * Extract searchable output text from paragraph results (TABLE headers, TEXT).
+   */
+  static String extractOutput(Paragraph p) {
+    InterpreterResult result = p.getReturn();
+    if (result == null) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();
+    for (InterpreterResultMessage msg : result.message()) {
+      if (msg.getType() == InterpreterResult.Type.TEXT
+          || msg.getType() == InterpreterResult.Type.TABLE) {
+        String data = msg.getData();
+        if (StringUtils.isNotBlank(data)) {
+          sb.append(data, 0, Math.min(data.length(), 500));
+          sb.append("\n");
+        }
+      }
+    }
+    return sb.toString().trim();
+  }
+
+  /**
+   * Build a rich text representation of a paragraph for embedding.
+   * Includes code/text, title, table names, and output (table headers, text results).
+   */
+  private String buildParagraphText(String noteName, Paragraph p) {
+    StringBuilder sb = new StringBuilder();
+    if (StringUtils.isNotBlank(noteName)) {
+      sb.append("Notebook: ").append(noteName).append("\n");
+    }
+    if (StringUtils.isNotBlank(p.getTitle())) {
+      sb.append(p.getTitle()).append("\n");
+    }
+    if (StringUtils.isNotBlank(p.getText())) {
+      String text = p.getText();
+      // Strip interpreter prefix (e.g. "%spark.sql", "%athena\n")
+      text = stripInterpreterPrefix(text);
+      // Include extracted table names for better semantic matching
+      String tables = extractTables(text);
+      if (StringUtils.isNotBlank(tables)) {
+        sb.append("Tables: ").append(tables).append("\n");
+      }
+      sb.append(text, 0, Math.min(text.length(), MAX_TEXT_LENGTH));
+    }
+    // Include output for richer semantic matching
+    InterpreterResult result = p.getReturn();
+    if (result != null) {
+      for (InterpreterResultMessage msg : result.message()) {
+        if (msg.getType() == InterpreterResult.Type.TEXT
+            || msg.getType() == InterpreterResult.Type.TABLE) {
+          String data = msg.getData();
+          if (StringUtils.isNotBlank(data)) {
+            sb.append("\n").append(data, 0, Math.min(data.length(), 500));
+          }
+        }
+      }
+    }
+    return sb.toString();
+  }
+
+  // ---- SearchService implementation ----
+
+  @Override
+  public List<Map<String, String>> query(String queryStr) {
+    if (StringUtils.isBlank(queryStr) || index.isEmpty()) {
+      return Collections.emptyList();
+    }
+
+    float[] queryEmbedding = embed(queryStr);
+
+    // Phase 1: find top-N results and discover relevant tables
+    List<Map.Entry<String, Float>> scored = new ArrayList<>();
+    indexLock.readLock().lock();
+    try {
+      for (Map.Entry<String, IndexEntry> entry : index.entrySet()) {
+        float sim = cosineSimilarity(queryEmbedding, entry.getValue().embedding);
+        scored.add(Map.entry(entry.getKey(), sim));
+      }
+    } finally {
+      indexLock.readLock().unlock();
+    }
+    scored.sort((a, b) -> Float.compare(b.getValue(), a.getValue()));
+
+    // Collect tables from top-20 results, weighted by rank
+    Map<String, Float> tableWeights = new HashMap<>();
+    for (int i = 0; i < Math.min(scored.size(), 20); i++) {
+      IndexEntry entry = index.get(scored.get(i).getKey());
+      if (entry != null && StringUtils.isNotBlank(entry.tables)) {
+        float weight = 1.0f / (i + 1);
+        for (String t : entry.tables.split(" ")) {
+          tableWeights.merge(t, weight, Float::sum);
+        }
+      }
+    }
+    // Keep tables with weight > 20% of top table's weight
+    Set<String> relevantTables = new HashSet<>();
+    if (!tableWeights.isEmpty()) {
+      float maxWeight = Collections.max(tableWeights.values());
+      float threshold = maxWeight * 0.2f;
+      tableWeights.forEach((t, w) -> {
+        if (w >= threshold) {
+          relevantTables.add(t);
+        }
+      });
+    }
+
+    // Phase 2: re-score with table boost
+    List<Map<String, String>> results = new ArrayList<>();
+    for (int i = 0; i < scored.size() && results.size() < MAX_RESULTS; i++) {
+      float sim = scored.get(i).getValue();
+      if (sim < MIN_SIMILARITY) {
+        break;
+      }
+      String docId = scored.get(i).getKey();
+      IndexEntry entry = index.get(docId);
+      if (entry == null || StringUtils.isBlank(entry.text)) {
+        continue;
+      }
+      // Boost paragraphs that reference discovered tables
+      if (!relevantTables.isEmpty() && StringUtils.isNotBlank(entry.tables)) {
+        for (String t : entry.tables.split(" ")) {
+          if (relevantTables.contains(t)) {
+            sim += TABLE_BOOST;
+          }

Review Comment:
   Table boosting has no effect on ranking here: `sim` is incremented, but the boosted score isn’t stored or used to re-sort `scored`/`results` before returning. As a result, relevant-table boosting won’t change result order even when it should.



##########
zeppelin-zengine/src/test/java/org/apache/zeppelin/search/EmbeddingSearchTest.java:
##########
@@ -0,0 +1,345 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zeppelin.search;
+
+import static org.apache.zeppelin.search.EmbeddingSearch.formatId;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.zeppelin.conf.ZeppelinConfiguration;
+import org.apache.zeppelin.interpreter.InterpreterFactory;
+import org.apache.zeppelin.interpreter.InterpreterSetting;
+import org.apache.zeppelin.interpreter.InterpreterSettingManager;
+import org.apache.zeppelin.notebook.AuthorizationService;
+import org.apache.zeppelin.notebook.Note;
+import org.apache.zeppelin.notebook.NoteManager;
+import org.apache.zeppelin.notebook.Notebook;
+import org.apache.zeppelin.notebook.Paragraph;
+import org.apache.zeppelin.notebook.repo.InMemoryNotebookRepo;
+import org.apache.zeppelin.notebook.repo.NotebookRepo;
+import org.apache.zeppelin.user.AuthenticationInfo;
+import org.apache.zeppelin.user.Credentials;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+
+/**
+ * Tests for {@link EmbeddingSearch}.
+ *
+ * <p>These tests require the ONNX model to be downloaded, so they are gated behind
+ * the {@code ZEPPELIN_EMBEDDING_TEST} environment variable. To run:
+ * <pre>
+ *   ZEPPELIN_EMBEDDING_TEST=true mvn test -pl zeppelin-zengine \
+ *     -Dtest=EmbeddingSearchTest
+ * </pre>
+ *
+ * <p>The model (~86MB) is downloaded once to a temp directory and cached for the
+ * duration of the test run.
+ */
+@EnabledIfEnvironmentVariable(named = "ZEPPELIN_EMBEDDING_TEST", matches = "true")
+class EmbeddingSearchTest {
+
+  private Notebook notebook;
+  private InterpreterSettingManager interpreterSettingManager;
+  private NoteManager noteManager;
+  private EmbeddingSearch searchService;
+  private File indexDir;
+
+  @BeforeEach
+  public void startUp() throws IOException {
+    indexDir = Files.createTempDirectory(this.getClass().getSimpleName()).toFile();
+    ZeppelinConfiguration zConf = ZeppelinConfiguration.load();
+    zConf.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_SEARCH_INDEX_PATH.getVarName(),
+        indexDir.getAbsolutePath());
+

Review Comment:
   This test creates a fresh temp `indexDir` for every test case and deletes it in `@AfterEach`. Since the model download is stored under `zeppelin.search.index.path/models/...`, this will trigger an ~86MB download per test method, contradicting the class-level comment and making runs very slow/flaky. Consider sharing a single temp dir per test class (`@BeforeAll`/`@AfterAll`) or caching the model directory across tests.



##########
zeppelin-web/src/app/search/result-list.html:
##########
@@ -14,33 +14,30 @@
 <div class="searchResults">
     <div ng-if="isResult" class="row">
     <div class="col-sm-8" style="margin: 0 auto; float: none">
-      <ul class="search-results">
+      <ul class="search-results" style="list-style:none;padding:0">
         <li class="panel panel-default" ng-repeat="note in notes">
           <div class="panel-heading">
-            <h4>
+            <h4 style="margin:0;display:flex;align-items:center;gap:8px">

Review Comment:
   Inline styling here duplicates what’s already defined in `zeppelin-web/src/app/search/search.css` (e.g., `.search-results` list style). Prefer updating the CSS file / adding classes rather than inlining styles in the template to keep theming and maintenance manageable.
   



##########
zeppelin-zengine/pom.xml:
##########
@@ -191,6 +193,26 @@
       <version>${lucene.version}</version>
     </dependency>
 
+    <!-- Semantic search: ONNX Runtime for model inference -->
+    <dependency>
+      <groupId>com.microsoft.onnxruntime</groupId>
+      <artifactId>onnxruntime</artifactId>
+      <version>${onnxruntime.version}</version>
+    </dependency>
+
+    <!-- Semantic search: DJL HuggingFace tokenizer -->
+    <dependency>
+      <groupId>ai.djl.huggingface</groupId>
+      <artifactId>tokenizers</artifactId>
+      <version>${djl.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>net.java.dev.jna</groupId>
+          <artifactId>jna</artifactId>
+        </exclusion>
+      </exclusions>

Review Comment:
   This exclusion removes `net.java.dev.jna:jna` from the zengine dependency tree, but `zeppelin-zengine` doesn’t declare JNA itself. If the DJL tokenizers runtime needs JNA, this can fail at runtime or in module-level tests. Consider adding an explicit JNA dependency (using the project’s chosen version) or switching to an alternative tokenizer artifact that doesn’t rely on JNA.
   



##########
zeppelin-zengine/src/main/java/org/apache/zeppelin/search/EmbeddingSearch.java:
##########
@@ -0,0 +1,724 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zeppelin.search;
+
+import ai.djl.huggingface.tokenizers.Encoding;
+import ai.djl.huggingface.tokenizers.HuggingFaceTokenizer;
+import com.google.common.collect.ImmutableMap;
+import ai.onnxruntime.OnnxTensor;
+import ai.onnxruntime.OrtEnvironment;
+import ai.onnxruntime.OrtException;
+import ai.onnxruntime.OrtSession;
+
+import java.io.BufferedInputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.LongBuffer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.annotation.PreDestroy;
+import jakarta.inject.Inject;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.zeppelin.conf.ZeppelinConfiguration;
+import org.apache.zeppelin.interpreter.InterpreterResult;
+import org.apache.zeppelin.interpreter.InterpreterResultMessage;
+import org.apache.zeppelin.notebook.Note;
+import org.apache.zeppelin.notebook.Notebook;
+import org.apache.zeppelin.notebook.Paragraph;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Semantic search for Zeppelin notebooks using ONNX-based sentence embeddings.
+ *
+ * <p>Uses the all-MiniLM-L6-v2 model to generate 384-dimensional embeddings for each
+ * paragraph's text, title, and output. Queries are embedded with the same model and
+ * matched via cosine similarity, enabling natural language search like
+ * "yesterday's spend query" to find {@code WHERE date = current_date - 1}.
+ *
+ * <p>The embedding index is held in memory (float[][] + metadata) and persisted to a
+ * single binary file on disk. For typical Zeppelin deployments (< 50K paragraphs),
+ * brute-force cosine similarity completes in under 50ms.
+ *
+ * <p>Model files are downloaded on first use to {@code zeppelin.search.index.path}
+ * and cached for subsequent starts.
+ */
+public class EmbeddingSearch extends SearchService {
+  private static final Logger LOGGER = LoggerFactory.getLogger(EmbeddingSearch.class);
+
+  private static final String MODEL_NAME = "all-MiniLM-L6-v2";
+  private static final String MODEL_URL =
+      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx";
+  private static final String TOKENIZER_URL =
+      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/tokenizer.json";
+  private static final int EMBEDDING_DIM = 384;
+  private static final int MAX_SEQ_LENGTH = 256;
+  private static final int MAX_RESULTS = 20;
+  private static final float MIN_SIMILARITY = 0.25f;
+  private static final int MAX_TEXT_LENGTH = 1500;
+  private static final int SNIPPET_LENGTH = 150;

Review Comment:
   `SNIPPET_LENGTH` is declared but never used, which makes it unclear what the intended snippet behavior is (and can confuse future maintenance). Either remove it or use it consistently when generating `snippet`/`text` output for the UI.
   



##########
zeppelin-web-angular/tsconfig.base.json:
##########
@@ -12,6 +12,8 @@
     "outDir": "./dist/out-tsc",
     "sourceMap": true,
     "strict": true,
+    "noImplicitAny": false,
+    "skipLibCheck": true,

Review Comment:
   Disabling `noImplicitAny` and enabling `skipLibCheck` in the base tsconfig weakens type-safety for the entire Angular app and can mask real typing issues. It’s safer to keep strict settings and address the specific type errors locally (or confine overrides to a narrower tsconfig used only where needed).



##########
zeppelin-zengine/src/main/java/org/apache/zeppelin/search/EmbeddingSearch.java:
##########
@@ -0,0 +1,724 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zeppelin.search;
+
+import ai.djl.huggingface.tokenizers.Encoding;
+import ai.djl.huggingface.tokenizers.HuggingFaceTokenizer;
+import com.google.common.collect.ImmutableMap;
+import ai.onnxruntime.OnnxTensor;
+import ai.onnxruntime.OrtEnvironment;
+import ai.onnxruntime.OrtException;
+import ai.onnxruntime.OrtSession;
+
+import java.io.BufferedInputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.LongBuffer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.annotation.PreDestroy;
+import jakarta.inject.Inject;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.zeppelin.conf.ZeppelinConfiguration;
+import org.apache.zeppelin.interpreter.InterpreterResult;
+import org.apache.zeppelin.interpreter.InterpreterResultMessage;
+import org.apache.zeppelin.notebook.Note;
+import org.apache.zeppelin.notebook.Notebook;
+import org.apache.zeppelin.notebook.Paragraph;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Semantic search for Zeppelin notebooks using ONNX-based sentence embeddings.
+ *
+ * <p>Uses the all-MiniLM-L6-v2 model to generate 384-dimensional embeddings for each
+ * paragraph's text, title, and output. Queries are embedded with the same model and
+ * matched via cosine similarity, enabling natural language search like
+ * "yesterday's spend query" to find {@code WHERE date = current_date - 1}.
+ *
+ * <p>The embedding index is held in memory (float[][] + metadata) and persisted to a
+ * single binary file on disk. For typical Zeppelin deployments (< 50K paragraphs),
+ * brute-force cosine similarity completes in under 50ms.
+ *
+ * <p>Model files are downloaded on first use to {@code zeppelin.search.index.path}
+ * and cached for subsequent starts.
+ */
+public class EmbeddingSearch extends SearchService {
+  private static final Logger LOGGER = LoggerFactory.getLogger(EmbeddingSearch.class);
+
+  private static final String MODEL_NAME = "all-MiniLM-L6-v2";
+  private static final String MODEL_URL =
+      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx";
+  private static final String TOKENIZER_URL =
+      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/tokenizer.json";
+  private static final int EMBEDDING_DIM = 384;
+  private static final int MAX_SEQ_LENGTH = 256;
+  private static final int MAX_RESULTS = 20;
+  private static final float MIN_SIMILARITY = 0.25f;
+  private static final int MAX_TEXT_LENGTH = 1500;
+  private static final int SNIPPET_LENGTH = 150;
+
+  static final String ID_FIELD = "id";
+  private static final String PARAGRAPH = "paragraph";
+  /** Regex to extract qualified table names from SQL (e.g. schema.table). */
+  private static final Pattern TABLE_RE =
+      Pattern.compile("(?:FROM|JOIN)\\s+([a-zA-Z_]\\w*\\.[a-zA-Z_]\\w*)", Pattern.CASE_INSENSITIVE);
+  private static final float TABLE_BOOST = 0.05f;
+
+  private final Notebook notebook;
+  private final Path indexPath;
+
+  // ONNX inference
+  private OrtEnvironment ortEnv;
+  private OrtSession ortSession;
+  private HuggingFaceTokenizer tokenizer;
+
+  // In-memory vector index: docId -> (embedding, metadata)
+  private final ConcurrentHashMap<String, IndexEntry> index = new ConcurrentHashMap<>();
+  private final ReadWriteLock indexLock = new ReentrantReadWriteLock();
+
+  /** A single indexed document (paragraph or note name). */
+  private static class IndexEntry {
+    final float[] embedding;
+    final String noteName;
+    final String text;
+    final String title;
+    final String tables;
+    final String output;
+
+    IndexEntry(float[] embedding, String noteName, String text, String title,
+               String tables, String output) {
+      this.embedding = embedding;
+      this.noteName = noteName;
+      this.text = text;
+      this.title = title;
+      this.tables = tables;
+      this.output = output;
+    }
+  }
+
+  @Inject
+  public EmbeddingSearch(ZeppelinConfiguration zConf, Notebook notebook) throws IOException {
+    super("EmbeddingSearch");
+    this.notebook = notebook;
+    this.indexPath = Paths.get(zConf.getZeppelinSearchIndexPath());
+    Files.createDirectories(indexPath);
+
+    try {
+      initModel();
+    } catch (Exception e) {
+      throw new IOException("Failed to initialize embedding model", e);
+    }
+
+    if (zConf.isIndexRebuild()) {
+      notebook.addInitConsumer(this::addNoteIndex);
+    }
+    loadIndex();
+    this.notebook.addNotebookEventListener(this);
+  }
+
+  /** Package-private constructor for testing without DI. */
+  EmbeddingSearch(ZeppelinConfiguration zConf, Notebook notebook, boolean skipModel)
+      throws IOException {
+    super("EmbeddingSearch");
+    this.notebook = notebook;
+    this.indexPath = Paths.get(zConf.getZeppelinSearchIndexPath());
+    Files.createDirectories(indexPath);
+    if (!skipModel) {
+      try {
+        initModel();
+      } catch (Exception e) {
+        throw new IOException("Failed to initialize embedding model", e);
+      }
+    }
+    if (zConf.isIndexRebuild()) {
+      notebook.addInitConsumer(this::addNoteIndex);
+    }
+    this.notebook.addNotebookEventListener(this);
+  }
+
+  // ---- Model initialization ----
+
+  private void initModel() throws OrtException, IOException {
+    Path modelDir = indexPath.resolve("models").resolve(MODEL_NAME);
+    Files.createDirectories(modelDir);
+
+    Path modelFile = modelDir.resolve("model.onnx");
+    Path tokenizerFile = modelDir.resolve("tokenizer.json");
+
+    if (!Files.exists(modelFile)) {
+      LOGGER.info("Downloading embedding model {} ...", MODEL_NAME);
+      downloadFile(MODEL_URL, modelFile);
+      LOGGER.info("Model downloaded to {}", modelFile);
+    }
+    if (!Files.exists(tokenizerFile)) {
+      LOGGER.info("Downloading tokenizer for {} ...", MODEL_NAME);
+      downloadFile(TOKENIZER_URL, tokenizerFile);
+    }
+
+    ortEnv = OrtEnvironment.getEnvironment();
+    OrtSession.SessionOptions opts = new OrtSession.SessionOptions();
+    opts.setIntraOpNumThreads(Runtime.getRuntime().availableProcessors());
+    ortSession = ortEnv.createSession(modelFile.toString(), opts);
+    tokenizer = HuggingFaceTokenizer.newInstance(tokenizerFile);
+    LOGGER.info("Embedding model loaded: {}, dim={}", MODEL_NAME, EMBEDDING_DIM);
+  }
+
+  private static void downloadFile(String urlStr, Path dest) throws IOException {
+    URL url = new URL(urlStr);
+    try (InputStream in = new BufferedInputStream(url.openStream());
+         FileOutputStream out = new FileOutputStream(dest.toFile())) {
+      byte[] buf = new byte[8192];
+      int n;
+      while ((n = in.read(buf)) != -1) {
+        out.write(buf, 0, n);
+      }
+    }
+  }
+
+  // ---- Embedding computation ----
+
+  /**
+   * Compute a normalized embedding for the given text.
+   * Uses mean pooling over token embeddings with attention mask.
+   */
+  float[] embed(String text) {
+    if (ortSession == null || tokenizer == null) {
+      return new float[EMBEDDING_DIM];
+    }
+    try {
+      Encoding encoding = tokenizer.encode(text, true, true);
+      long[] inputIds = encoding.getIds();
+      long[] attentionMask = encoding.getAttentionMask();
+
+      // Truncate to max sequence length
+      int seqLen = Math.min(inputIds.length, MAX_SEQ_LENGTH);
+      long[] ids = new long[seqLen];
+      long[] mask = new long[seqLen];
+      long[] tokenTypeIds = new long[seqLen];
+      System.arraycopy(inputIds, 0, ids, 0, seqLen);
+      System.arraycopy(attentionMask, 0, mask, 0, seqLen);
+
+      long[] shape = {1, seqLen};
+      OnnxTensor idsTensor = OnnxTensor.createTensor(ortEnv, LongBuffer.wrap(ids), shape);
+      OnnxTensor maskTensor = OnnxTensor.createTensor(ortEnv, LongBuffer.wrap(mask), shape);
+      OnnxTensor typeTensor = OnnxTensor.createTensor(ortEnv, LongBuffer.wrap(tokenTypeIds), shape);
+
+      Map<String, OnnxTensor> inputs = new HashMap<>();
+      inputs.put("input_ids", idsTensor);
+      inputs.put("attention_mask", maskTensor);
+      inputs.put("token_type_ids", typeTensor);
+
+      try (OrtSession.Result result = ortSession.run(inputs)) {
+        // Output shape: [1, seqLen, 384] — mean pool over sequence dim
+        float[][][] output = (float[][][]) result.get(0).getValue();
+        float[] pooled = meanPool(output[0], mask, seqLen);
+        normalize(pooled);
+        return pooled;
+      } finally {
+        idsTensor.close();
+        maskTensor.close();
+        typeTensor.close();
+      }
+    } catch (OrtException e) {
+      LOGGER.error("Embedding failed for text length {}", text.length(), e);
+      return new float[EMBEDDING_DIM];
+    }
+  }
+
+  /** Mean pooling: average token embeddings weighted by attention mask. */
+  private static float[] meanPool(float[][] tokenEmbeddings, long[] mask, int seqLen) {
+    float[] result = new float[EMBEDDING_DIM];
+    float maskSum = 0;
+    for (int i = 0; i < seqLen; i++) {
+      if (mask[i] == 1) {
+        maskSum++;
+        for (int j = 0; j < EMBEDDING_DIM; j++) {
+          result[j] += tokenEmbeddings[i][j];
+        }
+      }
+    }
+    if (maskSum > 0) {
+      for (int j = 0; j < EMBEDDING_DIM; j++) {
+        result[j] /= maskSum;
+      }
+    }
+    return result;
+  }
+
+  /** L2-normalize in place. */
+  private static void normalize(float[] vec) {
+    float norm = 0;
+    for (float v : vec) {
+      norm += v * v;
+    }
+    norm = (float) Math.sqrt(norm);
+    if (norm > 0) {
+      for (int i = 0; i < vec.length; i++) {
+        vec[i] /= norm;
+      }
+    }
+  }
+
+  /** Cosine similarity between two normalized vectors (= dot product). */
+  private static float cosineSimilarity(float[] a, float[] b) {
+    float dot = 0;
+    for (int i = 0; i < a.length; i++) {
+      dot += a[i] * b[i];
+    }
+    return dot;
+  }
+
+  // ---- Text extraction ----
+
+  /**
+   * Strip interpreter prefix like {@code %spark.sql}, {@code %athena} from paragraph text.
+   * Handles both {@code %name\ncode} and {@code %name code} formats.
+   */
+  static String stripInterpreterPrefix(String text) {
+    if (text == null || !text.startsWith("%")) {
+      return text;
+    }
+    // Find end of interpreter directive: first newline or first space after %word
+    int newlineIdx = text.indexOf('\n');
+    if (newlineIdx >= 0) {
+      return text.substring(newlineIdx + 1);
+    }
+    // Single-line: "%interpreter some code" — strip up to first space
+    int spaceIdx = text.indexOf(' ');
+    if (spaceIdx >= 0) {
+      return text.substring(spaceIdx + 1);
+    }
+    // Just "%interpreter" with no content
+    return "";
+  }
+
+  /**
+   * Extract qualified table names (schema.table) from SQL text.
+   */
+  static String extractTables(String text) {
+    if (text == null) {
+      return "";
+    }
+    Set<String> tables = new HashSet<>();
+    Matcher m = TABLE_RE.matcher(text);
+    while (m.find()) {
+      tables.add(m.group(1).toLowerCase());
+    }
+    return String.join(" ", tables);
+  }
+
+  /**
+   * Extract searchable output text from paragraph results (TABLE headers, TEXT).
+   */
+  static String extractOutput(Paragraph p) {
+    InterpreterResult result = p.getReturn();
+    if (result == null) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();
+    for (InterpreterResultMessage msg : result.message()) {
+      if (msg.getType() == InterpreterResult.Type.TEXT
+          || msg.getType() == InterpreterResult.Type.TABLE) {
+        String data = msg.getData();
+        if (StringUtils.isNotBlank(data)) {
+          sb.append(data, 0, Math.min(data.length(), 500));
+          sb.append("\n");
+        }
+      }
+    }
+    return sb.toString().trim();
+  }
+
+  /**
+   * Build a rich text representation of a paragraph for embedding.
+   * Includes code/text, title, table names, and output (table headers, text results).
+   */
+  private String buildParagraphText(String noteName, Paragraph p) {
+    StringBuilder sb = new StringBuilder();
+    if (StringUtils.isNotBlank(noteName)) {
+      sb.append("Notebook: ").append(noteName).append("\n");
+    }
+    if (StringUtils.isNotBlank(p.getTitle())) {
+      sb.append(p.getTitle()).append("\n");
+    }
+    if (StringUtils.isNotBlank(p.getText())) {
+      String text = p.getText();
+      // Strip interpreter prefix (e.g. "%spark.sql", "%athena\n")
+      text = stripInterpreterPrefix(text);
+      // Include extracted table names for better semantic matching
+      String tables = extractTables(text);
+      if (StringUtils.isNotBlank(tables)) {
+        sb.append("Tables: ").append(tables).append("\n");
+      }
+      sb.append(text, 0, Math.min(text.length(), MAX_TEXT_LENGTH));
+    }
+    // Include output for richer semantic matching
+    InterpreterResult result = p.getReturn();
+    if (result != null) {
+      for (InterpreterResultMessage msg : result.message()) {
+        if (msg.getType() == InterpreterResult.Type.TEXT
+            || msg.getType() == InterpreterResult.Type.TABLE) {
+          String data = msg.getData();
+          if (StringUtils.isNotBlank(data)) {
+            sb.append("\n").append(data, 0, Math.min(data.length(), 500));
+          }
+        }
+      }
+    }
+    return sb.toString();
+  }
+
+  // ---- SearchService implementation ----
+
+  @Override
+  public List<Map<String, String>> query(String queryStr) {
+    if (StringUtils.isBlank(queryStr) || index.isEmpty()) {
+      return Collections.emptyList();
+    }
+
+    float[] queryEmbedding = embed(queryStr);
+
+    // Phase 1: find top-N results and discover relevant tables
+    List<Map.Entry<String, Float>> scored = new ArrayList<>();
+    indexLock.readLock().lock();
+    try {
+      for (Map.Entry<String, IndexEntry> entry : index.entrySet()) {
+        float sim = cosineSimilarity(queryEmbedding, entry.getValue().embedding);
+        scored.add(Map.entry(entry.getKey(), sim));
+      }
+    } finally {
+      indexLock.readLock().unlock();
+    }
+    scored.sort((a, b) -> Float.compare(b.getValue(), a.getValue()));
+
+    // Collect tables from top-20 results, weighted by rank
+    Map<String, Float> tableWeights = new HashMap<>();
+    for (int i = 0; i < Math.min(scored.size(), 20); i++) {
+      IndexEntry entry = index.get(scored.get(i).getKey());
+      if (entry != null && StringUtils.isNotBlank(entry.tables)) {
+        float weight = 1.0f / (i + 1);
+        for (String t : entry.tables.split(" ")) {
+          tableWeights.merge(t, weight, Float::sum);
+        }
+      }
+    }
+    // Keep tables with weight > 20% of top table's weight
+    Set<String> relevantTables = new HashSet<>();
+    if (!tableWeights.isEmpty()) {
+      float maxWeight = Collections.max(tableWeights.values());
+      float threshold = maxWeight * 0.2f;
+      tableWeights.forEach((t, w) -> {
+        if (w >= threshold) {
+          relevantTables.add(t);
+        }
+      });
+    }
+
+    // Phase 2: re-score with table boost
+    List<Map<String, String>> results = new ArrayList<>();
+    for (int i = 0; i < scored.size() && results.size() < MAX_RESULTS; i++) {
+      float sim = scored.get(i).getValue();
+      if (sim < MIN_SIMILARITY) {
+        break;
+      }
+      String docId = scored.get(i).getKey();
+      IndexEntry entry = index.get(docId);
+      if (entry == null || StringUtils.isBlank(entry.text)) {
+        continue;
+      }
+      // Boost paragraphs that reference discovered tables
+      if (!relevantTables.isEmpty() && StringUtils.isNotBlank(entry.tables)) {
+        for (String t : entry.tables.split(" ")) {
+          if (relevantTables.contains(t)) {
+            sim += TABLE_BOOST;
+          }
+        }
+      }
+      // Frontend renders: header + "\n\n" + snippet in Monaco editor
+      // snippet = SQL/code (used for language detection too)
+      // header = title + tables + output preview
+      StringBuilder header = new StringBuilder();
+      if (StringUtils.isNotBlank(entry.title)) {
+        header.append(entry.title).append("\n");
+      }
+      if (StringUtils.isNotBlank(entry.tables)) {
+        header.append("📊 ").append(entry.tables).append("\n");
+      }
+      if (StringUtils.isNotBlank(entry.output)) {
+        String out = entry.output;
+        if (out.length() > 300) {
+          out = out.substring(0, 300);
+        }
+        header.append("\n").append(out);
+      }
+      results.add(ImmutableMap.of(
+          "id", docId,
+          "name", entry.noteName != null ? entry.noteName : "",
+          "snippet", entry.text,
+          "text", entry.text,
+          "header", header.toString()));
+    }
+    // Re-sort by boosted score
+    return results;
+  }
+
+  @Override
+  public void addNoteIndex(String noteId) {
+    try {
+      notebook.processNote(noteId, note -> {
+        if (note != null) {
+          indexNote(note);
+        }
+        return null;
+      });
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to add note {} to index", noteId, e);
+    }
+  }
+
+  @Override
+  public void addParagraphIndex(String noteId, String paragraphId) {
+    try {
+      notebook.processNote(noteId, note -> {
+        if (note != null) {
+          Paragraph p = note.getParagraph(paragraphId);
+          if (p != null) {
+            indexParagraph(note.getId(), note.getName(), p);
+          }
+        }
+        return null;
+      });
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to add paragraph {} of note {}", paragraphId, noteId, e);
+    }
+  }
+
+  @Override
+  public void updateNoteIndex(String noteId) {
+    try {
+      notebook.processNote(noteId, note -> {
+        if (note != null) {
+          indexNote(note);
+        }
+        return null;
+      });
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to update note index {}", noteId, e);
+    }
+  }
+
+  @Override
+  public void updateParagraphIndex(String noteId, String paragraphId) {
+    try {
+      notebook.processNote(noteId, note -> {
+        if (note != null) {
+          Paragraph p = note.getParagraph(paragraphId);
+          if (p != null) {
+            indexParagraph(noteId, note.getName(), p);
+          }
+        }
+        return null;
+      });
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to update paragraph {} of note {}", paragraphId, noteId, e);
+    }
+  }
+
+  @Override
+  public void deleteNoteIndex(String noteId) {
+    if (noteId == null) {
+      return;
+    }
+    indexLock.writeLock().lock();
+    try {
+      index.entrySet().removeIf(e -> e.getKey().startsWith(noteId));
+    } finally {
+      indexLock.writeLock().unlock();
+    }
+    try {
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to save index after deleting note {}", noteId, e);
+    }
+  }
+
+  @Override
+  public void deleteParagraphIndex(String noteId, String paragraphId) {
+    if (noteId == null) {
+      return;
+    }
+    String docId = paragraphId != null
+        ? String.join("/", noteId, PARAGRAPH, paragraphId)
+        : noteId;
+    index.remove(docId);
+    try {
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to save index after deleting paragraph {}", docId, e);
+    }
+  }
+
+  @Override
+  @PreDestroy
+  public void close() {
+    super.close();
+    try {
+      if (ortSession != null) {
+        ortSession.close();
+      }
+      if (tokenizer != null) {
+        tokenizer.close();
+      }
+    } catch (OrtException e) {
+      LOGGER.error("Failed to close ONNX session", e);
+    }
+  }
+
+  // ---- Internal indexing ----
+
+  private void indexNote(Note note) {
+    String noteName = note.getName();
+    // Index each paragraph (note name is included in paragraph embedding text)
+    for (Paragraph p : note.getParagraphs()) {
+      indexParagraph(note.getId(), noteName, p);
+    }
+  }
+
+  private void indexParagraph(String noteId, String noteName, Paragraph p) {
+    String text = buildParagraphText(noteName, p);
+    if (StringUtils.isBlank(text)) {
+      return;
+    }
+    float[] emb = embed(text);
+    String docId = String.join("/", noteId, PARAGRAPH, p.getId());
+    String title = p.getTitle() != null ? p.getTitle() : "";
+    String pText = p.getText() != null ? stripInterpreterPrefix(p.getText()) : "";
+    String tables = extractTables(pText);
+    String output = extractOutput(p);
+
+    indexLock.writeLock().lock();
+    try {
+      index.put(docId, new IndexEntry(emb, noteName, pText, title, tables, output));
+    } finally {
+      indexLock.writeLock().unlock();
+    }
+  }
+
+  static String formatId(String noteId, Paragraph p) {
+    if (p != null) {
+      return String.join("/", noteId, PARAGRAPH, p.getId());
+    }
+    return noteId;
+  }
+
+  // ---- Persistence ----
+
+  /**
+   * Save index to a binary file.
+   * Format: [int:version=3][int:count] then for each entry:
+   *   [utf:docId] [utf:noteName] [utf:text] [utf:title] [utf:tables] [utf:output] [float[384]:embedding]
+   */
+  private void saveIndex() throws IOException {
+    Path file = indexPath.resolve("embedding_index.bin");
+    indexLock.readLock().lock();
+    try (DataOutputStream out = new DataOutputStream(new FileOutputStream(file.toFile()))) {
+      out.writeInt(3); // version 3: includes output field
+      out.writeInt(index.size());

Review Comment:
   Index persistence writes directly to `embedding_index.bin` and rewrites the entire index each time `saveIndex()` is called. For large indexes and frequent paragraph updates this can cause significant IO and also risks leaving a corrupted file if the process crashes mid-write. Consider writing to a temp file and atomically moving it into place, and/or batching/debouncing saves.



##########
zeppelin-web/src/app/search/result-list.controller.js:
##########
@@ -21,136 +21,54 @@ function SearchResultCtrl($scope, $routeParams, searchService) {
   $scope.searchTerm = $routeParams.searchTerm;
   let results = searchService.search({'q': $routeParams.searchTerm}).query();
 
+  function detectLang(text) {
+    if (!text) { return ''; }
+    if (/select|insert|create|from|where/i.test(text)) { return 'sql'; }
+    if (/^%(\w*\.)?py/i.test(text)) { return 'python'; }
+    if (/^%md/i.test(text)) { return 'md'; }
+    if (/^%sh/i.test(text)) { return 'sh'; }
+    if (/import |def |class /i.test(text)) { return 'python'; }
+    return '';
+  }
+
   results.$promise.then(function(result) {
     $scope.notes = result.body.map(function(note) {
-      // redirect to notebook when search result is a notebook itself,
-      // not a paragraph
       if (!/\/paragraph\//.test(note.id)) {
         return note;
       }
-
       note.id = note.id.replace('paragraph/', '?paragraph=') +
         '&term=' + $routeParams.searchTerm;
 
+      // Parse header into tables and output
+      let tables = '';
+      let output = '';
+      if (note.header) {
+        note.header.split('\n').forEach(function(line) {
+          if (line.indexOf('📊') === 0) {
+            tables += (tables ? ', ' : '') + line.substring(2).trim();
+          } else if (line.trim()) {

Review Comment:
   `note.header` from `LuceneSearch` can also include `<B>...</B>` highlight markup. The current header parsing appends those lines into `outputText` without stripping, so the UI may display raw `<B>` tags. Consider stripping highlight markup from `note.header` the same way you do for `note.snippet`.



##########
zeppelin-zengine/src/main/java/org/apache/zeppelin/search/EmbeddingSearch.java:
##########
@@ -0,0 +1,724 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zeppelin.search;
+
+import ai.djl.huggingface.tokenizers.Encoding;
+import ai.djl.huggingface.tokenizers.HuggingFaceTokenizer;
+import com.google.common.collect.ImmutableMap;
+import ai.onnxruntime.OnnxTensor;
+import ai.onnxruntime.OrtEnvironment;
+import ai.onnxruntime.OrtException;
+import ai.onnxruntime.OrtSession;
+
+import java.io.BufferedInputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.LongBuffer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.annotation.PreDestroy;
+import jakarta.inject.Inject;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.zeppelin.conf.ZeppelinConfiguration;
+import org.apache.zeppelin.interpreter.InterpreterResult;
+import org.apache.zeppelin.interpreter.InterpreterResultMessage;
+import org.apache.zeppelin.notebook.Note;
+import org.apache.zeppelin.notebook.Notebook;
+import org.apache.zeppelin.notebook.Paragraph;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Semantic search for Zeppelin notebooks using ONNX-based sentence embeddings.
+ *
+ * <p>Uses the all-MiniLM-L6-v2 model to generate 384-dimensional embeddings for each
+ * paragraph's text, title, and output. Queries are embedded with the same model and
+ * matched via cosine similarity, enabling natural language search like
+ * "yesterday's spend query" to find {@code WHERE date = current_date - 1}.
+ *
+ * <p>The embedding index is held in memory (float[][] + metadata) and persisted to a
+ * single binary file on disk. For typical Zeppelin deployments (< 50K paragraphs),
+ * brute-force cosine similarity completes in under 50ms.
+ *
+ * <p>Model files are downloaded on first use to {@code zeppelin.search.index.path}
+ * and cached for subsequent starts.
+ */
+public class EmbeddingSearch extends SearchService {
+  private static final Logger LOGGER = LoggerFactory.getLogger(EmbeddingSearch.class);
+
+  private static final String MODEL_NAME = "all-MiniLM-L6-v2";
+  private static final String MODEL_URL =
+      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx";
+  private static final String TOKENIZER_URL =
+      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/tokenizer.json";
+  private static final int EMBEDDING_DIM = 384;
+  private static final int MAX_SEQ_LENGTH = 256;
+  private static final int MAX_RESULTS = 20;
+  private static final float MIN_SIMILARITY = 0.25f;
+  private static final int MAX_TEXT_LENGTH = 1500;
+  private static final int SNIPPET_LENGTH = 150;
+
+  static final String ID_FIELD = "id";
+  private static final String PARAGRAPH = "paragraph";
+  /** Regex to extract qualified table names from SQL (e.g. schema.table). */
+  private static final Pattern TABLE_RE =
+      Pattern.compile("(?:FROM|JOIN)\\s+([a-zA-Z_]\\w*\\.[a-zA-Z_]\\w*)", Pattern.CASE_INSENSITIVE);
+  private static final float TABLE_BOOST = 0.05f;
+
+  private final Notebook notebook;
+  private final Path indexPath;
+
+  // ONNX inference
+  private OrtEnvironment ortEnv;
+  private OrtSession ortSession;
+  private HuggingFaceTokenizer tokenizer;
+
+  // In-memory vector index: docId -> (embedding, metadata)
+  private final ConcurrentHashMap<String, IndexEntry> index = new ConcurrentHashMap<>();
+  private final ReadWriteLock indexLock = new ReentrantReadWriteLock();
+
+  /** A single indexed document (paragraph or note name). */
+  private static class IndexEntry {
+    final float[] embedding;
+    final String noteName;
+    final String text;
+    final String title;
+    final String tables;
+    final String output;
+
+    IndexEntry(float[] embedding, String noteName, String text, String title,
+               String tables, String output) {
+      this.embedding = embedding;
+      this.noteName = noteName;
+      this.text = text;
+      this.title = title;
+      this.tables = tables;
+      this.output = output;
+    }
+  }
+
+  @Inject
+  public EmbeddingSearch(ZeppelinConfiguration zConf, Notebook notebook) throws IOException {
+    super("EmbeddingSearch");
+    this.notebook = notebook;
+    this.indexPath = Paths.get(zConf.getZeppelinSearchIndexPath());
+    Files.createDirectories(indexPath);
+
+    try {
+      initModel();
+    } catch (Exception e) {
+      throw new IOException("Failed to initialize embedding model", e);
+    }
+
+    if (zConf.isIndexRebuild()) {
+      notebook.addInitConsumer(this::addNoteIndex);
+    }
+    loadIndex();
+    this.notebook.addNotebookEventListener(this);
+  }
+
+  /** Package-private constructor for testing without DI. */
+  EmbeddingSearch(ZeppelinConfiguration zConf, Notebook notebook, boolean skipModel)
+      throws IOException {
+    super("EmbeddingSearch");
+    this.notebook = notebook;
+    this.indexPath = Paths.get(zConf.getZeppelinSearchIndexPath());
+    Files.createDirectories(indexPath);
+    if (!skipModel) {
+      try {
+        initModel();
+      } catch (Exception e) {
+        throw new IOException("Failed to initialize embedding model", e);
+      }
+    }
+    if (zConf.isIndexRebuild()) {
+      notebook.addInitConsumer(this::addNoteIndex);
+    }
+    this.notebook.addNotebookEventListener(this);
+  }
+
+  // ---- Model initialization ----
+
+  private void initModel() throws OrtException, IOException {
+    Path modelDir = indexPath.resolve("models").resolve(MODEL_NAME);
+    Files.createDirectories(modelDir);
+
+    Path modelFile = modelDir.resolve("model.onnx");
+    Path tokenizerFile = modelDir.resolve("tokenizer.json");
+
+    if (!Files.exists(modelFile)) {
+      LOGGER.info("Downloading embedding model {} ...", MODEL_NAME);
+      downloadFile(MODEL_URL, modelFile);
+      LOGGER.info("Model downloaded to {}", modelFile);
+    }
+    if (!Files.exists(tokenizerFile)) {
+      LOGGER.info("Downloading tokenizer for {} ...", MODEL_NAME);
+      downloadFile(TOKENIZER_URL, tokenizerFile);
+    }
+
+    ortEnv = OrtEnvironment.getEnvironment();
+    OrtSession.SessionOptions opts = new OrtSession.SessionOptions();
+    opts.setIntraOpNumThreads(Runtime.getRuntime().availableProcessors());
+    ortSession = ortEnv.createSession(modelFile.toString(), opts);
+    tokenizer = HuggingFaceTokenizer.newInstance(tokenizerFile);
+    LOGGER.info("Embedding model loaded: {}, dim={}", MODEL_NAME, EMBEDDING_DIM);
+  }
+
+  private static void downloadFile(String urlStr, Path dest) throws IOException {
+    URL url = new URL(urlStr);
+    try (InputStream in = new BufferedInputStream(url.openStream());
+         FileOutputStream out = new FileOutputStream(dest.toFile())) {
+      byte[] buf = new byte[8192];

Review Comment:
   `downloadFile` uses `URL.openStream()` without connect/read timeouts, so enabling semantic search can hang startup indefinitely on slow or blocked networks. Consider using a `URLConnection` with explicit timeouts (and ideally retries / clearer error messages).



##########
zeppelin-zengine/src/main/java/org/apache/zeppelin/search/EmbeddingSearch.java:
##########
@@ -0,0 +1,724 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zeppelin.search;
+
+import ai.djl.huggingface.tokenizers.Encoding;
+import ai.djl.huggingface.tokenizers.HuggingFaceTokenizer;
+import com.google.common.collect.ImmutableMap;
+import ai.onnxruntime.OnnxTensor;
+import ai.onnxruntime.OrtEnvironment;
+import ai.onnxruntime.OrtException;
+import ai.onnxruntime.OrtSession;
+
+import java.io.BufferedInputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.LongBuffer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.annotation.PreDestroy;
+import jakarta.inject.Inject;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.zeppelin.conf.ZeppelinConfiguration;
+import org.apache.zeppelin.interpreter.InterpreterResult;
+import org.apache.zeppelin.interpreter.InterpreterResultMessage;
+import org.apache.zeppelin.notebook.Note;
+import org.apache.zeppelin.notebook.Notebook;
+import org.apache.zeppelin.notebook.Paragraph;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Semantic search for Zeppelin notebooks using ONNX-based sentence embeddings.
+ *
+ * <p>Uses the all-MiniLM-L6-v2 model to generate 384-dimensional embeddings for each
+ * paragraph's text, title, and output. Queries are embedded with the same model and
+ * matched via cosine similarity, enabling natural language search like
+ * "yesterday's spend query" to find {@code WHERE date = current_date - 1}.
+ *
+ * <p>The embedding index is held in memory (float[][] + metadata) and persisted to a
+ * single binary file on disk. For typical Zeppelin deployments (< 50K paragraphs),
+ * brute-force cosine similarity completes in under 50ms.
+ *
+ * <p>Model files are downloaded on first use to {@code zeppelin.search.index.path}
+ * and cached for subsequent starts.
+ */
+public class EmbeddingSearch extends SearchService {
+  private static final Logger LOGGER = LoggerFactory.getLogger(EmbeddingSearch.class);
+
+  private static final String MODEL_NAME = "all-MiniLM-L6-v2";
+  private static final String MODEL_URL =
+      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx";
+  private static final String TOKENIZER_URL =
+      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/tokenizer.json";
+  private static final int EMBEDDING_DIM = 384;
+  private static final int MAX_SEQ_LENGTH = 256;
+  private static final int MAX_RESULTS = 20;
+  private static final float MIN_SIMILARITY = 0.25f;
+  private static final int MAX_TEXT_LENGTH = 1500;
+  private static final int SNIPPET_LENGTH = 150;
+
+  static final String ID_FIELD = "id";
+  private static final String PARAGRAPH = "paragraph";
+  /** Regex to extract qualified table names from SQL (e.g. schema.table). */
+  private static final Pattern TABLE_RE =
+      Pattern.compile("(?:FROM|JOIN)\\s+([a-zA-Z_]\\w*\\.[a-zA-Z_]\\w*)", Pattern.CASE_INSENSITIVE);
+  private static final float TABLE_BOOST = 0.05f;
+
+  private final Notebook notebook;
+  private final Path indexPath;
+
+  // ONNX inference
+  private OrtEnvironment ortEnv;
+  private OrtSession ortSession;
+  private HuggingFaceTokenizer tokenizer;
+
+  // In-memory vector index: docId -> (embedding, metadata)
+  private final ConcurrentHashMap<String, IndexEntry> index = new ConcurrentHashMap<>();
+  private final ReadWriteLock indexLock = new ReentrantReadWriteLock();
+
+  /** A single indexed document (paragraph or note name). */
+  private static class IndexEntry {
+    final float[] embedding;
+    final String noteName;
+    final String text;
+    final String title;
+    final String tables;
+    final String output;
+
+    IndexEntry(float[] embedding, String noteName, String text, String title,
+               String tables, String output) {
+      this.embedding = embedding;
+      this.noteName = noteName;
+      this.text = text;
+      this.title = title;
+      this.tables = tables;
+      this.output = output;
+    }
+  }
+
+  @Inject
+  public EmbeddingSearch(ZeppelinConfiguration zConf, Notebook notebook) throws IOException {
+    super("EmbeddingSearch");
+    this.notebook = notebook;
+    this.indexPath = Paths.get(zConf.getZeppelinSearchIndexPath());
+    Files.createDirectories(indexPath);
+
+    try {
+      initModel();
+    } catch (Exception e) {
+      throw new IOException("Failed to initialize embedding model", e);
+    }
+
+    if (zConf.isIndexRebuild()) {
+      notebook.addInitConsumer(this::addNoteIndex);
+    }
+    loadIndex();
+    this.notebook.addNotebookEventListener(this);
+  }
+
+  /** Package-private constructor for testing without DI. */
+  EmbeddingSearch(ZeppelinConfiguration zConf, Notebook notebook, boolean skipModel)
+      throws IOException {
+    super("EmbeddingSearch");
+    this.notebook = notebook;
+    this.indexPath = Paths.get(zConf.getZeppelinSearchIndexPath());
+    Files.createDirectories(indexPath);
+    if (!skipModel) {
+      try {
+        initModel();
+      } catch (Exception e) {
+        throw new IOException("Failed to initialize embedding model", e);
+      }
+    }
+    if (zConf.isIndexRebuild()) {
+      notebook.addInitConsumer(this::addNoteIndex);
+    }
+    this.notebook.addNotebookEventListener(this);
+  }
+
+  // ---- Model initialization ----
+
+  private void initModel() throws OrtException, IOException {
+    Path modelDir = indexPath.resolve("models").resolve(MODEL_NAME);
+    Files.createDirectories(modelDir);
+
+    Path modelFile = modelDir.resolve("model.onnx");
+    Path tokenizerFile = modelDir.resolve("tokenizer.json");
+
+    if (!Files.exists(modelFile)) {
+      LOGGER.info("Downloading embedding model {} ...", MODEL_NAME);
+      downloadFile(MODEL_URL, modelFile);
+      LOGGER.info("Model downloaded to {}", modelFile);
+    }
+    if (!Files.exists(tokenizerFile)) {
+      LOGGER.info("Downloading tokenizer for {} ...", MODEL_NAME);
+      downloadFile(TOKENIZER_URL, tokenizerFile);
+    }
+
+    ortEnv = OrtEnvironment.getEnvironment();
+    OrtSession.SessionOptions opts = new OrtSession.SessionOptions();
+    opts.setIntraOpNumThreads(Runtime.getRuntime().availableProcessors());
+    ortSession = ortEnv.createSession(modelFile.toString(), opts);
+    tokenizer = HuggingFaceTokenizer.newInstance(tokenizerFile);
+    LOGGER.info("Embedding model loaded: {}, dim={}", MODEL_NAME, EMBEDDING_DIM);
+  }
+
+  private static void downloadFile(String urlStr, Path dest) throws IOException {
+    URL url = new URL(urlStr);
+    try (InputStream in = new BufferedInputStream(url.openStream());
+         FileOutputStream out = new FileOutputStream(dest.toFile())) {
+      byte[] buf = new byte[8192];
+      int n;
+      while ((n = in.read(buf)) != -1) {
+        out.write(buf, 0, n);
+      }
+    }
+  }
+
+  // ---- Embedding computation ----
+
+  /**
+   * Compute a normalized embedding for the given text.
+   * Uses mean pooling over token embeddings with attention mask.
+   */
+  float[] embed(String text) {
+    if (ortSession == null || tokenizer == null) {
+      return new float[EMBEDDING_DIM];
+    }
+    try {
+      Encoding encoding = tokenizer.encode(text, true, true);
+      long[] inputIds = encoding.getIds();
+      long[] attentionMask = encoding.getAttentionMask();
+
+      // Truncate to max sequence length
+      int seqLen = Math.min(inputIds.length, MAX_SEQ_LENGTH);
+      long[] ids = new long[seqLen];
+      long[] mask = new long[seqLen];
+      long[] tokenTypeIds = new long[seqLen];
+      System.arraycopy(inputIds, 0, ids, 0, seqLen);
+      System.arraycopy(attentionMask, 0, mask, 0, seqLen);
+
+      long[] shape = {1, seqLen};
+      OnnxTensor idsTensor = OnnxTensor.createTensor(ortEnv, LongBuffer.wrap(ids), shape);
+      OnnxTensor maskTensor = OnnxTensor.createTensor(ortEnv, LongBuffer.wrap(mask), shape);
+      OnnxTensor typeTensor = OnnxTensor.createTensor(ortEnv, LongBuffer.wrap(tokenTypeIds), shape);
+
+      Map<String, OnnxTensor> inputs = new HashMap<>();
+      inputs.put("input_ids", idsTensor);
+      inputs.put("attention_mask", maskTensor);
+      inputs.put("token_type_ids", typeTensor);
+
+      try (OrtSession.Result result = ortSession.run(inputs)) {
+        // Output shape: [1, seqLen, 384] — mean pool over sequence dim
+        float[][][] output = (float[][][]) result.get(0).getValue();
+        float[] pooled = meanPool(output[0], mask, seqLen);
+        normalize(pooled);
+        return pooled;
+      } finally {
+        idsTensor.close();
+        maskTensor.close();
+        typeTensor.close();
+      }
+    } catch (OrtException e) {
+      LOGGER.error("Embedding failed for text length {}", text.length(), e);
+      return new float[EMBEDDING_DIM];
+    }
+  }
+
+  /** Mean pooling: average token embeddings weighted by attention mask. */
+  private static float[] meanPool(float[][] tokenEmbeddings, long[] mask, int seqLen) {
+    float[] result = new float[EMBEDDING_DIM];
+    float maskSum = 0;
+    for (int i = 0; i < seqLen; i++) {
+      if (mask[i] == 1) {
+        maskSum++;
+        for (int j = 0; j < EMBEDDING_DIM; j++) {
+          result[j] += tokenEmbeddings[i][j];
+        }
+      }
+    }
+    if (maskSum > 0) {
+      for (int j = 0; j < EMBEDDING_DIM; j++) {
+        result[j] /= maskSum;
+      }
+    }
+    return result;
+  }
+
+  /** L2-normalize in place. */
+  private static void normalize(float[] vec) {
+    float norm = 0;
+    for (float v : vec) {
+      norm += v * v;
+    }
+    norm = (float) Math.sqrt(norm);
+    if (norm > 0) {
+      for (int i = 0; i < vec.length; i++) {
+        vec[i] /= norm;
+      }
+    }
+  }
+
+  /** Cosine similarity between two normalized vectors (= dot product). */
+  private static float cosineSimilarity(float[] a, float[] b) {
+    float dot = 0;
+    for (int i = 0; i < a.length; i++) {
+      dot += a[i] * b[i];
+    }
+    return dot;
+  }
+
+  // ---- Text extraction ----
+
+  /**
+   * Strip interpreter prefix like {@code %spark.sql}, {@code %athena} from paragraph text.
+   * Handles both {@code %name\ncode} and {@code %name code} formats.
+   */
+  static String stripInterpreterPrefix(String text) {
+    if (text == null || !text.startsWith("%")) {
+      return text;
+    }
+    // Find end of interpreter directive: first newline or first space after %word
+    int newlineIdx = text.indexOf('\n');
+    if (newlineIdx >= 0) {
+      return text.substring(newlineIdx + 1);
+    }
+    // Single-line: "%interpreter some code" — strip up to first space
+    int spaceIdx = text.indexOf(' ');
+    if (spaceIdx >= 0) {
+      return text.substring(spaceIdx + 1);
+    }
+    // Just "%interpreter" with no content
+    return "";
+  }
+
+  /**
+   * Extract qualified table names (schema.table) from SQL text.
+   */
+  static String extractTables(String text) {
+    if (text == null) {
+      return "";
+    }
+    Set<String> tables = new HashSet<>();
+    Matcher m = TABLE_RE.matcher(text);
+    while (m.find()) {
+      tables.add(m.group(1).toLowerCase());
+    }
+    return String.join(" ", tables);
+  }
+
+  /**
+   * Extract searchable output text from paragraph results (TABLE headers, TEXT).
+   */
+  static String extractOutput(Paragraph p) {
+    InterpreterResult result = p.getReturn();
+    if (result == null) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();
+    for (InterpreterResultMessage msg : result.message()) {
+      if (msg.getType() == InterpreterResult.Type.TEXT
+          || msg.getType() == InterpreterResult.Type.TABLE) {
+        String data = msg.getData();
+        if (StringUtils.isNotBlank(data)) {
+          sb.append(data, 0, Math.min(data.length(), 500));
+          sb.append("\n");
+        }
+      }
+    }
+    return sb.toString().trim();
+  }
+
+  /**
+   * Build a rich text representation of a paragraph for embedding.
+   * Includes code/text, title, table names, and output (table headers, text results).
+   */
+  private String buildParagraphText(String noteName, Paragraph p) {
+    StringBuilder sb = new StringBuilder();
+    if (StringUtils.isNotBlank(noteName)) {
+      sb.append("Notebook: ").append(noteName).append("\n");
+    }
+    if (StringUtils.isNotBlank(p.getTitle())) {
+      sb.append(p.getTitle()).append("\n");
+    }
+    if (StringUtils.isNotBlank(p.getText())) {
+      String text = p.getText();
+      // Strip interpreter prefix (e.g. "%spark.sql", "%athena\n")
+      text = stripInterpreterPrefix(text);
+      // Include extracted table names for better semantic matching
+      String tables = extractTables(text);
+      if (StringUtils.isNotBlank(tables)) {
+        sb.append("Tables: ").append(tables).append("\n");
+      }
+      sb.append(text, 0, Math.min(text.length(), MAX_TEXT_LENGTH));
+    }
+    // Include output for richer semantic matching
+    InterpreterResult result = p.getReturn();
+    if (result != null) {
+      for (InterpreterResultMessage msg : result.message()) {
+        if (msg.getType() == InterpreterResult.Type.TEXT
+            || msg.getType() == InterpreterResult.Type.TABLE) {
+          String data = msg.getData();
+          if (StringUtils.isNotBlank(data)) {
+            sb.append("\n").append(data, 0, Math.min(data.length(), 500));
+          }
+        }
+      }
+    }
+    return sb.toString();
+  }
+
+  // ---- SearchService implementation ----
+
+  @Override
+  public List<Map<String, String>> query(String queryStr) {
+    if (StringUtils.isBlank(queryStr) || index.isEmpty()) {
+      return Collections.emptyList();
+    }
+
+    float[] queryEmbedding = embed(queryStr);
+
+    // Phase 1: find top-N results and discover relevant tables
+    List<Map.Entry<String, Float>> scored = new ArrayList<>();
+    indexLock.readLock().lock();
+    try {
+      for (Map.Entry<String, IndexEntry> entry : index.entrySet()) {
+        float sim = cosineSimilarity(queryEmbedding, entry.getValue().embedding);
+        scored.add(Map.entry(entry.getKey(), sim));
+      }
+    } finally {
+      indexLock.readLock().unlock();
+    }
+    scored.sort((a, b) -> Float.compare(b.getValue(), a.getValue()));
+
+    // Collect tables from top-20 results, weighted by rank
+    Map<String, Float> tableWeights = new HashMap<>();
+    for (int i = 0; i < Math.min(scored.size(), 20); i++) {
+      IndexEntry entry = index.get(scored.get(i).getKey());
+      if (entry != null && StringUtils.isNotBlank(entry.tables)) {
+        float weight = 1.0f / (i + 1);
+        for (String t : entry.tables.split(" ")) {
+          tableWeights.merge(t, weight, Float::sum);
+        }
+      }
+    }
+    // Keep tables with weight > 20% of top table's weight
+    Set<String> relevantTables = new HashSet<>();
+    if (!tableWeights.isEmpty()) {
+      float maxWeight = Collections.max(tableWeights.values());
+      float threshold = maxWeight * 0.2f;
+      tableWeights.forEach((t, w) -> {
+        if (w >= threshold) {
+          relevantTables.add(t);
+        }
+      });
+    }
+
+    // Phase 2: re-score with table boost
+    List<Map<String, String>> results = new ArrayList<>();
+    for (int i = 0; i < scored.size() && results.size() < MAX_RESULTS; i++) {
+      float sim = scored.get(i).getValue();
+      if (sim < MIN_SIMILARITY) {
+        break;
+      }
+      String docId = scored.get(i).getKey();
+      IndexEntry entry = index.get(docId);
+      if (entry == null || StringUtils.isBlank(entry.text)) {
+        continue;
+      }
+      // Boost paragraphs that reference discovered tables
+      if (!relevantTables.isEmpty() && StringUtils.isNotBlank(entry.tables)) {
+        for (String t : entry.tables.split(" ")) {
+          if (relevantTables.contains(t)) {
+            sim += TABLE_BOOST;
+          }
+        }
+      }
+      // Frontend renders: header + "\n\n" + snippet in Monaco editor
+      // snippet = SQL/code (used for language detection too)
+      // header = title + tables + output preview
+      StringBuilder header = new StringBuilder();
+      if (StringUtils.isNotBlank(entry.title)) {
+        header.append(entry.title).append("\n");
+      }
+      if (StringUtils.isNotBlank(entry.tables)) {
+        header.append("📊 ").append(entry.tables).append("\n");
+      }
+      if (StringUtils.isNotBlank(entry.output)) {
+        String out = entry.output;
+        if (out.length() > 300) {
+          out = out.substring(0, 300);
+        }
+        header.append("\n").append(out);
+      }
+      results.add(ImmutableMap.of(
+          "id", docId,
+          "name", entry.noteName != null ? entry.noteName : "",
+          "snippet", entry.text,
+          "text", entry.text,
+          "header", header.toString()));
+    }
+    // Re-sort by boosted score
+    return results;
+  }
+
+  @Override
+  public void addNoteIndex(String noteId) {
+    try {
+      notebook.processNote(noteId, note -> {
+        if (note != null) {
+          indexNote(note);
+        }
+        return null;
+      });
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to add note {} to index", noteId, e);
+    }
+  }
+
+  @Override
+  public void addParagraphIndex(String noteId, String paragraphId) {
+    try {
+      notebook.processNote(noteId, note -> {
+        if (note != null) {
+          Paragraph p = note.getParagraph(paragraphId);
+          if (p != null) {
+            indexParagraph(note.getId(), note.getName(), p);
+          }
+        }
+        return null;
+      });
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to add paragraph {} of note {}", paragraphId, noteId, e);
+    }
+  }
+
+  @Override
+  public void updateNoteIndex(String noteId) {
+    try {
+      notebook.processNote(noteId, note -> {
+        if (note != null) {
+          indexNote(note);
+        }
+        return null;
+      });
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to update note index {}", noteId, e);
+    }
+  }
+
+  @Override
+  public void updateParagraphIndex(String noteId, String paragraphId) {
+    try {
+      notebook.processNote(noteId, note -> {
+        if (note != null) {
+          Paragraph p = note.getParagraph(paragraphId);
+          if (p != null) {
+            indexParagraph(noteId, note.getName(), p);
+          }
+        }
+        return null;
+      });
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to update paragraph {} of note {}", paragraphId, noteId, e);
+    }
+  }
+
+  @Override
+  public void deleteNoteIndex(String noteId) {
+    if (noteId == null) {
+      return;
+    }
+    indexLock.writeLock().lock();
+    try {
+      index.entrySet().removeIf(e -> e.getKey().startsWith(noteId));
+    } finally {
+      indexLock.writeLock().unlock();
+    }
+    try {
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to save index after deleting note {}", noteId, e);
+    }
+  }
+
+  @Override
+  public void deleteParagraphIndex(String noteId, String paragraphId) {
+    if (noteId == null) {
+      return;
+    }
+    String docId = paragraphId != null
+        ? String.join("/", noteId, PARAGRAPH, paragraphId)
+        : noteId;
+    index.remove(docId);
+    try {
+      saveIndex();
+    } catch (IOException e) {
+      LOGGER.error("Failed to save index after deleting paragraph {}", docId, e);
+    }
+  }
+
+  @Override
+  @PreDestroy
+  public void close() {
+    super.close();
+    try {
+      if (ortSession != null) {
+        ortSession.close();
+      }
+      if (tokenizer != null) {
+        tokenizer.close();
+      }
+    } catch (OrtException e) {
+      LOGGER.error("Failed to close ONNX session", e);
+    }
+  }
+
+  // ---- Internal indexing ----
+
+  private void indexNote(Note note) {
+    String noteName = note.getName();
+    // Index each paragraph (note name is included in paragraph embedding text)
+    for (Paragraph p : note.getParagraphs()) {
+      indexParagraph(note.getId(), noteName, p);
+    }
+  }

Review Comment:
   `EmbeddingSearch` currently indexes only paragraphs. Unlike `LuceneSearch`, it doesn’t add a note-level document keyed by just `<noteId>` for matching notebook names. This breaks the existing UI behavior that treats non-`/paragraph/` ids as notebook-level hits (and is part of the current SearchService behavior). Consider adding a note-name entry to the index (and keeping it updated/deleted).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@zeppelin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org