query.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package model
  2. import (
  3. "encoding/json"
  4. "gopkg.in/olivere/elastic.v5"
  5. )
  6. // QueryParams .
  7. type QueryParams struct {
  8. Business string `form:"business" params:"business;Required" validate:"required"`
  9. QueryBodyStr string `form:"query" params:"query;Required" validate:"required"`
  10. DebugLevel int `form:"debug_level" params:"debug_level" default:"0"` // 2 默认全局debug,(包含dsl执行后的分析),1 dsl执行前的分析,防止504啥分析看不到。通过 /x/admin/search/query/debug (※包含es query体是否正确 + dsl体 + explain 返回信息) 方式,非签名请求
  11. QueryBody *QueryBody
  12. AppIDConf *QueryConfDetail
  13. }
  14. // QueryBody .
  15. type QueryBody struct {
  16. Fields []string `json:"fields"` // default:"*" _source,default = *
  17. From string `json:"from"` //索引名,多个用逗号隔开
  18. Where *QueryBodyWhere `json:"where"`
  19. Order []map[string]string `json:"order"`
  20. OrderScoreFirst bool `json:"order_score_first"`
  21. OrderRandomSeed string `json:"order_random_seed"` // 随机排序种子
  22. Scroll bool `json:"scroll"`
  23. Highlight bool `json:"highlight"` //default:"false"
  24. Pn int `json:"pn"` //Range(1,5000) default:"1"
  25. Ps int `json:"ps"` //Range(1,1000) default:"10"
  26. }
  27. // QueryBodyWhere .
  28. type QueryBodyWhere struct {
  29. EQ map[string]interface{} `json:"eq"` //可能是数据或字符,[12,333,67] ["asd", "你好"]
  30. Or map[string]interface{} `json:"or"` //暂时不支持minimum should
  31. In map[string][]interface{} `json:"in"` //TODO改造为slice
  32. Range map[string]string `json:"range"` //[10,20) (2018-05-10 00:00:00,2018-05-31 00:00:00] (,30]
  33. Like []QueryBodyWhereLike `json:"like"`
  34. Enhanced []QueryBodyWhereEnhanced `json:"enhanced"` //包含GourpBy Collapse
  35. Combo []QueryBodyWhereCombo `json:"combo"` //混合与或
  36. Not map[string]map[string]bool `json:"not"` //对eq、in、range条件取反
  37. }
  38. // QueryBodyWhereLike .
  39. type QueryBodyWhereLike struct {
  40. KWFields []string `json:"kw_fields"`
  41. KW []string `json:"kw"` //将kw的值使用空白间隔给query
  42. Or bool `json:"or"` //default:"false"
  43. Level string `json:"level"` //默认default
  44. }
  45. // QueryBodyWhereEnhanced .
  46. type QueryBodyWhereEnhanced struct {
  47. Mode string `json:"mode"`
  48. Field string `json:"field"`
  49. Order []map[string]string `json:"order"`
  50. Size int `json:"size"` //todo:sdk增加子集返回数
  51. // more conditions...
  52. }
  53. // QueryBodyWhereCombo .
  54. type QueryBodyWhereCombo struct {
  55. EQ []map[string]interface{} `json:"eq"`
  56. In []map[string][]interface{} `json:"in"`
  57. Range []map[string]string `json:"range"`
  58. NotEQ []map[string]interface{} `json:"not_eq"`
  59. NotIn []map[string][]interface{} `json:"not_in"`
  60. NotRange []map[string]string `json:"not_range"`
  61. Min struct {
  62. EQ int `json:"eq"`
  63. In int `json:"in"`
  64. Range int `json:"range"`
  65. NotEQ int `json:"not_eq"`
  66. NotIn int `json:"not_in"`
  67. NotRange int `json:"not_range"`
  68. Min int `json:"min"`
  69. } `json:"min"`
  70. }
  71. // QueryConfDetail .
  72. type QueryConfDetail struct {
  73. ESCluster string
  74. IndexPrefix string
  75. IndexType string
  76. IndexID string
  77. IndexMapping string
  78. MaxIndicesNum int
  79. QueryMode int //1:默认完全走查询体 2:基于查询体的定制 3:nested查询
  80. MaxPageSize int //最大page size
  81. }
  82. // QueryResult query result.
  83. type QueryResult struct {
  84. Order string `json:"order"`
  85. Sort string `json:"sort"`
  86. Result json.RawMessage `json:"result"`
  87. Debug *QueryDebugResult `json:"debug"`
  88. Page *Page `json:"page"`
  89. }
  90. // QueryDebugResult query result.
  91. type QueryDebugResult struct {
  92. ErrMsg []string `json:"err_msg"`
  93. QueryBody string `json:"query_body"`
  94. DSL string `json:"dsl"`
  95. Mapping map[string]interface{} `json:"mapping"`
  96. Profile *elastic.SearchProfile `json:"profile"` //性能分析
  97. }
  98. // AddErrMsg .
  99. func (qdr *QueryDebugResult) AddErrMsg(msg ...string) {
  100. qdr.ErrMsg = append(qdr.ErrMsg, msg...)
  101. }
  102. // UpsertResult upsert result.
  103. type UpsertResult struct {
  104. }
  105. var (
  106. // QueryModeBasic completely using basic query & nested .
  107. QueryModeBasic = 1
  108. // QueryModeExtra write some extra conditions under basic query .
  109. QueryModeExtra = 2
  110. // EnhancedModeGroupBy group by .
  111. EnhancedModeGroupBy = "group_by"
  112. // EnhancedModeSum sum from a filed .
  113. EnhancedModeSum = "sum"
  114. // EnhancedModeCollapse collapse .
  115. EnhancedModeCollapse = "collapse"
  116. // EnhancedModeDistinct distinct .
  117. EnhancedModeDistinct = "distinct"
  118. // EnhancedModeDistinctCount distinct .
  119. EnhancedModeDistinctCount = "distinct_count"
  120. // EnhancedModeGroupBySum group by sum .
  121. EnhancedModeGroupBySum = "group_by_sum"
  122. // EnhancedModeGroupByTop top hits .
  123. EnhancedModeGroupByTop = "group_by_tophits"
  124. // LikeLevelHigh high level .
  125. LikeLevelHigh = "high"
  126. // LikeLevelMiddel middle level .
  127. LikeLevelMiddel = "middle"
  128. // LikeLevelLow low level .
  129. LikeLevelLow = "low"
  130. // QueryConf 自定义部分
  131. QueryConf = map[string]*QueryConfDetail{
  132. "archive_video_score": {ESCluster: "ssd_archive", IndexPrefix: "archive_video", MaxIndicesNum: 1, QueryMode: QueryModeExtra},
  133. "archive_score": {ESCluster: "ssd_archive", IndexPrefix: "archive", MaxIndicesNum: 1, QueryMode: QueryModeExtra},
  134. "task_qa_random": {ESCluster: "internalPublic", IndexPrefix: "task_qa", MaxIndicesNum: 1, QueryMode: QueryModeExtra},
  135. "esports_contests_date": {ESCluster: "pcie_pub_out01", IndexPrefix: "esports_contests_map", MaxIndicesNum: 1, QueryMode: QueryModeExtra},
  136. "creative_archive_search": {ESCluster: "pcie_pub_out01", IndexPrefix: "creative_archive", MaxIndicesNum: 1, QueryMode: QueryModeExtra},
  137. "creative_archive_staff": {ESCluster: "pcie_pub_out02", IndexPrefix: "creative_archive", IndexID: "%d,id", IndexType: "base", MaxIndicesNum: 1, QueryMode: QueryModeExtra},
  138. "creative_archive_apply": {ESCluster: "pcie_pub_out02", IndexPrefix: "creative_archive", MaxIndicesNum: 1, QueryMode: QueryModeExtra},
  139. "dm_history": {ESCluster: "dmout", IndexPrefix: "dm_search", MaxIndicesNum: 1, QueryMode: QueryModeExtra},
  140. // "pgc_contract_info": {ESCluster: "pcie_pub_out01", IndexPrefix: "pgc_contract_info", MaxIndicesNum: 1, QueryMode: QueryModeNested},
  141. // "pgc_contract_video": {ESCluster: "pcie_pub_out01", IndexPrefix: "pgc_contract_video", MaxIndicesNum: 1, QueryMode: QueryModeNested},
  142. }
  143. // PermConf 权限业务
  144. PermConf = map[string]map[string]string{
  145. "star": {"ops_log_billions": "true"}, // 业务使用*批量获取索引
  146. "scroll": {"dm_search": "true"}, // 业务使用scroll
  147. "oht": {"creative_reply": "true", "creative_reply_isreport": "true", "esports": "true"}, // 业务max_result_window 100k
  148. "es_cache": {"comics_firebird": "true", "pgc_media": "true", "pgc_season": "true"}, // request cache(失效时间和索引的refresh_interval一致)
  149. //"routing": {"creative_reply": "o_mid"},
  150. }
  151. )