elastic_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package elastic
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. es "github.com/olivere/elastic/v7"
  6. "testing"
  7. )
  8. var esClinet Elastic
  9. //func init() {
  10. // esClinet = Elastic{
  11. // S_esurl: "127.0.0.1:19805",
  12. // I_size: 2,
  13. // Username: "es_all",
  14. // Password: "TopJkO2E_d1x",
  15. // }
  16. // fmt.Println(esClinet)
  17. //
  18. // esClinet.InitElasticSize()
  19. //
  20. //}
  21. func TestCount(t *testing.T) {
  22. esClinet = Elastic{
  23. S_esurl: "http://127.0.0.1:19805",
  24. I_size: 2,
  25. Username: "es_all",
  26. Password: "TopJkO2E_d1x",
  27. }
  28. esClinet.InitElasticSize()
  29. rangeQuery := es.NewRangeQuery("id").Gte("6463a8800000000000000000").Lt("6464fa000000000000000000")
  30. termsQuery := es.NewTermsQuery("site", "元博网(采购与招标网)", "中国招标与采购网")
  31. boolQuery := es.NewBoolQuery().Must(rangeQuery, termsQuery)
  32. bytes, err := json.Marshal(boolQuery)
  33. if err != nil {
  34. fmt.Println("err", err.Error())
  35. }
  36. fmt.Println("qqq", string(bytes))
  37. count1 := esClinet.Count("bidding", "", boolQuery)
  38. fmt.Println(count1)
  39. }
  40. func TestBulkSave(t *testing.T) {
  41. esClinet = Elastic{
  42. S_esurl: "http://127.0.0.1:19805",
  43. I_size: 2,
  44. Username: "es_all",
  45. Password: "TopJkO2E_d1x",
  46. }
  47. esClinet.InitElasticSize()
  48. id := "6398a381063a7b816e072adf"
  49. esUpdate := map[string]interface{}{
  50. "area": "新疆",
  51. "city": "伊犁哈萨克自治州",
  52. "district": "奎屯市",
  53. }
  54. updateEs := []map[string]interface{}{
  55. {"_id": id},
  56. esUpdate,
  57. }
  58. arru := make([][]map[string]interface{}, 0)
  59. arru = append(arru, updateEs)
  60. esClinet.UpdateBulk("bidding", arru...)
  61. fmt.Println(arru)
  62. }