Пример #1
0
func es_push(es *elastic.Client, idx, typ string, m media) {
	if es == nil {
		return
	}

	r, err := es.Index().Index(idx).Type(typ).Id(strconv.Itoa(m.MediaId)).BodyJson(m).Do()
	log.Println(r, err)
}
Пример #2
0
// SaveDataIntoES save metrics and events to ES by using ES client
func SaveDataIntoES(esClient *elastic.Client, indexName string, typeName string, sinkData interface{}) error {
	if indexName == "" || typeName == "" || sinkData == nil {
		return nil
	}
	// Use the IndexExists service to check if a specified index exists.
	exists, err := esClient.IndexExists(indexName).Do()
	if err != nil {
		return err
	}
	if !exists {
		// Create a new index.
		createIndex, err := esClient.CreateIndex(indexName).Do()
		if err != nil {
			return err
		}
		if !createIndex.Acknowledged {
			return fmt.Errorf("failed to create Index in ES cluster: %s", err)
		}
	}
	indexID := uuid.NewUUID()
	_, err = esClient.Index().
		Index(indexName).
		Type(typeName).
		Id(string(indexID)).
		BodyJson(sinkData).
		Do()
	if err != nil {
		return err
	}
	return nil
}
Пример #3
0
func create_index(client *elastic.Client, index string) error {
	_, err := client.CreateIndex(index).Do()
	return err
}
Пример #4
0
func drop_index(client *elastic.Client, index string) error {
	_, err := client.DeleteIndex(index).Do()
	return err
}
Пример #5
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
}