您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了“elasticsearch怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“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怎么使用”的内容了,经过本文的学习后,相信大家对elasticsearch怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。