package elastic import ( "fmt" "log" "qfw/util/mongodb" "testing" "time" "gopkg.in/mgo.v2/bson" _ "gopkg.in/olivere/elastic.v1" ) func Test_Find1(t *testing.T) { InitElasticSize("http://192.168.3.18:9800", 5) log.Println(Get("service_v1", "service", `{"query":{"bool":{"must":[],"must_not":[],"should":[],"minimum_should_match" : 1}},"_source":["_id","s_name"],"sort":[{"l_createdate":"desc"}]}`)) } func Test_save(t *testing.T) { InitElastic("http://192.168.3.14:9800") //client.CreateIndex("test").Body(testMapping).Do() bid := bson.NewObjectId() map1 := map[string]interface{}{ "_id": bid, "name": "不知道,就是你们我的", "age": 503, "addr": "可就不呀,2245555555是不是5555", } b := Save("test", "test", &map1) log.Println(b, bid, bid.String()) //log.Println("保存结果", b, DelById("test", "test", bid.String())) } func Test_GoQ(t *testing.T) { InitElastic("http://192.168.3.18:9800") //"$中不支持range查询" //res := GetPage("bidding", "bidding", `{"$or":[{"TERM_toptype":"拟建"},{"TERM_toptype":"结果"}],"extracttype":{"$gt":1},"TERM_area": "广东","$and":[{"TERM_area": "广东"}{"TERM_area": "广东"}]}`, `{"id":-1}`, `"title","toptype"`, 0, 50) res := GetPage("bidding", "bidding", `{"$or":[{"TERM_toptype":"拟建"},{"TERM_toptype":"结果"}]}`, `{"publishtime":-1}`, `"title","toptype","publishtime"`, 0, 50) log.Println(res) } func Test_get(t *testing.T) { InitElastic("http://192.168.3.14:9800") a := make(chan int, 1) for i := 0; i < 1000; i++ { go func(b int) { log.Println(b, len(*GetPage("content", "content", `{}`, "", "", 0, 20))) }(i) } <-a //Q1 := `{"name":"*走*"}` //支持类mongodb的sql中的$and,$or $gt $lt //Q1 := `{"age":{"$lt":11}}` //log.Println("解析Q1,生成elastic所有的DSL,一般不直接调用:", AnalyQuery(Q1, "", QStr)) //log.Println("解析Q1、排序、显示字段,分页,生成elastic所有的DSL,一般不直接调用:", MakeQuery(Q1, `{"name":1,"age":-1}`, `"name","age"`, -1, 0)) //log.Println("分页查询,排序和显示字段,分页:", GetPage("test", "test", Q1, "", "", 0, 2)) //原生的查询方法,需要自己写DSL查询 //s := Get("test", `{"query" : { "bool": { "must":[{"query_string":{"default_field":"name","query":"*"}}]}}}`) //s := Get("test", "test", `{"query":{"match_all":{}}}`) //log.Println("查找全部", s) } func Test_GetById(t *testing.T) { InitElastic("http://192.168.3.14:9800") log.Println("GetById,获取对象", GetById("test", "test", "1", "553ddbc7a75c550890000001")) } func Test_DelType(t *testing.T) { InitElastic("http://192.168.3.14:9800") //Del("qfw", "enterprise", MakeQuery(`{}`, "", "", -1, -1)) c := GetEsConn() //c.Delete().Index("qfw").Type("enterprise").Do() //c.Refresh("qfw") c.Alias().Add("qfw", "qfw_back").Do() } func Test_Del(t *testing.T) { InitElastic("http://192.168.3.14:9800") /* Save("test", "test", map[string]interface{}{ "_id": 291, "name": "走啊走", "name2": map[string]interface{}{ "aa": "完全不好使", "bb": []int{1, 2, 3}, "cc": 29, }, "age": 29, "num": 112, "addr": "可就不呀,224是不是", }) */ //log.Println(Get("test", "test", MakeQuery(`{"name2.aa":"0"}`, "", "", -1, -1))) //log.Println("--:--", es.NewQueryStringQuery(`{"query":{"match":{"age":50}}}`).Query) //log.Println("1:", MakeQuery(`{"_id":2}`, "", "", -1, -1)) //log.Println("Del方法,删除:", Del("test", "test", MakeQuery(`{"$and":[{"age":{"$gte":28}},{"age":{"$lt":30}}],"TERM_num":112,"name2.cc":29}`, "", "", -1, -1))) //log.Println("Del方法,删除:", Del("test", "test", MakeQuery(`{}`, "", "", -1, -1))) DelById("qfw", "content", "AU2P5rW95E6XJdgtD0Eh") } func Test_Update(t *testing.T) { InitElastic("http://192.168.3.14:9800") log.Println("更新结果", Update("test", "test", "9", `ctx._source.age=101;ctx._source.name="张三"`)) } func Test_gg(t *testing.T) { InitElastic("http://192.168.3.14:9800") vv := Get("service", "service", `{"query":{"term":{"s_enterpriseid":"`+"555446f6120d8f654414a81f"+`"}}, _source:[`+`"_id"`+`], sort:{"l_createdate":"desc"}, from:`+fmt.Sprintf("%v", 0)+`, size:`+fmt.Sprintf("%v", 10)+` }`) log.Println(vv) } func Test_DelById(t *testing.T) { InitElastic("http://192.168.3.14:9800") // Save("test", "test", map[string]interface{}{ // "_id": 101, // "name": "走啊走", // "name2": map[string]interface{}{ // "aa": "完全不好使", // }, // "age": 220, // "addr": "可就不呀,224是不是", // }) log.Println("删除结果", DelById("test", "test", "559334fb91db0a21a400000a")) } //先删除后增 func Test_UpdateNewDoc(t *testing.T) { InitElastic("http://192.168.3.14:9800") log.Println("完整更新对象", UpdateNewDoc("service", "service", `{"_id":56497eb0bbb93e2070000002,"name5":"我是新对象3~","tt":{"name":"内嵌的name"}}`)) } //批量保存/更新 func Test_BulkSave(t *testing.T) { InitElastic("http://192.168.3.14:9800") // res := []map[string]interface{}{ // map[string]interface{}{ // "_id": 51, // "name9": "map第11", // }, // map[string]interface{}{ // "_id": 52, // "name9": "map第22", // }, // map[string]interface{}{ // "_id": 53, // "name9": "map第33", // }, // } mongodb.InitMongodbPool(1, "192.168.3.14:27080", "qfw") // res := mongodb.Find("bidding", `{"infoformat":2}`, nil, nil, false, 0, 50) // for _, v := range *res { // Save("bidding", "bidding", v) // } res := mongodb.FindById("bidding", "5833a0452d76c41b289b41f7", nil) log.Println("res:", res) Save("bidding", "bidding", res) log.Println("批量保存对象:") //BulkSave("test", "test", &res, true) } //批量更新字段 func Test_BulkUpdate(t *testing.T) { InitElastic("http://192.168.3.18:9800") //regno 540192600031442,542422600000086 ids := []string{"55a85997a442ceca9e20de9f", "55a85997a442ceca9e20deaf"} BulkUpdate("enterprise", "enterprise", ids, `ctx._source.Ycml=false`) } //获取数据,不受限制 func Test_getNoLimit(t *testing.T) { InitElasticSize("http://192.168.3.18:9800", 1) k := "EntType" v := "9600" query := `{"query": {"bool":{"must":[{"query_string":{"default_field":"` + k + `","query":"` + v + `"}},{"query_string":{"default_field":"Ycml","query":true}}]}},"_source": ["_id","EntName"],"from": 0,"size": 999999}` nt := GetNoLimit("enterprise", "enterprise", query) log.Println("总数", len(*nt)) } func Test_Count(t *testing.T) { log.Println("ESCOUNT", Count("test", "test", MakeQuery(`{"name":"你们","TERM_age":503}`, "", "", -1, -1))) } func Test_tmp(t *testing.T) { InitElastic("http://192.168.3.14:9800") DelById("qfw", "enterprise", "55633f50a442ab325175252b") } func Test_updateEntDoc(t *testing.T) { InitElastic("http://192.168.3.14:9800") mongodb.InitMongodbPool(1, "192.168.3.14:27080", "qfw") UpdateEntDoc("556d858ec2e875307286f868") } func Test_first(t *testing.T) { InitElasticSize("http://192.168.3.14:9800", 1) nt := GetPage("content", "content", `{"releasetime": {"$lt": 1440773202}, "s_contenttype": "qykb"}`, `{"releasetime":-1}`, `{"s_title":1}`, 0, 2) var i = 0 for _, v := range *nt { log.Println("\n", v, i) i++ } } func Test_date(t *testing.T) { //"1448267541" //s := time.Now().UnixNano() //log.Println(s, time.Now().Unix(), fmt.Sprintf("%d", s)[4:12], 999999/60/60) t1 := time.Now().Unix() t2 := t1 + 10000 tt1 := time.Unix(t1, 0) tt2 := time.Unix(t2, 0) nt1 := time.Date(tt1.Year(), tt1.Month(), tt1.Day(), 0, 0, 0, 0, time.Local) nt2 := time.Date(tt2.Year(), tt2.Month(), tt2.Day(), 0, 0, 0, 0, time.Local) log.Println((nt2.Unix() - nt1.Unix()) / 86400) } func Test_Getpage(t *testing.T) { InitElasticSize("http://192.168.3.18:9800", 1) data := *GetPage("enterprise", "enterprise", `{"s_action":"01"}`, `{"l_submittime":-1}`, `"EntName","l_submittime","_id"`, 0, 8) log.Println("data:", data) } func Test_q(t *testing.T) { //log.Println(MakeQuery(`{"_id":"556349a6a442ab325177ade0"}`, "", `"_id"`, -1, -1)) //Count("enterprise","enterprise",) } func Test_bidding(t *testing.T) { InitElasticSize("http://192.168.3.18:9800", 1) //log.Println(Count("bidding_v1", "bidding", GetNgramQuery("河南+工商", `"title"`))) log.Println(GetByNgramAll("bidding_v1", "bidding", "河南+郑州", `{"term":{"_id":"57aadfdf61a0721f1519c19d"}}`, `"title","detail"`, `{"publishtime":-1}`, `"title","_id"`, 0, 10, true, false)) //res := GetPage("bidding", "bidding", "", `{"comeintime":-1}`, `"_id"`, 0, 1) //log.Println((*res)[0]["_id"]) }