nselastic

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Nselastic

Nselastic is my PERSONAL tool to interact with ElasticSearch. ngl the elastic search client in go is a bit of a pain to use, so I made this to make my life easier casue as everyone knows, unnessary abstraction is the always the way to go.

Domains

Connectors

Connectors are the main way to interact with ElasticSearch. They are the abstraction that allows you to connect to an ElasticSearch instance and build the base form of the http requests that will be sent to the server.

NewConnector
package main

import (
    "github.com/karim-w/nselastic/impl/connector"
)
func main() {
    conn := connector.New("http://localhost:9200","username","password")
}
Index

Indexes are the main way to interact with the data stored in ElasticSearch. The Index interface provides the basic CRUD operations that you would expect from a database with the added bonus of being able to search for data.

NewIndex
package main

import (
    "github.com/karim-w/nselastic/impl/connector"
    "github.com/karim-w/nselastic/impl/index"
)

func main() {
    conn := connector.New("http://localhost:9200","username","password")
    idx := index.New(conn,"index_name")
}

License

BSD 3-Clause License

Author

karim-w

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connector

type Connector interface {
	Req(path string) httpclient.HTTPRequest
}

type Error

type Error struct {
	Err    ErrorDetails `json:"error"`
	Status int64        `json:"status"`
}

func (*Error) Error

func (e *Error) Error() string

type ErrorDetails

type ErrorDetails struct {
	RootCause []ErrorDetails `json:"root_cause,omitempty"`
	Type      string         `json:"type"`
	Reason    string         `json:"reason"`
	IndexUUID string         `json:"index_uuid"`
	Index     string         `json:"index"`
}

type Filters

type Filters struct {
	// Key is the field to be filtered.
	Key string
	// Value is the value to be filtered.
	Value any
}

Filters is a filter to be applied on the data.

type Hook

type Hook interface {
	Before(
		ctx context.Context,
		index string,
		id string,
		doc interface{},
		query Queryable,
	) error
	After(
		ctx context.Context,
		index string,
		id string,
		doc interface{},
		query Queryable,
		start time.Time,
		end time.Time,
		status int,
		err error,
	) error
}

type Index

type Index[T any] interface {
	// Upsert - add or update a document in the index.
	// - ctx: the context of the request.
	// - id: the id of the document.
	// - doc: the document body to be added or updated.
	Upsert(
		ctx context.Context,
		id string,
		doc T,
	) error
	// Fetch - fetches a document from the index.
	// - ctx: the context of the request.
	// - id: the id of the document.
	// Returns:
	// - document: an option of the document, can be None if not found.
	// - error: an error if any. Not found is not considered an error and is represented by the None option
	Fetch(
		ctx context.Context,
		id string,
	) (gopts.Option[T], error)
	// Delete - deletes one or many documents from the index.
	// - ctx: the context of the request.
	// - ids: the ids of the documents to be deleted. multi argument field for multiple ids.
	Delete(
		ctx context.Context,
		ids ...string,
	) error
	// List - retrieves one or many documents from the index.
	// - ctx: the context of the request.
	// - ids: the ids of the documents to be retrieved. multi argument field for multiple ids.
	// Returns:
	// - list: a list of documents.
	// - error: an error if any.
	List(
		ctx context.Context,
		ids ...string,
	) ([]T, error)
	// Query - queries the index with the given query.
	// - ctx: the context of the request.
	// - query: the query to be executed
	// Returns:
	// - list: a list of documents.
	// - error: an error if any.
	Query(
		ctx context.Context,
		query Queryable,
	) (QueryResult[T], error)
	// Ensure - ensures the index exists.
	// Returns:
	// - error: an error if any.
	Ensure() error
	// Client -  Returns an HTTP Client to be build
	// - endpoint: the elastic endpoint
	// Returns:
	// - httpclient.HTTPRequest
	Client(endpoint string) httpclient.HTTPRequest
	// Name - Returns the Index name
	// Returns:
	// - string
	Name() string
}

type QueryBody

type QueryBody struct {
	// Searches is a list of strings to be partially matched.
	Searches []string
	// Ranges is a range filter to be applied on the data.
	Ranges []Range
	// Equal is a list of filters to be applied on the data.
	Equal []Filters
	// NotEqual is a list of filters to be excluded from the data.
	NotEqual []Filters
	// In is a list of filters where the value should match any of the values
	// in an OR fashion
	In []Filters
}

QueryBody is the query body to be applied on the data.

type QueryResult added in v0.0.2

type QueryResult[T any] struct {
	// Data is the list of documents.
	Data []T
	// Total is the total number of documents.
	Total int
}

QueryResult is the **paged* response of the query.

type Queryable

type Queryable struct {
	// From is the starting index of the data to be fetched. Default is 0.
	// Size is the number of data to be fetched. Default is 1000.
	From, Size gopts.Option[int]
	// SortingOptions is a list of sorting options to be applied on the data.
	SortingOptions []SortingOptions
	// Search is the query body to be applied on the data.
	Search QueryBody
}

Queryable is a description of the query you would like to perform on the index data

type Range

type Range struct {
	// Key is the field to be ranged.
	Key string
	// Gte is the greater than or equal to value.
	// Lte is the less than or equal to value.
	Gte, Lte gopts.Option[any]
}

Range is a range filter to be applied on the data.

type SortingOptions

type SortingOptions struct {
	// SortField is the field to be sorted.
	SortField, SortOrder gopts.Option[string]
}

SortingOptions is a list of sorting options to be applied on the data.

type TraceInfo

type TraceInfo struct {
	Index, Id string
	Doc       interface{}
	Query     Queryable
}

Directories

Path Synopsis
impl

Jump to

Keyboard shortcuts

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