func (history *ElasticHistory) GetUserMessageCount(from, to string) ([]byte, error) { termAggregation := elastic.NewTermsAggregation().Field("from") rangeQuery := elastic.NewRangeQuery("date") if from != "" && to != "" { rangeQuery = rangeQuery.From(from).To(to) } searchResult, err := history.client.Search(). Index(history.index). Type(TYPE). Query(rangeQuery). Size(0). Aggregation("top", termAggregation). Do() if err != nil { return nil, err } resultAggr, _ := searchResult.Aggregations.Terms("top") outputResult := make([]termResult, len(resultAggr.Buckets), len(resultAggr.Buckets)) for i, r := range resultAggr.Buckets { outputResult[i] = termResult{Key: r.Key.(string), DocCount: r.DocCount} } return json.Marshal(outputResult) }
func (history *ElasticHistory) GetMessagesBetween(from, to string) ([]byte, error) { rangeQuery := elastic.NewRangeQuery("date").From(from).To(to) result, err := history.client.Search(). Index(history.index). Type(TYPE). Query(&rangeQuery). Sort("date", false). From(0).Size(10000). //this is cheating :( Do() if err != nil { return nil, err } if result.Error != "" { return nil, errors.New(result.Error) } resultCnt := len(result.Hits.Hits) log.Printf("Fetched %d entries\n", resultCnt) resultSlice := make([]Message, resultCnt, resultCnt) var tmpMessage Message for i, m := range result.Each(reflect.TypeOf(tmpMessage)) { resultSlice[i] = m.(Message) } reverseHistory(resultSlice) return json.Marshal(resultSlice) }
func (history *ElasticHistory) GetDailyHistogram(from, to string) ([]byte, error) { dailyAggregation := elastic.NewDateHistogramAggregation(). Field("date"). Interval("day"). Format("dd.MM.YYYY") rangeQuery := elastic.NewRangeQuery("date").From(from).To(to) searchResult, err := history.client.Search(). Index(history.index). Type(TYPE). Query(rangeQuery). Size(0). Aggregation("histogram", dailyAggregation). Do() if err != nil { return nil, err } hist, _ := searchResult.Aggregations.Histogram("histogram") log.Printf("In total there are %d buckets", len(hist.Buckets)) aggRes := make([]histogramResult, 30, 30) tDate := time.Now() tY, tM, tD := tDate.Date() d := time.Date(tY, tM, tD, 0, 0, 0, 0, time.UTC) for i := 0; i < 30; i++ { //aggregation results are for last 30days and for frontend they are prefilled aggRes[30-i-1] = histogramResult{Key: d.Unix(), DocCount: 0, Date: d.Format("02.01.2006")} d = d.Add(-24 * time.Hour) } for _, agg := range hist.Buckets { for i := 0; i < 30; i++ { if aggRes[i].Date == *agg.KeyAsString { aggRes[i].DocCount = agg.DocCount break } } } return json.Marshal(aggRes) }
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 }