|
@@ -10,9 +10,10 @@ import (
|
|
|
dataSourceModel "eta/eta_api/models/data_source"
|
|
|
"eta/eta_api/utils"
|
|
|
"fmt"
|
|
|
- "github.com/olivere/elastic/v7"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
+
|
|
|
+ "github.com/olivere/elastic/v7"
|
|
|
)
|
|
|
|
|
|
// indexName:索引名称
|
|
@@ -2205,3 +2206,203 @@ func ClearEsIndex(indexName string) (err error) {
|
|
|
fmt.Printf("Cleared index: %s, Deleted documents count: %d\n", indexName, res.Deleted)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// SearchDataSourceIndexWithScroll 使用scroll API查询es中的数据源
|
|
|
+func SearchDataSourceIndexWithScroll(indexName, keyword string, source, subSource int, classifyIds, adminIds []int, frequency []string, size int) (scrollId string, list []*dataSourceModel.SearchDataSourceItem, err error) {
|
|
|
+ list = make([]*dataSourceModel.SearchDataSourceItem, 0)
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tips := fmt.Sprintf("SearchDataSourceIndexWithScroll err: %v", err)
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ client := utils.EsClient
|
|
|
+
|
|
|
+ mustMap := make([]interface{}, 0)
|
|
|
+ mustNotMap := make([]interface{}, 0)
|
|
|
+ mustMap = append(mustMap, map[string]interface{}{
|
|
|
+ "term": map[string]interface{}{
|
|
|
+ "IsDeleted": 0,
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ // 指标编码/名称
|
|
|
+ shouldMap := make(map[string]interface{}, 0)
|
|
|
+ if keyword != "" {
|
|
|
+ shouldMap["should"] = []interface{}{
|
|
|
+ map[string]interface{}{
|
|
|
+ "match": map[string]interface{}{
|
|
|
+ "IndexCode": keyword,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ map[string]interface{}{
|
|
|
+ "match": map[string]interface{}{
|
|
|
+ "IndexName": keyword,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 来源/子来源
|
|
|
+ if source > 0 {
|
|
|
+ mustMap = append(mustMap, map[string]interface{}{
|
|
|
+ "term": map[string]interface{}{
|
|
|
+ "Source": source,
|
|
|
+ },
|
|
|
+ })
|
|
|
+ }
|
|
|
+ if subSource > 0 {
|
|
|
+ mustMap = append(mustMap, map[string]interface{}{
|
|
|
+ "term": map[string]interface{}{
|
|
|
+ "SubSource": subSource,
|
|
|
+ },
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 分类
|
|
|
+ if len(classifyIds) > 0 {
|
|
|
+ mustMap = append(mustMap, map[string]interface{}{
|
|
|
+ "term": map[string]interface{}{
|
|
|
+ "ClassifyId": classifyIds,
|
|
|
+ },
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建人
|
|
|
+ if len(adminIds) > 0 {
|
|
|
+ mustMap = append(mustMap, map[string]interface{}{
|
|
|
+ "term": map[string]interface{}{
|
|
|
+ "SysUserId": adminIds,
|
|
|
+ },
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 频度
|
|
|
+ if len(frequency) > 0 {
|
|
|
+ mustMap = append(mustMap, map[string]interface{}{
|
|
|
+ "term": map[string]interface{}{
|
|
|
+ "Frequency": frequency,
|
|
|
+ },
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关键字匹配
|
|
|
+ mustMap = append(mustMap, map[string]interface{}{
|
|
|
+ "bool": shouldMap,
|
|
|
+ })
|
|
|
+ queryMap := map[string]interface{}{
|
|
|
+ "query": map[string]interface{}{
|
|
|
+ "bool": map[string]interface{}{
|
|
|
+ "must": mustMap,
|
|
|
+ "must_not": mustNotMap,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ // 表格名称高亮
|
|
|
+ highlightKeyName := "IndexName"
|
|
|
+ queryMap["highlight"] = map[string]interface{}{
|
|
|
+ "fields": map[string]interface{}{
|
|
|
+ highlightKeyName: map[string]interface{}{},
|
|
|
+ },
|
|
|
+ "pre_tags": "<span style=\"color:#0052D9\">",
|
|
|
+ "post_tags": "</span>",
|
|
|
+ }
|
|
|
+ queryMap["size"] = size
|
|
|
+
|
|
|
+ // 使用scroll API
|
|
|
+ request := client.Scroll(indexName).Body(queryMap).Scroll("5m")
|
|
|
+ searchResp, e := request.Do(context.Background())
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("search do err: %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if searchResp.Status != 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if searchResp.Hits == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ scrollId = searchResp.ScrollId
|
|
|
+ searchMap := make(map[string]string)
|
|
|
+ for _, v := range searchResp.Hits.Hits {
|
|
|
+ if _, ok := searchMap[v.Id]; ok {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ j, e := v.Source.MarshalJSON()
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("hits json err: %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ item := new(dataSourceModel.SearchDataSourceItem)
|
|
|
+ if e = json.Unmarshal(j, &item); e != nil {
|
|
|
+ err = fmt.Errorf("hits json unmarshal err: %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(v.Highlight[highlightKeyName]) > 0 {
|
|
|
+ item.SearchText = v.Highlight[highlightKeyName][0]
|
|
|
+ }
|
|
|
+ if item.SearchText == "" {
|
|
|
+ item.SearchText = item.IndexName
|
|
|
+ }
|
|
|
+ list = append(list, item)
|
|
|
+ searchMap[v.Id] = v.Id
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// ScrollDataSourceIndex 使用scroll API获取下一页数据
|
|
|
+func ScrollDataSourceIndex(indexName, scrollId string) (nextScrollId string, list []*dataSourceModel.SearchDataSourceItem, err error) {
|
|
|
+ list = make([]*dataSourceModel.SearchDataSourceItem, 0)
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tips := fmt.Sprintf("ScrollDataSourceIndex err: %v", err)
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ client := utils.EsClient
|
|
|
+
|
|
|
+ // 使用scroll API获取下一页
|
|
|
+ request := client.Scroll().ScrollId(scrollId).Scroll("5m")
|
|
|
+ searchResp, e := request.Do(context.Background())
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("scroll do err: %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if searchResp.Status != 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if searchResp.Hits == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ nextScrollId = searchResp.ScrollId
|
|
|
+ searchMap := make(map[string]string)
|
|
|
+ for _, v := range searchResp.Hits.Hits {
|
|
|
+ if _, ok := searchMap[v.Id]; ok {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ j, e := v.Source.MarshalJSON()
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("hits json err: %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ item := new(dataSourceModel.SearchDataSourceItem)
|
|
|
+ if e = json.Unmarshal(j, &item); e != nil {
|
|
|
+ err = fmt.Errorf("hits json unmarshal err: %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(v.Highlight["IndexName"]) > 0 {
|
|
|
+ item.SearchText = v.Highlight["IndexName"][0]
|
|
|
+ }
|
|
|
+ if item.SearchText == "" {
|
|
|
+ item.SearchText = item.IndexName
|
|
|
+ }
|
|
|
+ list = append(list, item)
|
|
|
+ searchMap[v.Id] = v.Id
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|