Skip to content

Latest commit

 

History

History
493 lines (335 loc) · 19.8 KB

File metadata and controls

493 lines (335 loc) · 19.8 KB

openSearchQuery Package Documentation

Package openSearchQuery provides a query builder for OpenSearch.

Usage example:

q := openSearchQuery.NewBoolQueryBuilder(&openSearchQuery.QuerySettings{ /* ... */ })

if err := q.AddFilterRequest(filterRequest); err != nil {
    return nil, err
}

request, err := esquery.Search().

Query(q.Build()).
MarshalJSON()

if err != nil {
    return nil, err
}

responseBody, err := v.openSearchClient.Search(openSearchModels.VulnerabilityIndexName, request)

if err != nil {
    return nil, err
}

openSearchQuery

import "github.com/greenbone/opensight-golang-libraries/pkg/openSearch/openSearchQuery"

Package openSearchQuery provides a query builder for OpenSearch.

Index

func AddBucketSortAgg

func AddBucketSortAgg(aggs []esquery.Aggregation, sortingRequest *sorting.Request, sortFieldMapping map[string]EffectiveSortField, pagingRequest *paging.Request) ([]esquery.Aggregation, error)

func AddMaxAggForSorting

func AddMaxAggForSorting(aggs []esquery.Aggregation, sortingRequest *sorting.Request, sortFieldMapping map[string]EffectiveSortField) ([]esquery.Aggregation, error)

func AddOrder

func AddOrder(aggregation *esquery.TermsAggregation, sortingRequest *sorting.Request, sortFieldMapping map[string]EffectiveSortField) (*esquery.TermsAggregation, error)

func BucketSortAgg

func BucketSortAgg(sortingRequest *sorting.Request, sortFieldMapping map[string]EffectiveSortField, pagingRequest *paging.Request) (*esquery.CustomAggMap, error)

BucketSortAgg is capable to sort all existing buckets, but is currently only used for paging

func HandleCompareOperatorBeginsWith

func HandleCompareOperatorBeginsWith(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

HandleCompareOperatorBeginsWith handles begins with

func HandleCompareOperatorBetweenDates

func HandleCompareOperatorBetweenDates(fieldName string, _ []string, fieldValue any, _ *QuerySettings) esquery.Mappable

HandleCompareOperatorBetweenDates constructs an OpenSearch range query for a given date field. It accepts a field name and a field value, which must be either: - A slice of two time.Time values ([]time.Time), representing the start and end of the range, or - A slice of two RFC3339Nano-formatted strings ([]string), which are parsed into time.Time, representing the start and end of the range.

The generated range query is inclusive of both the lower and upper bounds. If a document’s timestamp is exactly equal to the start or end date, it will still match the query.

If the slice length is not exactly 2, or if the string values cannot be parsed into valid dates, the function logs an error and returns an empty query (MatchNone).

func HandleCompareOperatorContains

func HandleCompareOperatorContains(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

HandleCompareOperatorContains handles contains. In the index mapping the given field must be a string of type `keyword`.

func HandleCompareOperatorExists

func HandleCompareOperatorExists(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

func HandleCompareOperatorIsEqualTo

func HandleCompareOperatorIsEqualTo(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

HandleCompareOperatorIsEqualTo handles is equal to

func HandleCompareOperatorIsEqualToRating

func HandleCompareOperatorIsEqualToRating(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

func HandleCompareOperatorIsGreaterThan

func HandleCompareOperatorIsGreaterThan(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

HandleCompareOperatorIsGreaterThan handles is greater than

func HandleCompareOperatorIsGreaterThanOrEqualTo

func HandleCompareOperatorIsGreaterThanOrEqualTo(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

HandleCompareOperatorIsGreaterThanOrEqualTo handles is greater than or equal to

func HandleCompareOperatorIsGreaterThanOrEqualToRating

func HandleCompareOperatorIsGreaterThanOrEqualToRating(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

func HandleCompareOperatorIsGreaterThanRating

func HandleCompareOperatorIsGreaterThanRating(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

func HandleCompareOperatorIsKeywordEqualTo

func HandleCompareOperatorIsKeywordEqualTo(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

HandleCompareOperatorIsKeywordEqualTo handles is keyword field equal to

func HandleCompareOperatorIsLessThan

func HandleCompareOperatorIsLessThan(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

HandleCompareOperatorIsLessThan handles is less than

func HandleCompareOperatorIsLessThanOrEqualTo

func HandleCompareOperatorIsLessThanOrEqualTo(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

HandleCompareOperatorIsLessThanOrEqualTo handles is less than or equal to

func HandleCompareOperatorIsLessThanOrEqualToRating

func HandleCompareOperatorIsLessThanOrEqualToRating(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

func HandleCompareOperatorIsLessThanRating

func HandleCompareOperatorIsLessThanRating(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

func HandleCompareOperatorIsNotEqualToRating

func HandleCompareOperatorIsNotEqualToRating(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

func HandleCompareOperatorNotBeginsWith

func HandleCompareOperatorNotBeginsWith(fieldName string, fieldKeys []string, fieldValue any, querySettings *QuerySettings) esquery.Mappable

HandleCompareOperatorNotBeginsWith handles not begins with

func HandleCompareOperatorTextContains

func HandleCompareOperatorTextContains(fieldName string, _ []string, fieldValue any, _ *QuerySettings) esquery.Mappable

HandleCompareOperatorTextContains performs a full text search on the given field. In the index mapping it must be a string of type `text`.

func ValueToString

func ValueToString(value interface{}) string

ValueToString converts the given value to a string. Compared to fmt.Sprint it will give RFC3339 format for time.Time value and a specific formatting of numbers.

type BoolQueryBuilder

BoolQueryBuilder is a builder for an OpenSearch bool query. Use NewBoolQueryBuilder or NewBoolQueryBuilderWith for proper initialization.

type BoolQueryBuilder struct {
    Must    []esquery.Mappable
    MustNot []esquery.Mappable
    // contains filtered or unexported fields
}

func NewBoolQueryBuilder

func NewBoolQueryBuilder(querySettings *QuerySettings) *BoolQueryBuilder

NewBoolQueryBuilder creates a new BoolQueryBuilder and returns it. It uses the default set of CompareOperator.

querySettings is used to configure the query builder.

func NewBoolQueryBuilderWith

func NewBoolQueryBuilderWith(query *esquery.BoolQuery, querySettings *QuerySettings) *BoolQueryBuilder

NewBoolQueryBuilderWith creates a new BoolQueryBuilder and returns it. It uses the default set of CompareOperator.

query is the initial bool query to use. querySettings is used to configure the query builder.

func (*BoolQueryBuilder) AddCompareOperators

func (q *BoolQueryBuilder) AddCompareOperators(operators ...CompareOperator) *BoolQueryBuilder

AddCompareOperators adds the given set of CompareOperator to the set of CompareOperator to be used for this query builder.

operators is the set of CompareOperator to add.

func (*BoolQueryBuilder) AddFilterRequest

func (q *BoolQueryBuilder) AddFilterRequest(request *filter.Request) error

AddFilterRequest adds a filter request to this query. The filter request is translated into a bool query.

func (*BoolQueryBuilder) AddTermFilter

func (q *BoolQueryBuilder) AddTermFilter(fieldName string, value interface{}) *BoolQueryBuilder

AddTermFilter adds a term filter to this query.

value is the value to filter for.

func (*BoolQueryBuilder) AddTermsFilter

func (q *BoolQueryBuilder) AddTermsFilter(fieldName string, values ...interface{}) *BoolQueryBuilder

AddTermsFilter adds a terms filter to this query.

values is the list of values to filter for.

func (*BoolQueryBuilder) Build

func (q *BoolQueryBuilder) Build() *esquery.BoolQuery

Build returns the built query.

func (*BoolQueryBuilder) ReplaceCompareOperators

func (q *BoolQueryBuilder) ReplaceCompareOperators(operators []CompareOperator) *BoolQueryBuilder

ReplaceCompareOperators replaces the set of CompareOperator to be used for this query builder.

operators is the new set of CompareOperator to use.

type CompareOperator

CompareOperator defines a mapping between a filter.CompareOperator and a function to generate an appropriate query condition in from of a CompareOperatorHandler.

type CompareOperator struct {
    Operator filter.CompareOperator
    Handler  CompareOperatorHandler
    // MustCondition defines whether the condition should be added to the must (true) or must_not clause (false).
    MustCondition bool
}

type CompareOperatorHandler

CompareOperatorHandler is a function that generates an appropriate query condition for the given field.

fieldName is the name of the field. fieldKeys is a list of keys used only for nested fields. fieldValue is the value to compare against. querySettings are the settings to use for the query defining which fields are to be treated e.g. as wildcard array or keyword field.

type CompareOperatorHandler func(fieldName string, fieldKeys []string, fieldValue any,
    querySettings *QuerySettings) esquery.Mappable

type EffectiveSortField

type EffectiveSortField struct {
    PlainField       *string
    AggregationName  *string
    AggregationValue string
}

type NestedQueryFieldDefinition

NestedQueryFieldDefinition is a definition of a nested query field.

type NestedQueryFieldDefinition struct {
    // FieldName is the name of the field.
    FieldName string
    // FieldKeyName is the name of the key field.
    FieldKeyName string
    // FieldValueName is the name of the value field.
    FieldValueName string
}

type QuerySettings

QuerySettings is used to configure the query builder.

type QuerySettings struct {
    // WildcardArrays is a map of field names to a boolean value indicating whether the field is to be treated as a wildcard array.
    WildcardArrays map[string]bool
    // IsEqualToKeywordFields is a map of field names to a boolean value indicating whether the field is to be treated as a keyword field.
    IsEqualToKeywordFields map[string]bool
    // UseNestedMatchQueryFields is a map of field names to a boolean value indicating whether the field is to be treated as a nested query field.
    UseNestedMatchQueryFields map[string]bool
    // NestedQueryFieldDefinitions is a list of nested query field definitions.
    NestedQueryFieldDefinitions []NestedQueryFieldDefinition
    // UseMatchPhraseFields is a map of field names to a boolean value indicating whether the field should use a match phrase query.
    UseMatchPhrase     map[string]bool
    FilterFieldMapping map[string]string
    // StringFieldRating is a map for field names with a rating. The rating is used to determine the compare order of the field in the query.
    StringFieldRating map[string]map[string]RatingRange
}

type RatingRange

RatingRange represent a closed interval of float32 values.

type RatingRange struct {
    Min float32 // Lower bound of the rating range (inclusive)
    Max float32 // Upper bound of the rating range (inclusive)
}

Generated by gomarkdoc

License

Copyright (C) 2022-2023 [Greenbone AG][Greenbone AG]

Licensed under the GNU General Public License v3.0 or later.