Skip to content

Instantly share code, notes, and snippets.

@olivere
Created December 11, 2019 15:50
Show Gist options
  • Save olivere/3e2a833bbdcc2154c1686bb7fca6b958 to your computer and use it in GitHub Desktop.
Save olivere/3e2a833bbdcc2154c1686bb7fca6b958 to your computer and use it in GitHub Desktop.
Example mapping
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
// Connect creates an index with a mapping with different data types.
//
// Example
//
// mapping -url=http://127.0.0.1:9200 -index=twitter
//
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/olivere/elastic/v7"
)
const (
mapping = `
{
"settings":{
"number_of_shards":1,
"number_of_replicas":0
},
"mappings":{
"properties":{
"tags":{
"type":"keyword"
},
"location":{
"type":"geo_point"
},
"suggest_field":{
"type":"completion"
}
}
}
}
`
)
// Tweet is just an example document.
type Tweet struct {
Tags []string `json:"tags,omitempty"`
Location *elastic.GeoPoint `json:"location,omitempty"`
Suggest *elastic.SuggestField `json:"suggest_field,omitempty"`
}
func (t Tweet) String() string {
var sb strings.Builder
sb.WriteString("Tweet{")
sb.WriteString(fmt.Sprintf("Tags=%v", t.Tags))
if loc := t.Location; loc != nil {
sb.WriteString(fmt.Sprintf(",Location=[%v,%v]", loc.Lat, loc.Lon))
} else {
sb.WriteString(",Location=nil")
}
if s := t.Suggest; s != nil {
sb.WriteString(fmt.Sprintf(",Suggest=%#v", *s))
} else {
sb.WriteString(",Suggest=nil")
}
sb.WriteString("}")
return sb.String()
}
func main() {
var (
url = flag.String("url", "http://localhost:9200", "Elasticsearch URL")
sniff = flag.Bool("sniff", true, "Enable or disable sniffing")
trace = flag.Bool("trace", false, "Enable or disable trace output")
index = flag.String("index", "", "Index name")
)
flag.Parse()
log.SetFlags(0)
if *url == "" {
*url = "http://127.0.0.1:9200"
}
if *index == "" {
log.Fatal("please specify an index name -index")
}
// Create an Elasticsearch client
options := []elastic.ClientOptionFunc{
elastic.SetURL(*url),
elastic.SetSniff(*sniff),
}
if *trace {
options = append(options, elastic.SetTraceLog(log.New(os.Stdout, "", 0)))
}
client, err := elastic.NewClient(options...)
if err != nil {
log.Fatal(err)
}
// Check if index already exists. We'll drop it then.
// Next, we create a fresh index/mapping.
ctx := context.Background()
exists, err := client.IndexExists(*index).Do(ctx)
if err != nil {
log.Fatal(err)
}
if exists {
_, err := client.DeleteIndex(*index).Do(ctx)
if err != nil {
log.Fatal(err)
}
}
_, err = client.CreateIndex(*index).Body(mapping).Do(ctx)
if err != nil {
log.Fatal(err)
}
// Add a tweet
{
tweet := Tweet{
Tags: []string{"golang", "programming"},
Location: elastic.GeoPointFromLatLon(48.13743, 11.57549),
Suggest: elastic.NewSuggestField("Golang", "Elasticsearch"),
}
_, err := client.Index().
Index(*index).
Id("1").
BodyJson(&tweet).
Refresh("true").
Do(context.TODO())
if err != nil {
log.Fatal(err)
}
}
// Read the tweet
{
doc, err := client.Get().
Index(*index).
Id("1").
Do(context.TODO())
if err != nil {
log.Fatal(err)
}
var tweet Tweet
if err = json.Unmarshal(doc.Source, &tweet); err != nil {
log.Fatal(err)
}
fmt.Printf("Read tweet %s\n", tweet)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment