search_index.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. "gopkg.in/guregu/null.v3"
  9. "github.com/blevesearch/bleve"
  10. "github.com/blevesearch/bleve/mapping"
  11. _ "xiaoniaokuaiyan.com/xiaoniao/search/analyzer"
  12. "xiaoniaokuaiyan.com/xiaoniao/util"
  13. )
  14. type DetectItem struct {
  15. Name string `db:"name" json:"name"`
  16. //MarketPrice float32 `db:"market_price" json:"marketPrice"`
  17. }
  18. type ProductDB struct {
  19. Id string `db:"id" json:"id"`
  20. Name string `db:"name" json:"name"`
  21. Price int `db:"price" json:"price"`
  22. IsRecommend int `db:"is_recommend" json:"isRecommend"`
  23. TKeywords null.String `db:"keywords" json:"tkeywords"`
  24. Keywords string `db:"-" json:"keywords"`
  25. Picture string `db:"picture" json:"picture"`
  26. Items string `db:"-" json:"items"`
  27. CityIds []string `db:"-" json:"cityIds"`
  28. Pinyin string `db:"-" json:"py"`
  29. JP string `db:"-" json:"jp"`
  30. Cates []string `db:"-" json:"cates"`
  31. SaleNum int `db:"sale_num" json:"sale_num"`
  32. SortNo int `db:"sort_no" json:"sort_no"`
  33. CreatedAt string `db:"created_at" json:"created_at"`
  34. TPutawayTime null.String `db:"putaway_time" json:"-"`
  35. PutawayTime string `db:"-" json:"putaway_time"`
  36. MarketPrice int `db:"market_price" json:"market_price"`
  37. }
  38. func buildIndexMapping() (*mapping.IndexMappingImpl, error) {
  39. indexMapping := bleve.NewIndexMapping()
  40. err := indexMapping.AddCustomTokenizer("gojieba",
  41. map[string]interface{}{
  42. "dictpath": "dict/jieba.dict.utf8", //gojieba.DICT_PATH,
  43. "hmmpath": "dict/hmm_model.utf8", //gojieba.HMM_PATH,
  44. "userdictpath": "dict/user.dict.utf8", //gojieba.USER_DICT_PATH,
  45. "idf": "dict/idf.utf8", //gojieba.IDF_PATH,
  46. "stop_words": "dict/stop_words.utf8", //gojieba.STOP_WORDS_PATH,
  47. "type": "gojieba",
  48. },
  49. )
  50. if err != nil {
  51. panic(err)
  52. }
  53. err = indexMapping.AddCustomAnalyzer("gojieba",
  54. map[string]interface{}{
  55. "type": "gojieba",
  56. "tokenizer": "gojieba",
  57. },
  58. )
  59. if err != nil {
  60. panic(err)
  61. }
  62. indexMapping.DefaultAnalyzer = "gojieba"
  63. pyFieldMapping := mapping.NewTextFieldMapping()
  64. pyFieldMapping.Name = "py"
  65. pyFieldMapping.Analyzer = "standard"
  66. jpFieldMapping := mapping.NewTextFieldMapping()
  67. jpFieldMapping.Name = "jp"
  68. jpFieldMapping.Analyzer = "standard"
  69. cityFieldMapping := mapping.NewTextFieldMapping()
  70. cityFieldMapping.Name = "cityIds"
  71. cityFieldMapping.Analyzer = "standard"
  72. catesMapping := mapping.NewTextFieldMapping()
  73. catesMapping.Name = "cates"
  74. catesMapping.Analyzer = "standard"
  75. pyMapping := mapping.NewDocumentMapping()
  76. pyMapping.AddFieldMappingsAt("py", pyFieldMapping)
  77. pyMapping.AddFieldMappingsAt("jp", jpFieldMapping)
  78. pyMapping.AddFieldMappingsAt("cityIds", cityFieldMapping)
  79. pyMapping.AddFieldMappingsAt("cates", catesMapping)
  80. pictureFieldMapping := bleve.NewTextFieldMapping()
  81. pictureFieldMapping.Index = false
  82. pyMapping.AddFieldMappingsAt("picture", pictureFieldMapping)
  83. //pyMapping.AddSubDocumentMapping("picture", bleve.NewDocumentStaticMapping())
  84. //pyMapping.AddSubDocumentMapping("picture", bleve.NewDocumentDisabledMapping())
  85. pyMapping.AddSubDocumentMapping("tkeywords", bleve.NewDocumentDisabledMapping())
  86. //indexMapping.AddDocumentMapping("pinyin", pyMapping)
  87. indexMapping.DefaultMapping = pyMapping
  88. return indexMapping, nil
  89. }
  90. func dump(idx bleve.Index) {
  91. ma := bleve.NewMatchAllQuery()
  92. request := bleve.NewSearchRequest(ma)
  93. request.Fields = []string{"id", "price"}
  94. request.SortBy([]string{"_id"})
  95. request.Size = 1000
  96. dc, _ := idx.DocCount()
  97. fmt.Println(dc)
  98. sr, err := idx.Search(request)
  99. if err != nil {
  100. log.Fatal(err)
  101. }
  102. //fmt.Println(sr)
  103. for _, hit := range sr.Hits {
  104. fmt.Printf("%s\t,%v\n", hit.Fields["id"], hit.Fields["price"])
  105. }
  106. }
  107. func main() {
  108. isDump := flag.Bool("dump", false, "--dump")
  109. flag.Parse()
  110. mapping, err := buildIndexMapping()
  111. if err != nil {
  112. log.Fatal(err)
  113. }
  114. /*docMapping := mapping.DefaultMapping
  115. docMapping.AddSubDocumentMapping("isRecommend", bleve.NewDocumentStaticMapping())
  116. docMapping.AddSubDocumentMapping("picture", bleve.NewDocumentStaticMapping())
  117. */
  118. indexName := "xn_product.bleve"
  119. var productIndex bleve.Index
  120. var ierr error
  121. if _, err := os.Stat(indexName); os.IsNotExist(err) {
  122. productIndex, ierr = bleve.New(indexName, mapping)
  123. } else if err == nil {
  124. if *isDump {
  125. productIndex, ierr = bleve.OpenUsing(indexName, map[string]interface{}{
  126. "read_only": true,
  127. })
  128. } else {
  129. productIndex, ierr = bleve.Open(indexName)
  130. }
  131. }
  132. if ierr != nil {
  133. log.Fatal(ierr)
  134. }
  135. defer productIndex.Close()
  136. if *isDump {
  137. dump(productIndex)
  138. return
  139. }
  140. /*p := Product{
  141. Id: "200817010121",
  142. No: "sdsdfsjosdifkids",
  143. Name: "空腹血糖",
  144. Tags: "血糖",
  145. Tips: "需要空腹测试",
  146. }
  147. productIndex.Index(p.Id, p)
  148. */
  149. strSql := "select id, name,price, is_recommend, picture, keywords,sale_num,putaway_time,sort_no,created_at,market_price from t_product where is_putaway = 0 and is_delete = 0;"
  150. db := util.GetSqlDB()
  151. productList := []ProductDB{}
  152. err = db.Select(&productList, strSql)
  153. if err != nil {
  154. log.Fatal(err)
  155. }
  156. for _, product := range productList {
  157. strSql := "select t1.name from t_detect_product t1 left join t_product_detect_product t2 on t1.id = t2.detect_product_id where t2.product_id = " + product.Id
  158. itemList := []string{}
  159. db.Select(&itemList, strSql)
  160. //strSql = "select tag_name from t_product_tag t1 left join t_tag t2 on t1.tag_id = t2.id where t1.product_id = ?"
  161. //tagList := []string{}
  162. //db.Select(&tagList, strSql, product.Id)
  163. strSql = "select city_id from v_product_city where product_id = ?"
  164. cityIds := []string{}
  165. db.Select(&cityIds, strSql, product.Id)
  166. product.Items = strings.Join(itemList, " ")
  167. //product.Tags = strings.Join(tagList, " ")
  168. product.CityIds = cityIds
  169. product.Pinyin, product.JP, _ = util.GetPinyin(product.Name)
  170. product.Keywords = product.TKeywords.String
  171. product.PutawayTime = product.TPutawayTime.String
  172. strSql = "SELECT cat_id from t_product_category_product where product_id = ?"
  173. var cateIds = []string{}
  174. db.Select(&cateIds, strSql, product.Id)
  175. product.Cates = cateIds
  176. productIndex.Index(product.Id, product)
  177. }
  178. //bytes, _ := json.Marshal(&productList)
  179. //fmt.Println(string(bytes))
  180. }