arquivei / elasticutil

A Golang library that assists in the use of Elasticsearch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ElasticUtil

A Golang library that assists in the use of Elasticsearch


Table of Contents

ElasticUtil is a generic library that assists in the use of Elasticsearch, using olivere/elastic library. It is possible to create olivere/elastic's queries using only one struct. It is also possible to translate errors and responses.

Stack Version
Golang v1.21.3
golangci-lint v1.54.2
    • Any Golang programming language version installed, preferred 1.18 or later.
  • go get -u github.com/arquivei/elasticutil
    
  • go mod vendor
    go mod tidy
    
    • Import the package

      import (
          "github.com/arquivei/elasticutil"
      )
    • Define a filter struct

      type ExampleFilterMust struct {
        Names            []string `es:"Name"`
        SocialNames      []string `es:"SocialName"`
        Ages             []uint64 `es:"Age"`
        HasCovid         *bool
        CreatedAt        *elasticutil.TimeRange
        AgeRange         *elasticutil.IntRange            `es:"Age"`
        CovidInfo        elasticutil.Nested               `es:"Covid"`
        NameOrSocialName elasticutil.FullTextSearchShould `es:"Name,SocialName"`
      }
      
      type ExampleFilterExists struct {
        HasCovidInfo elasticutil.Nested `es:"Covid"`
        HasAge       *bool              `es:"Age"`
      }
      
      type ExampleCovidInfo struct {
        HasCovidInfo     *bool                  `es:"Covid"`
        Symptoms         []string               `es:"Covid.Symptom"`
        FirstSymptomDate *elasticutil.TimeRange `es:"Covid.Date"`
      }
    • And now, it's time!

      requestFilter := elasticutil.Filter{
        Must: ExampleFilterMust{
          Names:    []string{"John", "Mary"},
          Ages:     []uint64{16, 17, 18, 25, 26},
          HasCovid: refBool(true),
          CovidInfo: elasticutil.NewNested(
            ExampleCovidInfo{
              Symptoms: []string{"cough"},
              FirstSymptomDate: &elasticutil.TimeRange{
                From: time.Date(2019, time.November, 28, 15, 27, 39, 49, time.UTC),
                To:   time.Date(2020, time.November, 28, 15, 27, 39, 49, time.UTC),
              },
            },
          ),
          CreatedAt: &elasticutil.TimeRange{
            From: time.Date(2020, time.November, 28, 15, 27, 39, 49, time.UTC),
            To:   time.Date(2021, time.November, 28, 15, 27, 39, 49, time.UTC),
          },
          AgeRange: &elasticutil.IntRange{
            From: 15,
            To:   30,
          },
          NameOrSocialName: elasticutil.NewFullTextSearchShould([]string{"John", "Mary", "Rebecca"}),
        },
        MustNot: ExampleFilterMust{
          Names: []string{"Lary"},
          AgeRange: &elasticutil.IntRange{
            From: 29,
            To:   30,
          },
        },
        Exists: ExampleFilterExists{
          HasCovidInfo: elasticutil.NewNested(
            ExampleCovidInfo{
              HasCovidInfo: refBool(true),
            },
          ),
          HasAge: refBool(true),
        },
      }
      
      // BuildElasticBoolQuery builds a olivere/elastic's query based on Filter.
      elasticQuery, err := elasticutil.BuildElasticBoolQuery(context.Background(), requestFilter)
      if err != nil {
        panic(err)
      }
      
      // MarshalQuery transforms a olivere/elastic's query in a string for log and test
      // purpose.
      verboseElasticQuery := elasticutil.MarshalQuery(elasticQuery)
      
      fmt.Println(verboseElasticQuery)
  • ElasticUtil 0.1.0 (May 27, 2022)

    • [New] Decoupling this package from Arquivei's API projects.
    • [New] Setting github's workflow with golangci-lint
    • [New] Example for usage.
    • [New] Documents: Code of Conduct, Contributing, License and Readme.

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

We use Semantic Versioning for versioning. For the versions available, see the tags on this repository.

This project is licensed under the BSD 3-Clause - see the LICENSE.md file for details.

All contact may be doing by marcos.filho@arquivei.com.br

About

A Golang library that assists in the use of Elasticsearch

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Go 100.0%