opensearch

package
v0.26.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func WithOptions

func WithOptions(ctx context.Context, opts ...Option) context.Context

WithOptions returns a context that carries opts. Each statement started with the context applies them after the options of the DSN.

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).

func ParseDSN

func ParseDSN(dsn string) (*Config, error)

ParseDSN parses a DSN of the form opensearch://user:pass@host:port?key=value (D27, D35 and D168). A DSN with a path is refused, because OpenSearch has no database to choose, and so is every key that the driver does not know.

func (*Config) FormatDSN

func (cfg *Config) FormatDSN() string

FormatDSN returns the DSN of cfg. ParseDSN reads it back as cfg, for a Config that ParseDSN or NewConnector filled.

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

func NewConnector(cfg Config) *Connector

NewConnector returns a Connector for cfg. The connector keeps a copy of cfg, and fills each field that is zero with its default.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the idle connections of the transport.

func (*Connector) Connect

func (c *Connector) Connect(context.Context) (driver.Conn, error)

Connect satisfies driver.Connector. A connection holds nothing on the server, so it sends no request.

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver satisfies driver.Connector.

type Driver

type Driver struct{}

Driver is the database/sql driver for OpenSearch.

func (Driver) Open

func (Driver) Open(string) (driver.Conn, error)

Open satisfies driver.Driver. database/sql opens a connection through OpenConnector, and Open returns an error, because opening a connection needs a context.

func (Driver) OpenConnector

func (Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector satisfies driver.DriverContext. It parses the DSN.

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).

func (*Error) AuthenticationRefused added in v0.24.0

func (err *Error) AuthenticationRefused() bool

AuthenticationRefused reports whether the server refused the credential. It returns what Is reports for dbimp.ErrAuthentication, and a caller that does not import this module reads it through an interface of its own (D227).

func (*Error) ErrVerbose added in v0.25.0

func (err *Error) ErrVerbose(_ string) string

ErrVerbose returns the error as psql writes it for \errverbose: the code and the message on the first line, and then each field that the server sent, one on each line with its name in upper case. A caller that does not import this module reads it through an interface of its own (D231).

func (*Error) Error

func (err *Error) Error() string

Error satisfies the error interface.

func (*Error) Is added in v0.18.0

func (err *Error) Is(target error) bool

Is reports whether err matches target. It matches dbimp.ErrAuthentication when the status is 401, whether it is the HTTP status or the status of a body that came with HTTP 200. HTTP 401 is a wrong password (recorded: "a wrong password"). Every other refusal does not match, such as HTTP 403 for a missing permission (D197).

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns the *dbimp.StatusError of the response, and nil for an error that came with HTTP 200.

type Option

type Option = dbimp.Option[options]

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

func WithDatabase(name string) Option

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

func WithFetchSize(n int) Option

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

func WithParameter(name string, value any) Option

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

func WithReadonly(bool) Option

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

func WithTimeout(d time.Duration) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL