{"id":207639,"date":"2025-07-08T10:36:24","date_gmt":"2025-07-08T02:36:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207639\/"},"modified":"2025-07-08T10:36:24","modified_gmt":"2025-07-08T02:36:24","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-searchresult-totalhits-%e4%b8%8e-lensearchresult-hits-hits-%e4%b8%8d%e5%90%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207639\/","title":{"rendered":"\u4e3a\u4ec0\u4e48 searchResult.TotalHits() \u4e0e len(searchResult.Hits.Hits) \u4e0d\u540c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4e3a\u4ec0\u4e48 searchResult.TotalHits() \u4e0e len(searchResult.Hits.Hits) \u4e0d\u540c\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 10:36:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3a\u4ec0\u4e48 searchResult.TotalHits() \u4e0e len(searchResult.Hits.Hits) \u4e0d\u540c\uff1f\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4f7f\u7528 golang elastic 5 api \u5728 elasticsearch \u4e2d\u8fd0\u884c\u67e5\u8be2\u3002\u6211\u4f7f\u7528 searchresult.totalhits() \u68c0\u67e5\u547d\u4e2d\u6570\uff0c\u5b83\u7ed9\u4e86\u6211\u4e00\u4e2a\u5f88\u5927\u7684\u6570\u5b57\uff08\u8d85\u8fc7 100\uff09\uff0c\u4f46\u662f\u5f53\u6211\u5c1d\u8bd5\u8fed\u4ee3\u547d\u4e2d\u65f6\uff0c\u5b83\u53ea\u7ed9\u51fa 10 \u4e2a\u5b9e\u4f53\u3002\u53e6\u5916\uff0c\u5f53\u6211\u68c0\u67e5 len(searchresult.hits.hits) \u53d8\u91cf\u65f6\uff0c\u6211\u5f97\u5230 10\u3002<\/p>\n<p>\u5f53\u6211\u9009\u62e9\u5c11\u4e8e 10 \u4e2a\u5b9e\u4f53\u65f6\uff0c\u6211\u5c1d\u8bd5\u4e86\u4e0d\u540c\u7684\u67e5\u8be2\uff0c\u6548\u679c\u5f88\u597d\u3002 <\/p>\n<pre>query = elastic.NewBoolQuery()\nctx := context.Background()\n\nquery = query.Must(elastic.NewTermQuery(\"key0\", \"term\"),\n    elastic.NewWildcardQuery(\"key1\", \"*term2*\"),\n    elastic.NewWildcardQuery(\"key3\", \"*.*\"),\n    elastic.NewRangeQuery(\"timestamp\").From(fromTime).To(toTime),\n)\nsearchResult, err = client.Search().Index(\"index\").\n    Query(query).Pretty(true).Do(ctx)\nfmt.Printf(\"TotalHits(): %v\", searchResult.TotalHits()) \/\/It gives me 482\nfmt.Printf(\"length of the hits array: %v\", len(searchResult.Hits.Hits)) \/\/It gives 10\nfor _, hit := range searchResult.Hits.Hits {\n    var tweet Tweet\n    _ = json.Unmarshal(*hit.Source, &amp;tweet)\n            fmt.Printf(\"entity: %s\", tweet) \/\/It prints 10 entity\n}<\/pre>\n<p>\u6211\u505a\u9519\u4e86\u4ec0\u4e48\uff1f searchresult \u4e2d\u662f\u5426\u6709\u6279\u6b21\u6216\u8005\u89e3\u51b3\u65b9\u6848\u662f\u4ec0\u4e48\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u7684\u95ee\u9898\u4e2d\u672a\u6307\u5b9a\uff0c\u56e0\u6b64\u5982\u679c\u60a8\u4f7f\u7528\u4e0d\u540c\u7684\u5ba2\u6237\u7aef\u5e93\uff08\u4f8b\u5982\u5b98\u65b9\u5ba2\u6237\u7aef\uff09\uff0c\u8bf7\u53d1\u8868\u8bc4\u8bba\uff0c\u4f46\u60a8\u4f3c\u4e4e\u6b63\u5728\u4f7f\u7528 github.com\/olivere\/elastic\u3002\u57fa\u4e8e\u8be5\u5047\u8bbe\uff0c\u60a8\u770b\u5230\u7684\u662f\u9ed8\u8ba4\u7ed3\u679c\u96c6\u5927\u5c0f 10\u3002 <code>TotalHits<\/code> \u6570\u5b57\u662f\u4e0e\u60a8\u7684\u67e5\u8be2\u5339\u914d\u7684\u6587\u6863\u603b\u6570\uff1b <code>Hits<\/code> \u6570\u5b57\u662f\u5f53\u524d\u7ed3\u679c\u4e2d\u8fd4\u56de\u7684\u6570\u91cf\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 \u3001<code>Sort<\/code> \u548c <code>From<\/code> \u8fdb\u884c\u64cd\u4f5c\u3002 <code>Size<\/code> \u8bb0\u5f55\u4e3a\uff1a<\/p>\n<p>Size \u662f\u8981\u8fd4\u56de\u7684\u641c\u7d22\u547d\u4e2d\u6570\u3002\u9ed8\u8ba4\u4e3a 10\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4e3a\u4ec0\u4e48 searchResult.TotalHits() \u4e0e len(searchResult.Hits.Hits) \u4e0d\u540c\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207639","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207639"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207639\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}