What are the methods of manipulating elasticsearch documents
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!