コード例 #1
0
func elResult(client elastic.Client, cond Conditions) (*elastic.SearchResult, error) {
	//---------20150730下午修必ALl查询--------------
	if len(cond.All) > 0 {

		q := elastic.NewQueryStringQuery(cond.All).AnalyzeWildcard(true).DefaultOperator("or").Boost(0.4)
		q = q.DefaultField("name")

		q2 := elastic.NewQueryStringQuery(cond.All).AnalyzeWildcard(true).DefaultOperator("or").Boost(0.2)
		q2 = q2.DefaultField("artistName")

		q3 := elastic.NewQueryStringQuery(cond.All).AnalyzeWildcard(true).DefaultOperator("or").Boost(0.1)
		q3 = q2.DefaultField("special")

		qbool := elastic.NewBoolQuery()
		qbool = qbool.Must(q)
		builder := elastic.SortInfo{Field: "hotNum", Ascending: false} //指定按hotNum排序
		serarchResult, err := client.Search().Index(indexing).SearchType("dfs_query_then_fetch").SortWithInfo(builder).Query(qbool).From(0).Size(size).Explain(true).Timeout("1s").Do()
		if len(serarchResult.Hits.Hits) > 0 {
			fmt.Println("Name")
			return serarchResult, err
		} else {
			qbool2 := elastic.NewBoolQuery()
			qbool2 = qbool2.Must(q2)
			serarchResult, err = client.Search().Index(indexing).SearchType("dfs_query_then_fetch").SortWithInfo(builder).Query(qbool2).From(0).Size(size).Explain(true).Timeout("1s").Do()
			if len(serarchResult.Hits.Hits) > 0 {
				fmt.Println("artistName")
				return serarchResult, err
			} else {
				qbool3 := elastic.NewBoolQuery()
				qbool3 = qbool3.Must(q3)
				serarchResult, err = client.Search().Index(indexing).SearchType("dfs_query_then_fetch").SortWithInfo(builder).Query(qbool3).From(0).Size(size).Explain(true).Timeout("1s").Do()
				if len(serarchResult.Hits.Hits) > 0 {
					fmt.Println("special")
					return serarchResult, err
				} else {
					return nil, errors.New("没能找到你需要的歌曲")
				}
			}
		}

	} else {
		if len(cond.ArtisName) == 0 && len(cond.Name) == 0 && len(cond.Special) == 0 { //默认列表
			r := rand.New(rand.NewSource(time.Now().UnixNano()))
			mathdata := (r.Intn(100) + 1) * random
			q := elastic.NewRangeQuery("hotNum").From(strconv.Itoa(mathdata)).To(hotum)
			data, errdata := json.Marshal(q.Source())
			if errdata != nil {
				fmt.Println(string(data))
			} else {
				fmt.Println(string(data))
			}
			//	builder := SortInfo{Field: "hotNum", Ascending: false} //指定按hotNum排序
			return client.Search().SearchType("dfs_query_then_fetch").
				Query(q).From(0).Size(size).Explain(true).Timeout("3s").Do()

		} else {
			qbool := elastic.NewBoolQuery()
			if len(cond.ArtisName) > 0 {
				q_artisName := elastic.NewQueryStringQuery(cond.ArtisName).AnalyzeWildcard(false).DefaultOperator("and")
				q_artisName = q_artisName.DefaultField("artistName")
				qbool = qbool.Must(q_artisName)
			}
			if len(cond.Name) > 0 {
				q_name := elastic.NewQueryStringQuery(cond.Name).AnalyzeWildcard(false).DefaultOperator("and")
				q_name = q_name.DefaultField("name")
				qbool = qbool.Must(q_name)
			}
			if len(cond.Special) > 0 {
				q_special := elastic.NewQueryStringQuery(cond.Special).AnalyzeWildcard(false).DefaultOperator("and")
				q_special = q_special.DefaultField("special")
				qbool = qbool.Must(q_special)
			}
			data, errdata := json.Marshal(qbool.Source())
			if errdata == nil {
				fmt.Println(string(data))
			}
			//	builder := SortInfo{Field: "hotNum", Ascending: false} //指定按hotNum排序
			return client.Search().Index(indexing).
				Query(qbool).Explain(true).From(0).Size(size).Timeout("3s").Do()
		}

	}
	return nil, nil
}