dm_search.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/search/model"
  6. elastic "gopkg.in/olivere/elastic.v5"
  7. )
  8. // DmSearch .
  9. func (d *Dao) DmSearch(c context.Context, p *model.DmSearchParams) (res *model.SearchResult, err error) {
  10. var (
  11. query = elastic.NewBoolQuery()
  12. indexName = fmt.Sprintf("dm_search_%03d", p.Oid%1000)
  13. )
  14. if p.Bsp.KW != "" {
  15. query = query.Must(elastic.NewRegexpQuery(p.Bsp.KwFields[0], ".*"+p.Bsp.KW+".*"))
  16. }
  17. if p.Oid != -1 {
  18. query = query.Filter(elastic.NewTermQuery("oid", p.Oid))
  19. }
  20. if p.Mid != -1 {
  21. query = query.Filter(elastic.NewTermQuery("mid", p.Mid))
  22. }
  23. if p.Mode != -1 {
  24. query = query.Filter(elastic.NewTermQuery("mode", p.Mode))
  25. }
  26. if p.Pool != -1 {
  27. query = query.Filter(elastic.NewTermQuery("pool", p.Pool))
  28. }
  29. if p.Progress != -1 {
  30. query = query.Filter(elastic.NewTermQuery("progress", p.Progress))
  31. }
  32. if len(p.States) > 0 {
  33. interfaceSlice := make([]interface{}, len(p.States))
  34. for k, m := range p.States {
  35. interfaceSlice[k] = m
  36. }
  37. query = query.Filter(elastic.NewTermsQuery("state", interfaceSlice...))
  38. }
  39. if p.Type != -1 {
  40. query = query.Filter(elastic.NewTermQuery("type", p.Type))
  41. }
  42. if len(p.AttrFormat) > 0 {
  43. interfaceSlice := make([]interface{}, len(p.AttrFormat))
  44. for k, m := range p.AttrFormat {
  45. interfaceSlice[k] = m
  46. }
  47. query = query.Filter(elastic.NewTermsQuery("attr_format", interfaceSlice...))
  48. }
  49. if p.CtimeFrom != "" {
  50. query = query.Filter(elastic.NewRangeQuery("ctime").Gte(p.CtimeFrom))
  51. }
  52. if p.CtimeTo != "" {
  53. query = query.Filter(elastic.NewRangeQuery("ctime").Lte(p.CtimeTo))
  54. }
  55. if res, err = d.searchResult(c, "dmExternal", indexName, query, p.Bsp); err != nil {
  56. PromError(fmt.Sprintf("es:%s ", p.Bsp.AppID), "%v", err)
  57. }
  58. return
  59. }