feat(elasticsearch): improve REST commands and visual filters

This commit is contained in:
zipg authored and GitHub committed 2026-07-16 16:31:15 +08:00
1 parent bfcd7593ee
commit da7eedb49c
15 files changed
+1151 -115

No files matched your search

@@ -220,6 +220,54 @@ describe("splitSqlStatementRanges", () => {
});
describe("statementRangeAtCursor", () => {
it("splits Elasticsearch REST requests without semicolons", () => {
const sql = `# node information
GET /_nodes/stats/jvm?pretty
// search orders
POST /orders/_search
{
"query": { "match_all": {} }
}
HEAD /orders`;
expect(rangeSqlTexts(splitSqlStatementRanges(sql, "elasticsearch"))).toEqual(["GET /_nodes/stats/jvm?pretty", 'POST /orders/_search\n{\n "query": { "match_all": {} }\n}', "HEAD /orders"]);
expect(hasMultipleExecutionTargets(sql, "elasticsearch")).toBe(true);
});
it("targets the Elasticsearch request following a comment", () => {
const sql = "# JVM statistics\nGET /_nodes/stats/jvm?pretty\n\nGET /_cluster/health";
expect(statementRangeAtCursor(sql, indexOf(sql, "JVM"), "elasticsearch")?.sql).toBe("GET /_nodes/stats/jvm?pretty");
expect(statementRangeAtCursor(sql, indexOf(sql, "cluster"), "elasticsearch")?.sql).toBe("GET /_cluster/health");
});
it("splits and targets Elasticsearch requests after block comments", () => {
const sql = `/* node information
including JVM details */
GET /_nodes/stats/jvm?pretty
/* cluster information */
GET /_cluster/health`;
expect(rangeSqlTexts(splitSqlStatementRanges(sql, "elasticsearch"))).toEqual(["GET /_nodes/stats/jvm?pretty", "GET /_cluster/health"]);
expect(statementRangeAtCursor(sql, indexOf(sql, "JVM details"), "elasticsearch")?.sql).toBe("GET /_nodes/stats/jvm?pretty");
expect(statementRangeAtCursor(sql, indexOf(sql, "cluster information"), "elasticsearch")?.sql).toBe("GET /_cluster/health");
});
it("ignores request-looking lines inside Elasticsearch block comments", () => {
const sql = `GET /_cluster/health
/* disabled cleanup
DELETE /important-index
*/
GET /_cat/indices`;
expect(rangeSqlTexts(splitSqlStatementRanges(sql, "elasticsearch"))).toEqual(["GET /_cluster/health", "GET /_cat/indices"]);
expect(statementRangeAtCursor(sql, indexOf(sql, "important-index"), "elasticsearch")?.sql).toBe("GET /_cat/indices");
});
it("returns the first statement when the cursor is inside it", () => {
const sql = "SELECT 1;\nSELECT 2;";
const pos = indexOf(sql, "1");
@@ -840,7 +888,7 @@ describe("hasMultipleExecutionTargets", () => {
});
describe("supportsExecutionTargetPicker", () => {
it("enables the picker for SQL database connections and Redis", () => {
it("enables the picker for SQL database connections, Redis, and Elasticsearch", () => {
expect(supportsExecutionTargetPicker("mysql")).toBe(true);
expect(supportsExecutionTargetPicker("postgres")).toBe(true);
expect(supportsExecutionTargetPicker("sqlserver")).toBe(true);
@@ -848,7 +896,7 @@ describe("supportsExecutionTargetPicker", () => {
expect(supportsExecutionTargetPicker("jdbc")).toBe(true);
expect(supportsExecutionTargetPicker("redis")).toBe(true);
expect(supportsExecutionTargetPicker("mongodb")).toBe(false);
expect(supportsExecutionTargetPicker("elasticsearch")).toBe(false);
expect(supportsExecutionTargetPicker("elasticsearch")).toBe(true);
expect(supportsExecutionTargetPicker("qdrant")).toBe(false);
expect(supportsExecutionTargetPicker("milvus")).toBe(false);
expect(supportsExecutionTargetPicker("weaviate")).toBe(false);
@@ -5,6 +5,8 @@ import { formatMongoShellLiteral } from "@/lib/mongo/mongoDocumentValues";
export type DocumentStoreKind = "mongodb" | "elasticsearch";
export type DocumentFilterMode = "equals" | "not-equals" | "like" | "not-like" | "greater-than" | "less-than" | "is-null" | "is-not-null";
export type ElasticsearchBoolClause = "filter" | "must" | "should" | "must_not";
export type ElasticsearchQueryType = "term" | "terms" | "match" | "match_phrase" | "wildcard" | "range_gt" | "range_gte" | "range_lt" | "range_lte" | "exists";
export type DocumentFilterRule = {
id: string;
@@ -12,6 +14,8 @@ export type DocumentFilterRule = {
mode: DocumentFilterMode;
rawValue: string;
conjunction: "AND" | "OR";
elasticsearchClause?: ElasticsearchBoolClause;
elasticsearchQueryType?: ElasticsearchQueryType;
};
export type DocumentStoreQueryPreviewOptions = {
@@ -42,6 +46,22 @@ export const documentFilterModeOptions: Array<{ value: DocumentFilterMode; label
{ value: "is-not-null", labelKey: "grid.filterBuilderIsNotNull" },
];
export const elasticsearchBoolClauseOptions: ElasticsearchBoolClause[] = ["filter", "must", "should", "must_not"];
const ELASTICSEARCH_TEXT_QUERY_TYPES: ElasticsearchQueryType[] = ["match", "match_phrase", "term", "wildcard", "exists"];
const ELASTICSEARCH_KEYWORD_QUERY_TYPES: ElasticsearchQueryType[] = ["term", "terms", "wildcard", "exists"];
const ELASTICSEARCH_RANGE_QUERY_TYPES: ElasticsearchQueryType[] = ["term", "terms", "range_gt", "range_gte", "range_lt", "range_lte", "exists"];
const ELASTICSEARCH_BOOLEAN_QUERY_TYPES: ElasticsearchQueryType[] = ["term", "exists"];
export function elasticsearchQueryTypeOptions(fieldType?: string): ElasticsearchQueryType[] {
const normalized = fieldType?.trim().toLowerCase() ?? "";
if (normalized === "text" || normalized === "search_as_you_type") return ELASTICSEARCH_TEXT_QUERY_TYPES;
if (normalized === "keyword" || normalized === "constant_keyword" || normalized === "wildcard") return ELASTICSEARCH_KEYWORD_QUERY_TYPES;
if (/^(?:byte|short|integer|long|unsigned_long|half_float|float|double|scaled_float|date|date_nanos|ip)$/.test(normalized)) return ELASTICSEARCH_RANGE_QUERY_TYPES;
if (normalized === "boolean") return ELASTICSEARCH_BOOLEAN_QUERY_TYPES;
return ["term", "terms", "match", "match_phrase", "wildcard", "range_gt", "range_gte", "range_lt", "range_lte", "exists"];
}
const mongoDocumentProvider: DocumentStoreProvider = {
kind: "mongodb",
filterInputLabel: "find",
@@ -90,6 +110,8 @@ export function defaultDocumentFilterRule(id: string, fieldName = ""): DocumentF
mode: "equals",
rawValue: "",
conjunction: "AND",
elasticsearchClause: "filter",
elasticsearchQueryType: "term",
};
}
@@ -97,6 +119,68 @@ export function documentFilterModeNeedsValue(mode: DocumentFilterMode): boolean
return mode !== "is-null" && mode !== "is-not-null";
}
export function elasticsearchQueryTypeNeedsValue(queryType: ElasticsearchQueryType | undefined): boolean {
return queryType !== "exists";
}
export function buildElasticsearchQueryFromRules(rules: readonly DocumentFilterRule[]): Record<string, unknown> | null {
const boolQuery: Partial<Record<ElasticsearchBoolClause, Record<string, unknown>[]>> & { minimum_should_match?: number } = {};
for (const rule of rules) {
const query = buildElasticsearchRuleQuery(rule);
if (!query) continue;
const clause = rule.elasticsearchClause ?? "filter";
(boolQuery[clause] ??= []).push(query);
}
if (Object.keys(boolQuery).length === 0) return null;
if (boolQuery.should?.length) boolQuery.minimum_should_match = 1;
return { bool: boolQuery };
}
function buildElasticsearchRuleQuery(rule: DocumentFilterRule): Record<string, unknown> | null {
const field = rule.fieldName.trim();
const queryType = rule.elasticsearchQueryType ?? "term";
if (!field || (elasticsearchQueryTypeNeedsValue(queryType) && !rule.rawValue.trim())) return null;
if (queryType === "exists") return { exists: { field } };
const value = parseDocumentFilterValue(rule.rawValue, { kind: "elasticsearch" });
switch (queryType) {
case "term":
return { term: { [field]: value } };
case "terms": {
const values = Array.isArray(value)
? value
: rule.rawValue
.split(",")
.map((item) => item.trim())
.filter(Boolean)
.map((item) => parseDocumentFilterValue(item, { kind: "elasticsearch" }));
return values.length ? { terms: { [field]: values } } : null;
}
case "match":
return { match: { [field]: value } };
case "match_phrase":
return { match_phrase: { [field]: value } };
case "wildcard":
// Keep the compact form compatible with every supported Elasticsearch 7.x release.
// `case_insensitive` was only added in Elasticsearch 7.10.
return { wildcard: { [field]: String(value) } };
case "range_gt":
return { range: { [field]: { gt: value } } };
case "range_gte":
return { range: { [field]: { gte: value } } };
case "range_lt":
return { range: { [field]: { lt: value } } };
case "range_lte":
return { range: { [field]: { lte: value } } };
}
}
export function elasticsearchStructuredFilter(query: Record<string, unknown> | null): Record<string, unknown> | null {
return query ? { $esQuery: query } : null;
}
type DocumentFilterParseOptions = {
kind?: DocumentStoreKind;
};
@@ -298,6 +382,9 @@ function translateDocumentFilterToElasticsearchQuery(filter: Record<string, unkn
} else if (key === "$or") {
const should = translateLogicalArrayToElasticsearch("$or", value);
if (should.length > 0) filters.push({ bool: { should, minimum_should_match: 1 } });
} else if (key === "$esQuery") {
if (!isPlainRecord(value)) throw new Error("$esQuery must be an object");
filters.push(value);
} else {
filters.push(translateFieldFilterToElasticsearchQuery(key, value));
}
@@ -22,14 +22,21 @@ export interface ElasticsearchCompletionInput {
indices?: string[];
}
const HTTP_METHODS = ["GET", "POST", "PUT", "DELETE"] as const;
const HTTP_METHODS = ["GET", "POST", "PUT", "DELETE", "HEAD"] as const;
const ROOT_ENDPOINTS = [
{ label: "/_search", apply: "_search", method: "GET", detail: "Search all indices" },
{ label: "/_cat/indices", apply: "_cat/indices?v", method: "GET", detail: "List indices" },
{ label: "/_cluster/health", apply: "_cluster/health", method: "GET", detail: "Cluster health" },
{ label: "/_cluster/stats", apply: "_cluster/stats?pretty", method: "GET", detail: "Cluster statistics" },
{ label: "/_nodes/stats", apply: "_nodes/stats?pretty", method: "GET", detail: "Node statistics" },
{ label: "/_cat/nodes", apply: "_cat/nodes?v", method: "GET", detail: "List nodes" },
{ label: "/_cat/shards", apply: "_cat/shards?v", method: "GET", detail: "List shards" },
{ label: "/_cat/thread_pool", apply: "_cat/thread_pool?v", method: "GET", detail: "Thread pools" },
{ label: "/_aliases", apply: "_aliases", method: "GET", detail: "List aliases" },
{ label: "/_bulk", apply: "_bulk\n", method: "POST", detail: "Bulk operations" },
{ label: "/_msearch", apply: "_msearch\n", method: "POST", detail: "Multi search" },
{ label: "/_tasks", apply: "_tasks?detailed=true", method: "GET", detail: "List tasks" },
{ label: "/_count", apply: "_count", method: "GET", detail: "Count documents" },
];
@@ -86,44 +93,47 @@ export function getElasticsearchCompletionContext(text: string, cursor: number):
const firstLineEnd = text.indexOf("\n");
const firstLineLimit = firstLineEnd >= 0 ? firstLineEnd : text.length;
// Check the current line before falling back to JSON mode so completion also
// works after leading comments and on subsequent REST requests.
const methodMatch = /^\s*([A-Za-z]*)$/.exec(beforeCursorOnLine);
if (methodMatch) {
const leadingWhitespace = beforeCursorOnLine.length - beforeCursorOnLine.trimStart().length;
return {
mode: "method",
prefix: methodMatch[1] ?? "",
from: lineStart + leadingWhitespace,
};
}
const commandMatch = /^\s*([A-Za-z]+)\s+(\S*)/.exec(text.slice(lineStart, currentLineEnd));
if (commandMatch) {
const method = commandMatch[1]?.toUpperCase();
const path = commandMatch[2] ?? "";
const pathStart = lineStart + commandMatch[0].length - path.length;
const pathCursor = Math.max(0, safeCursor - pathStart);
const boundedPathCursor = Math.min(pathCursor, path.length);
const beforePathCursor = path.slice(0, boundedPathCursor);
const segmentStartInPath = beforePathCursor.lastIndexOf("/") + 1;
const prefix = beforePathCursor.slice(segmentStartInPath);
const segmentIndex = beforePathCursor.slice(0, segmentStartInPath).split("/").filter(Boolean).length;
return {
mode: "path",
prefix,
from: pathStart + segmentStartInPath,
method,
path,
segmentIndex,
};
}
if (lineStart > 0 || safeCursor > firstLineLimit || looksLikeJsonBody(text, safeCursor)) {
const jsonPrefix = readJsonPrefix(text, safeCursor);
return { mode: "json", prefix: jsonPrefix.prefix, from: jsonPrefix.from };
}
const methodMatch = /^([A-Za-z]*)$/.exec(beforeCursorOnLine);
if (methodMatch) {
return {
mode: "method",
prefix: methodMatch[1] ?? "",
from: lineStart,
};
}
const commandMatch = /^([A-Za-z]+)\s+(\S*)/.exec(text.slice(lineStart, currentLineEnd));
if (!commandMatch) {
const prefix = readWordPrefix(text, safeCursor);
return { mode: "method", prefix: prefix.prefix, from: prefix.from };
}
const method = commandMatch[1]?.toUpperCase();
const path = commandMatch[2] ?? "";
const pathStart = lineStart + (commandMatch[0].indexOf(path) >= 0 ? commandMatch[0].indexOf(path) : 0);
const pathCursor = Math.max(0, safeCursor - pathStart);
const boundedPathCursor = Math.min(pathCursor, path.length);
const beforePathCursor = path.slice(0, boundedPathCursor);
const segmentStartInPath = beforePathCursor.lastIndexOf("/") + 1;
const prefix = beforePathCursor.slice(segmentStartInPath);
const segmentIndex = beforePathCursor.slice(0, segmentStartInPath).split("/").filter(Boolean).length;
return {
mode: "path",
prefix,
from: pathStart + segmentStartInPath,
method,
path,
segmentIndex,
};
const prefix = readWordPrefix(text, safeCursor);
return { mode: "method", prefix: prefix.prefix, from: prefix.from };
}
export function buildElasticsearchCompletionItems(text: string, cursor: number, input: ElasticsearchCompletionInput = {}): ElasticsearchCompletionItem[] {
@@ -1,11 +1,12 @@
import type { DatabaseType, QueryResult } from "@/types/database";
import { stripLeadingElasticsearchComments } from "@/lib/sql/sqlStatementRanges";
export interface ElasticsearchJsonResponse {
status: number;
body: string;
}
const ELASTICSEARCH_REST_STATEMENT = /^(?:GET|POST|PUT|DELETE)\s+\S+/i;
const ELASTICSEARCH_REST_STATEMENT = /^(?:GET|POST|PUT|DELETE|HEAD)\s+\S+/i;
/**
* Detect the result shape emitted for a JSON response to an explicit
@@ -14,7 +15,7 @@ const ELASTICSEARCH_REST_STATEMENT = /^(?:GET|POST|PUT|DELETE)\s+\S+/i;
*/
export function elasticsearchJsonResponseForResult(databaseType: DatabaseType | undefined, sourceStatement: string | undefined, result: QueryResult | undefined): ElasticsearchJsonResponse | undefined {
if (databaseType !== "elasticsearch" || !result || typeof sourceStatement !== "string") return undefined;
if (!ELASTICSEARCH_REST_STATEMENT.test(sourceStatement.trim())) return undefined;
if (!ELASTICSEARCH_REST_STATEMENT.test(stripLeadingElasticsearchComments(sourceStatement))) return undefined;
if (result.columns.length !== 2 || result.columns[0] !== "status" || result.columns[1] !== "response" || result.rows.length !== 1) return undefined;
const row = result.rows[0];
+151 -2
View File
@@ -15,7 +15,7 @@ export interface SqlTextRange {
const NON_SQL_EXECUTION_TARGET_TYPES: ReadonlySet<DatabaseType> = new Set(["mongodb", "elasticsearch", "qdrant", "milvus", "weaviate", "chromadb", "etcd", "zookeeper", "mq", "neo4j"]);
export function supportsExecutionTargetPicker(databaseType?: DatabaseType): boolean {
return !!databaseType && (databaseType === "redis" || !NON_SQL_EXECUTION_TARGET_TYPES.has(databaseType));
return !!databaseType && (databaseType === "redis" || databaseType === "elasticsearch" || !NON_SQL_EXECUTION_TARGET_TYPES.has(databaseType));
}
export function hasMultipleExecutionTargets(sql: string, databaseType?: DatabaseType): boolean {
@@ -36,6 +36,150 @@ interface RawStatement {
sql: string;
}
interface ElasticsearchRequestLine {
from: number;
to: number;
text: string;
}
interface ElasticsearchRequestLineCandidate {
lineIndex: number;
from: number;
}
export interface ElasticsearchRestRequestTarget {
method: "GET" | "POST" | "PUT" | "DELETE" | "HEAD";
path: string;
}
const ELASTICSEARCH_REST_REQUEST_LINE = /^\s*(?:GET|POST|PUT|DELETE|HEAD)\s+\S+/i;
function leadingElasticsearchPreambleEnd(value: string): number {
let offset = 0;
while (offset < value.length) {
while (offset < value.length && /\s/.test(value[offset] ?? "")) offset += 1;
if (offset >= value.length) return offset;
if (value[offset] === "#" || value.startsWith("//", offset)) {
const newline = value.indexOf("\n", offset);
offset = newline < 0 ? value.length : newline + 1;
continue;
}
if (value.startsWith("/*", offset)) {
const close = value.indexOf("*/", offset + 2);
offset = close < 0 ? value.length : close + 2;
continue;
}
break;
}
return offset;
}
export function stripLeadingElasticsearchComments(value: string): string {
return value.slice(leadingElasticsearchPreambleEnd(value)).trimStart();
}
function isElasticsearchRequestPreamble(value: string): boolean {
return leadingElasticsearchPreambleEnd(value) === value.length;
}
function elasticsearchRequestLines(sql: string): ElasticsearchRequestLine[] {
const lines: ElasticsearchRequestLine[] = [];
let from = 0;
while (from <= sql.length) {
const newline = sql.indexOf("\n", from);
const to = newline >= 0 ? newline : sql.length;
lines.push({ from, to, text: sql.slice(from, to) });
if (newline < 0) break;
from = newline + 1;
}
return lines;
}
function elasticsearchRequestLineCandidates(lines: ElasticsearchRequestLine[]): ElasticsearchRequestLineCandidate[] {
const candidates: ElasticsearchRequestLineCandidate[] = [];
let inBlockComment = false;
for (let lineIndex = 0; lineIndex < lines.length; lineIndex += 1) {
const line = lines[lineIndex];
let offset = 0;
while (offset < line.text.length) {
if (inBlockComment) {
const close = line.text.indexOf("*/", offset);
if (close < 0) break;
inBlockComment = false;
offset = close + 2;
continue;
}
while (offset < line.text.length && /\s/.test(line.text[offset] ?? "")) offset += 1;
if (offset >= line.text.length) break;
if (line.text.startsWith("/*", offset)) {
inBlockComment = true;
offset += 2;
continue;
}
if (line.text[offset] === "#" || line.text.startsWith("//", offset)) break;
if (ELASTICSEARCH_REST_REQUEST_LINE.test(line.text.slice(offset))) {
candidates.push({ lineIndex, from: line.from + offset });
}
break;
}
}
return candidates;
}
export function parseElasticsearchRestRequestTarget(value: string): ElasticsearchRestRequestTarget | null {
const requestLine = stripLeadingElasticsearchComments(value).split("\n", 1)[0]?.trim() ?? "";
const match = requestLine.match(/^(GET|POST|PUT|DELETE|HEAD)\s+(\S+)/i);
if (!match) return null;
return {
method: match[1].toUpperCase() as ElasticsearchRestRequestTarget["method"],
path: match[2].startsWith("/") ? match[2] : `/${match[2]}`,
};
}
export function isElasticsearchRestRequestText(value: string): boolean {
return parseElasticsearchRestRequestTarget(value) !== null;
}
function splitElasticsearchRestRequestRanges(sql: string): RawStatement[] | undefined {
const lines = elasticsearchRequestLines(sql);
const candidates = elasticsearchRequestLineCandidates(lines);
const firstRequestIndex = candidates.findIndex((candidate) => isElasticsearchRequestPreamble(sql.slice(0, candidate.from)));
if (firstRequestIndex < 0) return undefined;
const requests = candidates.slice(firstRequestIndex);
const hitFroms = requests.map((request, requestIndex) => {
const previousRequestLine = requestIndex > 0 ? requests[requestIndex - 1].lineIndex : -1;
let hitFrom = request.from;
for (let preambleLine = previousRequestLine + 1; preambleLine < request.lineIndex; preambleLine += 1) {
const candidateFrom = lines[preambleLine].from;
if (isElasticsearchRequestPreamble(sql.slice(candidateFrom, request.from))) {
hitFrom = candidateFrom;
break;
}
}
return hitFrom;
});
return requests.map((request, requestIndex) => {
const from = request.from;
const rawTo = requestIndex + 1 < requests.length ? hitFroms[requestIndex + 1] : sql.length;
const to = trimRangeEnd(sql, from, rawTo);
return {
hitFrom: hitFroms[requestIndex],
from,
to,
sql: sql.slice(from, to),
};
});
}
type QuoteState = "none" | "single" | "double" | "backtick" | "bracket" | "dollar";
const COMMON_SOFT_STATEMENT_START_KEYWORDS = [
@@ -126,6 +270,11 @@ const SAP_HANA_SCRIPT_BLOCK_TERMINATORS = new Set(["IF", "FOR", "WHILE"]);
* whitespace are excluded so editor highlights stay tight).
*/
export function splitSqlStatementRanges(sql: string, databaseType?: DatabaseType): RawStatement[] {
if (databaseType === "elasticsearch") {
const requests = splitElasticsearchRestRequestRanges(sql);
if (requests) return requests;
}
const statements: RawStatement[] = [];
const len = sql.length;
const supportsDelimiterCommands = databaseType === "mysql";
@@ -401,7 +550,7 @@ export function statementRangeAtCursor(sql: string, cursorPos: number, databaseT
// Cursor in indentation or inter-statement whitespace immediately before
// the statement should still target that statement, while the returned
// execution range remains tight around the SQL text itself.
if (pos >= statement.hitFrom && pos < statement.from && sql.slice(pos, statement.from).trim() === "") {
if (pos >= statement.hitFrom && pos < statement.from && (sql.slice(pos, statement.from).trim() === "" || (databaseType === "elasticsearch" && isElasticsearchRequestPreamble(sql.slice(statement.hitFrom, statement.from))))) {
const previous = statements[index - 1];
if (previous && isCursorInSameLineDelimiterGap(sql, previous.to, pos)) {
const previousSoftRanges = splitStatementRangeAtSoftStarts(sql, previous, databaseType);