Documentation
¶
Overview ¶
Package opensearch is a database/sql driver for OpenSearch, which runs SQL through POST /_plugins/_sql over HTTP. It registers the one name "opensearch", and takes a DSN of the form opensearch://user:pass@host:9200?key=value (D168).
db, err := sql.Open("opensearch", "opensearch://admin:pass@localhost:9200")
The driver reads only. SQL in OpenSearch takes SELECT, SHOW and DESCRIBE, and the server refuses every write, which the driver returns (D163). A write goes through the document API of the server, which the driver does not speak. The server binds no argument that it keeps apart from the text of the statement, so the driver writes each argument into the statement as a literal (D34 and D168).
The driver sends a page size with each plain SELECT, reads each page one token at a time, follows the cursor to the last page, and closes the cursor of a result that the caller leaves before the end (D168). A cursor serves once, so a page is never read twice. A failure on a later page wraps dbimp.ErrIncomplete (D107). The SQL plugin has no way to stop a statement, so a cancelled context closes the request and the cursor only. OpenSearch has no transactions. docs/OPENSEARCH.md holds what the driver knows about the server, and the caps that the server applies to a result with no sign (D163).
Index ¶
Constants ¶
const Name = "opensearch"
Name is the name that the driver registers with database/sql, and the scheme of its DSN.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// Host is the host of the server.
Host string
// Port is the port of the HTTP interface, 9200 by default.
Port int
// TLS is true to speak HTTPS.
TLS bool
// User and Password are the credentials, which the driver sends with
// basic authentication. With neither, the driver sends none (D94).
User string
Password string
// FetchSize is the rows of a page of a plain SELECT, as fetch_size, 1000
// by default. The server cannot give a page larger than
// index.max_result_window of an index, which is 10000 by default
// (measured).
FetchSize int
}
Config is the configuration of a connector, which the DSN holds. The caller owns it (D7).
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector opens connections to one server. It owns its transport, and every connection shares it. A caller can build one from a Config and open it with sql.OpenDB.
func NewConnector ¶
NewConnector returns a Connector for cfg. The connector keeps a copy of cfg, and fills each field that is zero with its default.
type Driver ¶
type Driver struct{}
Driver is the database/sql driver for OpenSearch.
type Error ¶
type Error struct {
// HTTPStatus is the status code of the response, which is 200 for an
// error that only the body reports.
HTTPStatus int
// Status is the status that the body names, and the HTTP status when the
// body names none.
Status int
// Type is the name of the exception of the server, such as
// SemanticCheckException or NullPointerException, and empty when the
// answer has none.
Type string
// Reason is the reason of the error, or the text of the answer when it
// has no JSON object.
Reason string
// Details is the text that the server gives with the reason, and empty
// when the answer has none.
Details string
// contains filtered or unexported fields
}
Error is an error that OpenSearch reported. The SQL plugin answers an error as a JSON object, {"error": {"reason": ..., "details": ..., "type": ...}, "status": ...}, with the content type text/plain, such as HTTP 400 with SemanticCheckException for an unknown column, HTTP 404 with IndexNotFoundException for an unknown index, and HTTP 404 with SearchContextMissingException for a cursor that is read already. Some errors arrive with HTTP 200, and then only the status of the body says that the statement failed, such as UNION with status 500 and a refusal of the security plugin in the legacy engine with status 403. A wrong password is HTTP 401 with a body of the plain text Unauthorized (measured).
type Option ¶
Option sets an option of one statement (D109). An option comes from the DSN, then from the context through WithOptions, then from an argument of the statement, and a later one wins.
func WithDatabase ¶
WithDatabase names the database of one statement. OpenSearch has no database to choose, so a statement with WithDatabase fails with dbimp.ErrNotSupported (D168).
func WithFetchSize ¶
WithFetchSize sets the rows of a page of a plain SELECT, as fetch_size, as the key fetch_size of the DSN does.
func WithParameter ¶
WithParameter sets any key of the body of POST /_plugins/_sql by its name, such as "filter", which holds a query of the Query DSL, or "fetch_size". The value is encoded with json/v2. A key named here replaces one that the driver sets itself, as in Couchbase, so "fetch_size" replaces the page size of the driver, and 0 turns paging off. The key applies to the first request of a statement and not to the request for each next page, which sends only the cursor. A statement with "filter" runs in the legacy engine of the server, whose types, errors and pages differ (measured).
func WithReadonly ¶
WithReadonly asks that the statement write nothing. SQL in OpenSearch takes no write of any kind (D163), so every statement is read-only, and the option changes nothing.
func WithTimeout ¶
WithTimeout asks the server to stop the statement after d. The SQL plugin has no such setting: wait_for_completion_timeout does not work, and the plugin has no way to cancel a statement (measured). A statement with a positive timeout fails with dbimp.ErrNotSupported, so that a caller never believes that it holds. The context of the statement bounds the request instead.