rdluck 4 lat temu
rodzic
commit
1015b1f558
2 zmienionych plików z 20 dodań i 14 usunięć
  1. 3 1
      controllers/search.go
  2. 17 13
      services/elastic.go

+ 3 - 1
controllers/search.go

@@ -65,7 +65,9 @@ func (this *SearchController) SearchList() {
 		categoryName := strings.Join(categoryNameArr, ",")
 	*/
 	indexName := "article_list"
-	result, total, err := services.EsMatchFunctionScoreQuery(indexName, keyWord, startSize, pageSize) //services.EsMatchFunctionScoreQuery(indexName, keyWord, startSize, pageSize) //services.EsMatchQuery(indexName, keyWord)
+	//EsMultiMatchFunctionScoreQuery
+	//EsMatchFunctionScoreQuery
+	result, total, err := services.EsMultiMatchFunctionScoreQuery(indexName, keyWord, startSize, pageSize) //services.EsMatchFunctionScoreQuery(indexName, keyWord, startSize, pageSize) //services.EsMatchQuery(indexName, keyWord)
 	//result, err := services.EsMatchQuery(indexName, keyWord)//services.EsMatchQuery(indexName, keyWord)
 	//indexName := "article_list"
 	//result, err := services.EsMatchPhraseQuery(indexName, keyWord)

+ 17 - 13
services/elastic.go

@@ -289,11 +289,14 @@ func EsMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize in
 	//matchArr = append(matchArr, matchq2)
 	n := 0
 	keyWordLen := len(keyWordArr)
-	if keyWordLen<=0{
-		keyWordArr=append(keyWordArr,keyWord)
+	if keyWordLen <= 0 {
+		keyWordArr = append(keyWordArr, keyWord)
 		keyWordLen = len(keyWordArr)
 	}
 	keyWordWeight := GetWeight(keyWordLen)
+
+	fmt.Println(keyWordArr)
+	fmt.Println(keyWordWeight)
 	for k, v := range keyWordArr {
 		if v != "" {
 			weight := float64(keyWordWeight[k])
@@ -324,8 +327,8 @@ func EsMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize in
 	highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
 	request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
 	searchByMatch, err := request.Do(context.Background())
-	searchJson,err:=json.Marshal(searchByMatch)
-	utils.FileLog.Info("%s",string(searchJson))
+	searchJson, err := json.Marshal(searchByMatch)
+	utils.FileLog.Info("%s", string(searchJson))
 	if searchByMatch != nil {
 		if searchByMatch.Hits != nil {
 			for _, v := range searchByMatch.Hits.Hits {
@@ -367,16 +370,14 @@ func EsMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize in
 			}
 		}
 	}
-	total=searchByMatch.Hits.TotalHits.Value
+	total = searchByMatch.Hits.TotalHits.Value
 	return
 }
 
-
-
 func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
 	client, err := NewClient()
 	keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
-	
+
 	keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
 	searchMap := make(map[int]int)
 	boolquery := elastic.NewBoolQuery()
@@ -384,11 +385,15 @@ func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSi
 
 	n := 0
 	keyWordLen := len(keyWordArr)
+	if keyWordLen <= 0 {
+		keyWordArr = append(keyWordArr, keyWord)
+		keyWordLen = len(keyWordArr)
+	}
 	keyWordWeight := GetWeight(keyWordLen)
 	for k, v := range keyWordArr {
 		if v != "" {
 			weight := float64(keyWordWeight[k])
-			multiMatch:=elastic.NewMultiMatchQuery(v,"Title","BodyText").Analyzer("ik_smart")
+			multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
 			bodyFunctionQuery := elastic.NewFunctionScoreQuery()
 			bodyFunctionQuery.Query(multiMatch)
 			bodyFunctions := elastic.NewWeightFactorFunction(weight)
@@ -445,15 +450,14 @@ func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSi
 			}
 		}
 	}
-	total=searchByMatch.Hits.TotalHits.Value
+	total = searchByMatch.Hits.TotalHits.Value
 	return
 }
 
-
 func GetWeight(length int) []int {
 	steep := 10
 	intArr := make([]int, 0)
-	for i := 1; i <= 10; i++ {
+	for i := 1; i <= length; i++ {
 		if i == 1 {
 			intArr = append(intArr, 1)
 		} else {
@@ -475,4 +479,4 @@ func GetArrSum(intArr []int) (sum int) {
 		sum += val
 	}
 	return
-}
+}