2019-02-19 15:24
浏览 1.0k


I work with golang elastic 5 API to run queries in ElasticSearch. I check the number of hits with searchResult.TotalHits() and it gives me a large number (more than 100), but when I try to iterate over the hits it gives only 10 entities. Also when I check the len(searchResult.Hits.Hits) variable I get 10.

I tried different queries when I select less than 10 entity and it works well.

query = elastic.NewBoolQuery()
ctx := context.Background()

query = query.Must(elastic.NewTermQuery("key0", "term"),
    elastic.NewWildcardQuery("key1", "*term2*"),
    elastic.NewWildcardQuery("key3", "*.*"),
searchResult, err = client.Search().Index("index").
fmt.Printf("TotalHits(): %v", searchResult.TotalHits()) //It gives me 482
fmt.Printf("length of the hits array: %v", len(searchResult.Hits.Hits)) //It gives 10
for _, hit := range searchResult.Hits.Hits {
    var tweet Tweet
    _ = json.Unmarshal(*hit.Source, &tweet)
            fmt.Printf("entity: %s", tweet) //It prints 10 entity

What am I doing wrong? Are there batches in the SearchResult or what could be the solution?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyakan8924 2019-02-19 15:33

    It's not specified in your question so please comment if you're using a different client library (eg the official client), but it appears you're using github.com/olivere/elastic. Based on that assumption, what you're seeing is the default result set size of 10. The TotalHits number is how many documents match your query in total; the Hits number is how many were returned in your current result, which you can manipulate using Size, Sort and From. Size is documented as:

    Size is the number of search hits to return. Defaults to 10.

    解决 无用
    打赏 举报

相关推荐 更多相似问题