Go es中使用脚本 *** 作

Go es中使用脚本 *** 作,第1张

摘要:官方库:使用go向es进行数据 *** 作脚本 - 紫色飞猪 - 博客园

摘要:排序查询:golang-ElasticSearch学习 - 简书

摘要:go-elasticSearch实战篇(二) - 知乎

摘要:完美起航-ES7(三):用GO搜索elasticsearch数据

go中使用es的script语法,并通过返回自定义_score和其他字段排序

func (s *Search) esSearch(excludeIds []string, Keys string, lastScores int64, lastUpdateTime int64, limit int) (arr []*RecommendEs, err error) {
	/*aa := `{
			"query": {
				"function_score": {
					"query": {
						"bool": {
							"should": [
								{
									"match": {
										"name": "adfasdf3"
									}
								},
								{
									"match": {
										"desc": "adfasdf3"
									}
								}
							]
						}
					},
					"max_boost": 1000000000000,
					"min_score": 0,
					"boost_mode": "replace",
					"functions": [
						{
							"script_score": {
								"script": {
									"inline": "def v=0; if(_score>50){v=25+doc['source'].value+doc['source'].value}else if(_score > 40){v=15+doc['source'].value+doc['source'].value}else if(_score>30){v=10+doc['source'].value+doc['source'].value}else{v=5} def last_utime=10; def last_score=20; if(v < last_score && doc['update_time'].value.toInstant().toEpochMilli() > last_utime){return v}"
								}
							}
						}
					]
				}
			},
			"size": 5
		}`*/

	//fmt.Println("lastScores: ", lastScores)
	//fmt.Printf("lastUpdateTime: %f", float64(lastUpdateTime))
	//fmt.Println("limit: ", limit)
	inline := "def v=0; if(_score>50){v=25+doc['like_number'].value+doc['share_comment_number'].value}else if(_score > 40){v=15+doc['like_number'].value+doc['share_comment_number'].value}else if(_score>30){v=10+doc['like_number'].value+doc['share_comment_number'].value}else{v=5+doc['like_number'].value+doc['share_comment_number'].value} def last_utime=%f; def last_score=%d; if(v <= last_score && doc['update_time'].value.getMillis() > last_utime){return v}"
	//inline := "_score"
	inline2 := fmt.Sprintf(inline, float64(lastUpdateTime), lastScores)
	//fmt.Println("inline2: ", inline2)
	script := elastic.NewScript(inline2)
	fun01 := elastic.NewScriptFunction(script)

	func_score := elastic.NewFunctionScoreQuery()
	func_score.BoostMode("replace")
	func_score.MinScore(0)
	func_score.MaxBoost(10000000000000000000)

	condition := elastic.NewBoolQuery().Must(elastic.NewTermQuery("source", topicmodel.TopicTypeCircle))
	//Keys := []interface{}{"阿斯蒂芬", "圈子"}
	if len(Keys) > 0 {
		condition.Must(
			elastic.NewBoolQuery().Should(
				elastic.NewMatchQuery("name", Keys),
				elastic.NewMatchQuery("desc", Keys),
			),
		)
	}
	//excludeIds = []string{}
	var excludeQuery []elastic.Query
	if len(excludeIds) > 0 {
		for i := range excludeIds {
			excludeQuery = append(excludeQuery, elastic.NewTermQuery("id", excludeIds[i]))
		}
		condition.MustNot(excludeQuery...)
	}

	func_score.Query(condition)
	func_score.AddScoreFunc(fun01)

	sortScore := elastic.NewScoreSort().Desc()
	sortUpdateTime := elastic.NewFieldSort("update_time").Asc()
	sorters := make([]elastic.Sorter, 0)
	sorters = append(sorters, sortScore)
	sorters = append(sorters, sortUpdateTime)
	index := s.es.Search(IndexTopic).SortBy(sorters...).Query(func_score).Size(limit)
	result, err := index.Do(context.Background())

	if err != nil {
		s.logger.Error(err.Error())
		//fmt.Println("err:>>>>>>>>>>>>>>>", err.Error())
		return
	}

	return assembleResult(result), nil
}

type RecommendEs struct {
	ccEs
	Score uint64 `json:"score"`
}

func assembleResult(result *elastic.SearchResult) []*RecommendCircleEs  {
	arr := make([]*RecommendEs, 0, len(result.Hits.Hits))

	fmt.Println("hits.TotalHits: ", result.Hits.TotalHits)
	fmt.Println("result.Hits.Hits: ", result.Hits.Hits)
	for _, hits := range result.Hits.Hits {
		if hits.Source == nil || *hits.Score == 0 {
			continue
		}

		c := &RecommendEs{}
		err := json.Unmarshal(hits.Source, c)
		if err != nil {
			continue
		}
		circle.Score = uint64(*hits.Score)

		//fmt.Printf("circle%+v:>>>>>>>>>>>>>>>\n", circle)
		//fmt.Printf("hits.score: %f, AA:%T\n", *hits.Score, *hits.Score)
		//fmt.Println("hits.index: ", hits.Index)
		//fmt.Println("hits.type: ", hits.Type)
		//fmt.Println("hits.id: ", hits.Id)

		arr = append(arr, c)
	}

	return arr
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/994812.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存