您好,登錄后才能下訂單哦!
這篇文章主要講解了“elasticsearch怎么使用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“elasticsearch怎么使用”吧!
package main import ( "context" "github.com/olivere/elastic" "time" ) var ( esUrl = "http://s10:19200" ctx = context.Background() client *elastic.Client ) func init() { var err error client, err = elastic.NewClient( elastic.SetSniff(false), elastic.SetURL(esUrl), elastic.SetHealthcheckInterval(10*time.Second), elastic.SetMaxRetries(5), ) if err != nil { panic(err.Error()) } }
package main import ( _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/xorm" ) var ( db *xorm.EngineGroup ) func init() { conns := []string{"test:test@tcp(127.0.0.1:3306)/test"} var err error db, err = xorm.NewEngineGroup("mysql", conns) if err != nil { panic(err.Error()) } db.ShowSQL(true) db.SetMaxIdleConns(5) db.SetMaxOpenConns(10) }
package main import ( "encoding/json" "time" ) type StudentAnswer struct { Id int64 `json:"id"` Qid int64 `json:"qid"` QuesType int `json:"ques_type"` ClassType int `json:"class_type"` LessonId int64 `json:"lesson_id"` ScheduleId int64 `json:"schedule_id"` IsFirstSubmit int `json:"is_first_submit"` IsRight int `json:"is_right"` StuAnswer string `json:"stu_answer"` Scores string `json:"scores"` StudentId int64 `json:"student_id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } func (*StudentAnswer) TableName() string { return "student_answer" } func (sa *StudentAnswer) String() string { buf, _ := json.Marshal(sa) return string(buf) }
package main import ( "fmt" "github.com/olivere/elastic" "googo.io/goo/log" "sync" ) var ( MaxId = int64(0) pageSize = 1000 index = "stu-answer" wg sync.WaitGroup ch = make(chan int64, 1000) data = make(chan []StudentAnswer) ) func init() { // client.DeleteIndex(index).Do(ctx) // client.CreateIndex(index).Do(ctx) } func main() { wg.Add(1) go func() { defer wg.Done() for { rows := getRows() fmt.Println(len(rows)) if rows == nil || len(rows) == 0 { break } MaxId = rows[len(rows)-1].Id data <- rows } }() wg.Add(1) go func() { defer wg.Done() for { select { case rows := <-data: el := client.Bulk().Index(index) for _, row := range rows { el.Add(elastic.NewBulkIndexRequest().Id(fmt.Sprintf("%d", row.Id)).Doc(row)) } if _, err := el.Do(ctx); err != nil { gooLog.Error(err.Error()) } } } }() wg.Wait() } func getRows() []StudentAnswer { rows := []StudentAnswer{} err := db.Where("id > ?", MaxId).Limit(pageSize).Find(&rows) if err != nil { gooLog.Error(err.Error()) return nil } return rows }
package main import ( "encoding/json" "fmt" "github.com/olivere/elastic" ) // SELECT COUNT(*) AS cnt FROM (SELECT id FROM `student_answer` // WHERE lesson_id IN (633,63,635,636,665,668) AND is_first_submit = 1 AND class_type = 2 // GROUP BY `student_id`) AS a var ( index = "jy_edu_student_answer" ) func main() { q := elastic.NewBoolQuery().Must( elastic.NewTermsQuery("lesson_id", 633, 634, 635, 636, 665, 668), elastic.NewMatchQuery("is_first_submit", "1"), elastic.NewMatchQuery("class_type", "2"), ) agg := elastic.NewTermsAggregation().Field("student_id") cnt, err := client.Count().Index(index).Do(ctx) fmt.Println(cnt, err) cnt, err = client.Count().Index(index).Query(q).Do(ctx) fmt.Println(cnt, err) rst, _ := client.Search().Index(index).Aggregation("agg_student_id", agg).Query(q).Size(0).Do(ctx) buf, _ := json.Marshal(rst) fmt.Println(string(buf)) }
感謝各位的閱讀,以上就是“elasticsearch怎么使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)elasticsearch怎么使用這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。