In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the methods of elasticsearch document operation". In the daily operation, I believe that many people have doubts about the methods of operating elasticsearch documents. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "what are the methods of operating elasticsearch documents?" Next, please follow the editor to study!
The document finds the data rst of name=hnatao, _: = client.Search (). Index ("user") .Query (elastic.NewMatchQuery ("name", "hnatao")) .Do (ctx) buf, _: = json.Marshal (rst.Hits.Hits) fmt.Println (string (buf))
Return
[{"_ score": 1.3862942, "_ index": "user", "_ type": "_ doc", "_ id": "1", "_ seq_no": null, "_ primary_term": null, "_ source": {"name": "hnatao", "age": 21 "score": 80}}] find data for 20-year-old hnatao 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))
Return
[] find all user information 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))
Return
[{"_ 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}}] find all user information over 21 years old 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))
Return
[{"_ 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}}] find users with score records Q: = elastic.NewExistsQuery ("score") rst, _: = client.Search (). Index ("user") .query (Q) .Do (ctx) buf, _: = json.Marshal (rst.Hits.Hits) fmt.Println (string (buf))
Return
[{"_ 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}}] find users 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))
Return
[] Total number of users aged 20: Q: = elastic.NewTermQuery ("age", "20") rst, _: = client.Count (). Index ("user") .query (Q) .Do (ctx) buf, _: = json.Marshal (rst) fmt.Println (string (buf))
Return
Number: 1 average number of users 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"]))
Return
{"value": 22.0} find the youngest user rst, _: = client.Search (). Index ("user"). Sort ("age", true) .size (1) .Do (ctx) buf, _: = json.Marshal (rst.Hits.Hits) fmt.Println (string (buf))
Return
[{"_ index": "user", "_ type": "_ doc", "_ id": "5", "_ seq_no": null, "_ primary_term": null, "sort": [20], "_ source": {"name": "guofucheng", "age": 20 "score": 0}}] Statistics age of each dimension 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))
Return
{"count": 5, "min": 20.0, "max": 24.0, "avg": 22.0, "sum": 110.0} Statistical age percentage 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))
Return
{"values": {"1.0,5.0": 20.0,25.0,50.0: 22.0,75.0,23.25,95.0: 24.0,99.0: 24.0} query the average score of each age And sort by age 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))
Return
{"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} query the average score for each age The average score is sorted by 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))
Return
{"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}}]} so far On the "what are the methods of elasticsearch document operation" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.