Skip to content

Latest commit

 

History

History
370 lines (255 loc) · 17 KB

File metadata and controls

370 lines (255 loc) · 17 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(aggs []esquery.Aggregation, sortingRequest *sorting.Request, pagingRequest *paging.Request) ([]esquery.Aggregation, error)

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

func AddOrder(aggregation *esquery.TermsAggregation, sortingRequest *sorting.Request) (*esquery.TermsAggregation, error)

func BucketSortAgg(sortingRequest *sorting.Request, pagingRequest *paging.Request) (*esquery.CustomAggMap, error)

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

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

HandleCompareOperatorBeginsWith handles begins with

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

HandleCompareOperatorContains handles contains

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

HandleCompareOperatorIsEqualTo handles is equal to

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

HandleCompareOperatorIsGreaterThan handles is greater than

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

HandleCompareOperatorIsGreaterThanOrEqualTo handles is greater than or equal to

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

HandleCompareOperatorIsKeywordEqualTo handles is keyword field equal to

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

HandleCompareOperatorIsLessThan handles is less than

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

HandleCompareOperatorIsLessThanOrEqualTo handles is less than or equal to

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

HandleCompareOperatorNotBeginsWith handles not begins with

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

func (*BoolQueryBuilder) Size

func (q *BoolQueryBuilder) Size(size uint64) *BoolQueryBuilder

Size sets the size to be used in the query.

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
}

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

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
}

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
}

Generated by gomarkdoc

License

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

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