How can I create a index in Elasticsearch with `go-elasticsearch` library?

huangapple go评论74阅读模式
英文:

How can I create a index in Elasticsearch with `go-elasticsearch` library?

问题

我正在使用这个库作为Elasticsearch客户端:https://pkg.go.dev/github.com/elastic/go-elasticsearch/esapi#IndicesCreate.WithBody

我在使用这个库创建一个新的索引时遇到了问题。文档中说有这个方法:

type IndicesCreate func(index string, o ...func(*IndicesCreateRequest)) (*Response, error)

看起来这个方法可以用来创建索引。但是我对go不太熟悉,不确定如何传递第二个参数。

以下是我的代码:

req := esapi.IndicesCreateRequest{
		Index: indexName,
}
esapi.IndicesCreate(indexName, &req)

但是我得到了too many arguments in conversion to esapi.IndicesCreate的错误消息。请问正确的做法是什么?

英文:

I am using this library in go as Elasticsearch client: https://pkg.go.dev/github.com/elastic/go-elasticsearch/esapi#IndicesCreate.WithBody

I have a problem on creating a new index with this library. The doc says this method:

type IndicesCreate func(index string, o ...func(*IndicesCreateRequest)) (*Response, error)

which looks like the one I can use to create the index. But I am new to go and not sure how to pass the second parameter.

Below is my code:

req := esapi.IndicesCreateRequest{
		Index: indexName,
	}
	esapi.IndicesCreate(indexName, &req)

but I got too many arguments in conversion to esapi.IndicesCreate error message. What is the right way to do it?

答案1

得分: 3

根据这篇文章,这是你的问题的答案:

package main

import (
	"log"
	"strings"

	"github.com/elastic/go-elasticsearch/v8"
)

func main() {

	client, err := elasticsearch.NewDefaultClient()
	if err != nil {
		log.Fatal(err)
	}

	index := "products"
	mapping := `
	{
	  "settings": {
	    "number_of_shards": 1
	  },
	  "mappings": {
	    "properties": {
	      "field1": {
	        "type": "text"
	      }
	    }
	  }
	}`

	res, err := client.Indices.Create(
		index,
		client.Indices.Create.WithBody(strings.NewReader(mapping)),
	)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(res)
}

根据这篇文章:

你只需要做以下操作:

export ES_INDEX=products # Linux, MacOS
set ES_INDEX=products # Windows
package main

import (
	"errors"
	"log"
	"os"

	elasticsearch "github.com/elastic/go-elasticsearch/v8"
)

var (
	IndexNameEmptyStringError = errors.New("索引名称不能为空字符串")
	IndexAlreadyExistsError   = errors.New("Elasticsearch索引已经存在")
)

func main() {
	index := os.Getenv("ES_INDEX")
	if index == "" {
		log.Fatal(IndexNameEmptyStringError)
	}

	elastic, err := elasticsearch.NewClient(elasticsearch.Config{
		Addresses: []string{"http://localhost:9200"},
	})

	if err != nil {
		log.Fatal(err)
	}

	response, err := elastic.Indices.Exists([]string{index})
	if err != nil {
		log.Fatal(err)
	}

	if response.StatusCode != 404 {
		log.Fatal(IndexAlreadyExistsError)
	}

	response, err = elastic.Indices.Create(index)
	if err != nil {
		log.Fatal(err)
	}

	if response.IsError() {
		log.Fatal(err)
	}
}
英文:

New Answer

Based on this article, this is answer to your question:

package main

import (
	"log"
	"strings"

	"github.com/elastic/go-elasticsearch/v8"
)

func main() {

	client, err := elasticsearch.NewDefaultClient()
	if err != nil {
		log.Fatal(err)
	}

	index := "products"
	mapping := `
	{
	  "settings": {
	    "number_of_shards": 1
	  },
	  "mappings": {
	    "properties": {
	      "field1": {
	        "type": "text"
	      }
	    }
	  }
	}`

	res, err := client.Indices.Create(
		index,
		client.Indices.Create.WithBody(strings.NewReader(mapping)),
	)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(res)
}

Old Answer

According to this article:

All you have to do is:

export ES_INDEX=products # Linux, MacOS
set ES_INDEX=products # Windows
package main

import (
	"errors"
	"log"
	"os"

	elasticsearch "github.com/elastic/go-elasticsearch/v8"
)

var (
	IndexNameEmptyStringError = errors.New("index name cannot be empty string")
	IndexAlreadyExistsError   = errors.New("elasticsearch index already exists")
)

func main() {
	index := os.Getenv("ES_INDEX")
	if index == "" {
		log.Fatal(IndexNameEmptyStringError)
	}

	elastic, err := elasticsearch.NewClient(elasticsearch.Config{
		Addresses: []string{"http://localhost:9200"},
	})

	if err != nil {
		log.Fatal(err)
	}

	response, err := elastic.Indices.Exists([]string{index})
	if err != nil {
		log.Fatal(err)
	}

	if response.StatusCode != 404 {
		log.Fatal(IndexAlreadyExistsError)
	}

	response, err = elastic.Indices.Create(index)
	if err != nil {
		log.Fatal(err)
	}

	if response.IsError() {
		log.Fatal(err)
	}
}

huangapple
  • 本文由 发表于 2022年4月2日 18:18:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/71716661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定