Mapping:
商品索引结构:

  1. PUT /career_plan_sku_index_15
  2. {
  3. "settings": {
  4. "number_of_shards": 3,
  5. "number_of_replicas": 1
  6. },
  7. "mappings": {
  8. "properties": {
  9. "skuId": {
  10. "type": "keyword"
  11. },
  12. "skuName": {
  13. "type": "text",
  14. "analyzer": "ik_max_word",
  15. "search_analyzer": "ik_smart"
  16. },
  17. "category": {
  18. "type": "keyword"
  19. },
  20. "basePrice": {
  21. "type": "integer"
  22. },
  23. "vipPrice": {
  24. "type": "integer"
  25. },
  26. "saleCount": {
  27. "type": "integer"
  28. },
  29. "commentCount": {
  30. "type": "integer"
  31. },
  32. "skuImgUrl": {
  33. "type": "keyword",
  34. "index": false
  35. },
  36. "createTime": {
  37. "type": "date",
  38. "format": "yyyy-MM-dd HH:mm:ss"
  39. },
  40. "updateTime": {
  41. "type": "date",
  42. "format": "yyyy-MM-dd HH:mm:ss"
  43. }
  44. }
  45. }
  46. }

suggest索引
索引2:

  1. PUT /career_plan_sku_suggest_15
  2. {
  3. "settings": {
  4. "number_of_shards": 3,
  5. "number_of_replicas": 1,
  6. "analysis": {
  7. "analyzer": {
  8. "ik_and_pinyin_analyzer": {
  9. "type": "custom",
  10. "tokenizer": "ik_smart",
  11. "filter": "my_pinyin"
  12. }
  13. },
  14. "filter": {
  15. "my_pinyin": {
  16. "type": "pinyin",
  17. "keep_first_letter": true,
  18. "keep_full_pinyin": true,
  19. "keep_original": true,
  20. "remove_duplicated_term": true
  21. }
  22. }
  23. }
  24. },
  25. "mappings": {
  26. "properties": {
  27. "word1": {
  28. "type": "completion",
  29. "analyzer": "ik_and_pinyin_analyzer"
  30. },
  31. "word2": {
  32. "type": "text"
  33. }
  34. }
  35. }
  36. }
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/davecgh/go-spew/spew"
  6. "github.com/olivere/elastic/v7"
  7. "log"
  8. "os"
  9. )
  10. //https://www.cnblogs.com/gwyy/p/13356345.html 参数解释
  11. //tracelog 实现 elastic.Logger 接口
  12. type tracelog struct{}
  13. //实现输出
  14. func (tracelog) Printf(format string, v ...interface{}) {
  15. fmt.Printf(format, v...)
  16. }
  17. type Person struct {
  18. Name string `json:"name"`
  19. Age int `json:"age"`
  20. Married bool `json:"married"`
  21. }
  22. type Goods struct {
  23. SkuId int `json:"sku_id,omitempty"`
  24. SkuName string `json:"sku_name,omitempty"`
  25. Category string `json:"category,omitempty"`
  26. BasePrice float64 `json:"base_price,omitempty"`
  27. VipPrice int `json:"vip_price,omitempty"`
  28. SaleCount int `json:"sale_count,omitempty"`
  29. CommentCount int `json:"comment_count,omitempty"`
  30. SkuImgUrl string `json:"sku_img_url,omitempty"`
  31. CreateTime string `json:"create_time,omitempty"`
  32. UpdateTime string `json:"update_time,omitempty"`
  33. }
  34. func main() {
  35. client, err := elastic.NewClient(elastic.SetURL("http://127.0.0.1:9200"),
  36. elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)),
  37. elastic.SetTraceLog(new(tracelog)))
  38. if err != nil {
  39. panic(err)
  40. }
  41. fmt.Println("connect es success", client)
  42. p1 := Person{Name: "lmh", Age: 18, Married: false}
  43. put1, err := client.Index().Index("user").BodyJson(p1).Do(context.Background())
  44. if err != nil {
  45. // Handle error
  46. panic(err)
  47. }
  48. fmt.Printf("Indexed user %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
  49. spew.Dump(put1)
  50. // 批量操作
  51. //10001, 房屋卫士自流平美缝剂瓷砖地砖专用双组份真瓷胶防水填缝剂镏金色, 品质建材, 398.00, 上海, 540785126782
  52. //10002, 艾瑞泽手工大号小号调温热熔胶枪玻璃胶枪硅胶条热溶胶棒20W-100W, 品质建材,21.80, 山东青岛, 24727352473
  53. var AllGoods = []Goods{{
  54. SkuId: 10004,
  55. SkuName: "艾瑞泽手工大号小号调温热熔胶枪玻璃胶枪硅胶条热溶胶棒20W-100W",
  56. Category: "品质建材",
  57. BasePrice: 21.80,
  58. VipPrice: 0,
  59. SaleCount: 0,
  60. CommentCount: 0,
  61. SkuImgUrl: "www.baidu.com",
  62. CreateTime: "2022-02-22 22:22:22",
  63. UpdateTime: "2022-02-22 22:22:22",
  64. }, {
  65. SkuId: 10005,
  66. SkuName: "房屋卫士自流平美缝剂瓷砖地砖专用双组份真瓷胶防水填缝剂镏金色",
  67. Category: "品质建材",
  68. BasePrice: 398.00,
  69. VipPrice: 0,
  70. SaleCount: 0,
  71. CommentCount: 0,
  72. SkuImgUrl: "www.baidu.com",
  73. CreateTime: "2022-02-22 22:22:22",
  74. UpdateTime: "2022-02-22 22:22:22",
  75. }}
  76. bulkRequest := client.Bulk().Index("career_plan_sku_index_15")
  77. for _, goods := range AllGoods {
  78. // index 设置在bulk 或者 设置在indexrequest 都可以
  79. //doc := elastic.NewBulkIndexRequest().Index("career_plan_sku_index_15").Doc(goods)
  80. doc := elastic.NewBulkIndexRequest().Doc(goods)
  81. bulkRequest.Add(doc)
  82. }
  83. response, err := bulkRequest.Refresh("false").Do(context.Background())
  84. if err != nil {
  85. panic(err)
  86. }
  87. fmt.Println("response")
  88. spew.Dump(response)
  89. //failed := response.Failed()
  90. //l := len(failed)
  91. //if l > 0 {
  92. // fmt.Printf("Error(%d)", l, response.Errors)
  93. //}
  94. }

试试这个代码 是否可以 阻止 多个协成 降低 cpu

  1. quit := make(chan bool)
  2. for i := 0; i < 100 ; i++ {
  3. go func() {
  4. for {
  5. select {
  6. case v,ok<-quit:
  7. if ok{
  8. dosomething
  9. }else{
  10. brek
  11. }
  12. }
  13. }
  14. }()
  15. }
  16. time.Sleep(time.Second * 15)
  17. for i := 0; i != 5; i++ {
  18. quit <- true
  19. }

es 分词器

  • Standard Analyzer - 默认分词器,按词切分,小写处理
  • Simple Analyzer - 按照非字母切分(符号被过滤), 小写处理
  • Stop Analyzer - 小写处理,停用词过滤(the,a,is)
  • Whitespace Analyzer - 按照空格切分,不转小写
  • Keyword Analyzer - 不分词,直接将输入当作输出
  • Patter Analyzer - 正则表达式,默认\W+(非字符分割)
  • Language - 提供了30多种常见语言的分词器
  • Customer Analyzer 自定义分词器

es百万数量写入 - 图1
测试用法:
GET/POST /index_name/_analyze

  1. GET /career_plan_sku_index_15/_analyze
  2. {
  3. "field": "name", //指定分词的字段
  4. "text": ["我是中国人"], // 需要被分词的内容文本
  5. "analyzer":"stander" // 指定分词器
  6. }
  7. POST _analyze
  8. {
  9. "field": "name",
  10. "text": "我是中国人",
  11. "analyzer": "english"
  12. }
  13. POST _analyze
  14. {
  15. "text":"eating",
  16. "analyzer":"english"
  17. }

TODO: 创建索引时 指定字段的相关分词及分词器 的写法
查询内容时,可以指定分词器么?
suggest 分词 是要单独写一个 索引 还是在原来的索引上 添加即可