您好,登錄后才能下訂單哦!
這篇文章主要介紹“elasticsearch文檔操作的方法有哪些”,在日常操作中,相信很多人在elasticsearch文檔操作的方法有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”elasticsearch文檔操作的方法有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
name=hnatao
的數(shù)據(jù)rst, _ := client.Search().Index("user").Query(elastic.NewMatchQuery("name", "hnatao")).Do(ctx) buf, _ := json.Marshal(rst.Hits.Hits) fmt.Println(string(buf))
返回
[ { "_score": 1.3862942, "_index": "user", "_type": "_doc", "_id": "1", "_seq_no": null, "_primary_term": null, "_source": { "name": "hnatao", "age": 21, "score": 80 } } ]
q := elastic.NewBoolQuery().Must( elastic.NewMatchQuery("name", "hnatao"), elastic.NewMatchQuery("age", "20"), ) rst, _ := client.Search().Index("user").Query(q).Do(ctx) buf, _ := json.Marshal(rst.Hits.Hits) fmt.Println(string(buf))
返回
[]
q := elastic.NewRangeQuery("age").Gte("20").Lte("21") rst, _ := client.Search().Index("user").Query(q).Do(ctx) buf, _ := json.Marshal(rst.Hits.Hits) fmt.Println(string(buf))
返回
[ { "_score": 1, "_index": "user", "_type": "_doc", "_id": "1", "_seq_no": null, "_primary_term": null, "_source": { "name": "hnatao", "age": 21, "score": 80 } }, { "_score": 1, "_index": "user", "_type": "_doc", "_id": "5", "_seq_no": null, "_primary_term": null, "_source": { "name": "guofucheng", "age": 20, "score": 0 } } ]
q := elastic.NewRangeQuery("age").Gte("21") rst, _ := client.Search().Index("user").Query(q).Do(ctx) buf, _ := json.Marshal(rst.Hits.Hits) fmt.Println(string(buf))
返回
[ { "_score": 1, "_index": "user", "_type": "_doc", "_id": "1", "_seq_no": null, "_primary_term": null, "_source": { "name": "hnatao", "age": 21, "score": 80 } }, { "_score": 1, "_index": "user", "_type": "_doc", "_id": "2", "_seq_no": null, "_primary_term": null, "_source": { "name": "lqt", "age": 22, "score": 90 } }, { "_score": 1, "_index": "user", "_type": "_doc", "_id": "3", "_seq_no": null, "_primary_term": null, "_source": { "name": "liudehua", "age": 23, "score": 85 } }, { "_score": 1, "_index": "user", "_type": "_doc", "_id": "4", "_seq_no": null, "_primary_term": null, "_source": { "name": "zhangxueyou", "age": 24, "score": 86 } } ]
q := elastic.NewExistsQuery("score") rst, _ := client.Search().Index("user").Query(q).Do(ctx) buf, _ := json.Marshal(rst.Hits.Hits) fmt.Println(string(buf))
返回
[ { "_score": 1, "_index": "user", "_type": "_doc", "_id": "1", "_seq_no": null, "_primary_term": null, "_source": { "name": "hnatao", "age": 21, "score": 80 } }, { "_score": 1, "_index": "user", "_type": "_doc", "_id": "2", "_seq_no": null, "_primary_term": null, "_source": { "name": "lqt", "age": 22, "score": 90 } }, { "_score": 1, "_index": "user", "_type": "_doc", "_id": "3", "_seq_no": null, "_primary_term": null, "_source": { "name": "liudehua", "age": 23, "score": 85 } }, { "_score": 1, "_index": "user", "_type": "_doc", "_id": "4", "_seq_no": null, "_primary_term": null, "_source": { "name": "zhangxueyou", "age": 24, "score": 86 } }, { "_score": 1, "_index": "user", "_type": "_doc", "_id": "5", "_seq_no": null, "_primary_term": null, "_source": { "name": "guofucheng", "age": 20, "score": 0 } } ]
q := elastic.NewBoolQuery().MustNot(elastic.NewExistsQuery("score")) rst, _ := client.Search().Index("user").Query(q).Do(ctx) buf, _ := json.Marshal(rst.Hits.Hits) fmt.Println(string(buf))
返回
[]
q := elastic.NewTermQuery("age", "20") rst, _ := client.Count().Index("user").Query(q).Do(ctx) buf, _ := json.Marshal(rst) fmt.Println(string(buf))
返回
數(shù)字:1
q := elastic.NewAvgAggregation().Field("age") rst, _ := client.Search().Index("user").Aggregation("avg_age", q).Size(0).Do(ctx) fmt.Println(string(rst.Aggregations["avg_age"]))
返回
{ "value": 22.0 }
rst, _ := client.Search().Index("user").Sort("age", true).Size(1).Do(ctx) buf, _ := json.Marshal(rst.Hits.Hits) fmt.Println(string(buf))
返回
[ { "_index": "user", "_type": "_doc", "_id": "5", "_seq_no": null, "_primary_term": null, "sort": [20], "_source": { "name": "guofucheng", "age": 20, "score": 0 } } ]
agg := elastic.NewStatsAggregation().Field("age") rst, _ := client.Search().Index("user").Aggregation("stats_age", agg).Do(ctx) buf, _ := rst.Aggregations["stats_age"].MarshalJSON() fmt.Println(string(buf))
返回
{ "count": 5, "min": 20.0, "max": 24.0, "avg": 22.0, "sum": 110.0 }
agg := elastic.NewPercentilesAggregation().Field("age") rst, _ := client.Search().Index("user").Aggregation("stats_age", agg).Do(ctx) buf, _ := rst.Aggregations["stats_age"].MarshalJSON() fmt.Println(string(buf))
返回
{ "values": { "1.0": 20.0, "5.0": 20.0, "25.0": 20.75, "50.0": 22.0, "75.0": 23.25, "95.0": 24.0, "99.0": 24.0 } }
agg := elastic.NewTermsAggregation().Field("age"). SubAggregation("avg_score", elastic.NewAvgAggregation().Field("score")).OrderByKeyAsc() rst, _ := client.Search().Index("user").Aggregation("stats_age", agg).Do(ctx) buf, _ := rst.Aggregations["stats_age"].MarshalJSON() fmt.Println(string(buf))
返回
{ "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 20, "doc_count": 1, "avg_score": { "value": 0.0 } }, { "key": 21, "doc_count": 2, "avg_score": { "value": 85.0 } }, { "key": 22, "doc_count": 2, "avg_score": { "value": 85.5 } } ] }
agg := elastic.NewTermsAggregation().Field("age"). SubAggregation("avg_score", elastic.NewAvgAggregation().Field("score")).OrderByAggregation("avg_score", false) rst, _ := client.Search().Index("user").Aggregation("stats_age", agg).Do(ctx) buf, _ := rst.Aggregations["stats_age"].MarshalJSON() fmt.Println(string(buf))
返回
{ "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 22, "doc_count": 2, "avg_score": { "value": 85.5 } }, { "key": 21, "doc_count": 2, "avg_score": { "value": 85.0 } }, { "key": 20, "doc_count": 1, "avg_score": { "value": 0.0 } } ] }
到此,關(guān)于“elasticsearch文檔操作的方法有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。