es

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: May 25, 2020 License: Apache-2.0 Imports: 4 Imported by: 1

README

ElasticSearch

package main

import (
    "context"

    "github.com/joshqu1985/fireman/configor"
    "github.com/joshqu1985/fireman/store/es"
)

type Config struct {
    Elastic  es.Config
}

var (
    Conf Config
)

func init() {
    if err := configor.Load("./configs/conf.toml", &Conf); err != nil {
        panic(err)
    }
}

type SearchInfo struct {
    Source   []byte
    Distance float64
}

type SearchInfos []SearchInfo

func main() {
    pool := es.NewPool(Conf.Elastic)

    items := make(SearchInfos, 0)
    handle := func(c *elastic.Client) error {
        queryer := elastic.NewBoolQuery().
            Should(elastic.NewQueryStringQuery("lily").Field("name"))

        res, err := c.Search().Index("user").Query(queryer).
            From(int(0)).Size(20).Do(context.Background())
        if err != nil {
            return err
        }
        if res.Hits.TotalHits == 0 {
            return nil
        }
        
        for _, hit := range res.Hits.Hits {
            items = append(items, SearchInfo{Source: *hit.Source})
        }
        return nil
    }
    err := pool.Doit(context.Background(), handle)

    fmt.Prinln(items, err)
}
#conf.toml
[elastic]
    hosts = ["http://172.31.40.234:9200"]
    auth  = ""

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Hosts []string
	Auth  string
}

type Pool

type Pool struct {
	Client *elastic.Client
}

func NewPool

func NewPool(conf Config) *Pool

func (*Pool) Doit

func (p *Pool) Doit(ctx context.Context, h func(*elastic.Client) error) error

Jump to

Keyboard shortcuts

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