新聞中心
這篇文章主要講解了“elasticsearch怎么使用”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“elasticsearch怎么使用”吧!

10年積累的成都網(wǎng)站建設(shè)、成都網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有磴口免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
es.go
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())
}
}MySQL.go
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)
}model.go
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)
}main.go
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
}test/main.go
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)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
文章標(biāo)題:elasticsearch怎么使用
當(dāng)前鏈接:http://m.biofuelwatch.net/article/ghgcpp.html


咨詢
建站咨詢
