es包
gopkg.in/olivere/elastic.v3
Golang操作es 过程中,定义struct 结果写入es的内容总是为空,后来发现,定义struct 首字母小写了,Golang中首字母小写表示私有,所以无法成功写入,改成共有的即可。
刚学习,还不熟悉,做个记录
//聽qnzsEs聽project聽main.go
package聽main
import聽(
聽聽聽聽"fmt"
聽聽聽聽"log"
聽聽聽聽"os"
聽聽聽聽"database/sql"
聽聽聽聽"time"
聽聽聽聽_聽"github.com/go-sql-driver/mysql"
聽聽聽聽"gopkg.in/olivere/elastic.v3"
)
var聽client聽*elastic.Client
var聽db聽*sql.DB
type聽ques聽struct聽{
聽聽聽聽qid聽聽聽string
聽聽聽聽title聽string
聽聽聽聽added聽string
聽聽聽聽orgid聽string
}
type聽Ques2聽struct聽{
聽聽聽聽Content聽string
}
func聽init()聽{
聽聽聽聽var聽err聽error
聽聽聽聽client,聽err聽=聽elastic.NewClient(
聽聽聽聽聽聽聽聽elastic.SetURL("http://127.0.0.1:9200"),
聽聽聽聽聽聽聽聽elastic.SetSniff(false),
聽聽聽聽聽聽聽聽elastic.SetHealthcheckInterval(10*time.Second),
聽聽聽聽聽聽聽聽elastic.SetMaxRetries(5),
聽聽聽聽聽聽聽聽elastic.SetErrorLog(log.New(os.Stderr,聽"ELASTIC聽",聽log.LstdFlags)),
聽聽聽聽聽聽聽聽elastic.SetInfoLog(log.New(os.Stdout,聽"",聽log.LstdFlags)))
聽聽聽聽if聽err聽!=聽nil聽{
聽聽聽聽聽聽聽聽fmt.Println(err)
聽聽聽聽}
聽聽聽聽//初始化Mysql
聽聽聽聽db,聽_聽=聽sql.Open("mysql",聽"root:123456@tcp(127.0.0.1:3306)/youth_qnzs")
聽聽聽聽db.SetMaxOpenConns(2000)
聽聽聽聽db.SetMaxIdleConns(1000)
聽聽聽聽db.Ping()
}
func聽main()聽{
聽聽聽聽quesRow聽:=聽getFromMysql()
聽聽聽聽//fmt.Println(ques["orgstrid"])
聽聽聽聽//quesContent聽:=聽ques{qid:聽quesRow["q_id"],聽title:聽quesRow["q_title"],聽added:聽quesRow["q_added"],聽orgid:聽quesRow["orgid"]}
聽聽聽聽quesContent聽:=聽Ques2{Content:聽quesRow["q_title"]}
聽聽聽聽fmt.Println(quesContent)
聽聽聽聽_,聽err聽:=聽client.Index().
聽聽聽聽聽聽聽聽Index("ikindex").
聽聽聽聽聽聽聽聽Type("fulltext").
聽聽聽聽聽聽聽聽Id(quesRow["q_id"]).
聽聽聽聽聽聽聽聽BodyJson(quesContent).
聽聽聽聽聽聽聽聽Refresh(true).
聽聽聽聽聽聽聽聽Do()
聽聽聽聽if聽err聽!=聽nil聽{
聽聽聽聽聽聽聽聽//聽Handle聽error
聽聽聽聽聽聽聽聽panic(err)
聽聽聽聽}
}
func聽getFromMysql()聽map[string]string聽{
聽聽聽聽record聽:=聽make(map[string]string)
聽聽聽聽rows,聽err聽:=聽db.Query("SELECT聽*聽FROM聽q_questions聽limit聽998,聽1")
聽聽聽聽defer聽rows.Close()
聽聽聽聽//checkErr(err)
聽聽聽聽if聽err聽!=聽nil聽{
聽聽聽聽聽聽聽聽fmt.Println(err)
聽聽聽聽聽聽聽聽return聽record
聽聽聽聽}
聽聽聽聽columns,聽_聽:=聽rows.Columns()
聽聽聽聽scanArgs聽:=聽make([]interface{},聽len(columns))
聽聽聽聽values聽:=聽make([]interface{},聽len(columns))
聽聽聽聽for聽j聽:=聽range聽values聽{
聽聽聽聽聽聽聽聽scanArgs[j]聽=聽&values[j]
聽聽聽聽}
聽聽聽聽for聽rows.Next()聽{
聽聽聽聽聽聽聽聽//将行数据保存到record字典
聽聽聽聽聽聽聽聽err聽=聽rows.Scan(scanArgs...)
聽聽聽聽聽聽聽聽for聽i,聽col聽:=聽range聽values聽{
聽聽聽聽聽聽聽聽聽聽聽聽if聽col聽!=聽nil聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽record[columns[i]]聽=聽string(col.([]byte))
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}
聽聽聽聽}
聽聽聽聽return聽record
}
/**
聽*聽从es搜索
聽*/
func聽esSearch()聽{
聽聽聽聽termQuery聽:=聽elastic.NewTermQuery("content",聽"中国")
聽聽聽聽searchResult,聽err聽:=聽client.Search().
聽聽聽聽聽聽聽聽Index("ikindex").Type("fulltext").聽//聽search聽in聽index聽"twitter"
聽聽聽聽聽聽聽聽Query(termQuery).聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽specify聽the聽query
聽聽聽聽聽聽聽聽//Sort("user",聽true).聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽sort聽by聽"user"聽field,聽ascending
聽聽聽聽聽聽聽聽From(0).Size(10).聽//聽take聽documents聽0-9
聽聽聽聽聽聽聽聽Pretty(true).聽聽聽聽聽//聽pretty聽print聽request聽and聽response聽JSON
聽聽聽聽聽聽聽聽Do()聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽execute
聽聽聽聽if聽err聽!=聽nil聽{
聽聽聽聽聽聽聽聽//聽Handle聽error
聽聽聽聽聽聽聽聽panic(err)
聽聽聽聽}
聽聽聽聽//聽searchResult聽is聽of聽type聽SearchResult聽and聽returns聽hits,聽suggestions,
聽聽聽聽//聽and聽all聽kinds聽of聽other聽information聽from聽Elasticsearch.
聽聽聽聽fmt.Printf("Query聽took聽%d聽milliseconds\n",聽searchResult.TookInMillis)
聽聽聽聽fmt.Println(searchResult.Hits.TotalHits)
聽聽聽聽fmt.Println("Hello聽World!")
}
本文出自 “大胡学习笔记” 博客,请务必保留此出处http://zhixinhu.blog.51cto.com/3132199/1835287
有疑问加站长微信联系(非本文作者)