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 = ""