英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论