# 关于 add\_more\_topics\_if\_expected 及其内部的一个 bug

**URL:** <https://meta.discourse.org/t/about-add-more-topics-if-expected-and-a-bug-inside-it/86808>\
**Category:** Development\
**Created:** [2018年五月5日 03:05 UTC](https://meta.discourse.org/t/about-add-more-topics-if-expected-and-a-bug-inside-it/86808 "2018-05-05T03:05:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ballistic](https://avatars.discourse-cdn.com/v4/letter/b/dfb087/32.png) [@ballistic](https://meta.discourse.org/u/ballistic)\
**Post date:** [2018年五月5日 03:05 UTC](https://meta.discourse.org/t/about-add-more-topics-if-expected-and-a-bug-inside-it/86808/1 "2018-05-05T03:05:56Z")

</div>

I was wondering the logic behind “add\_more\_topics\_if\_expected”, why/when do we need the extra posts.

```
def find_grouped_results

  if @results.type_filter.present?
    ...
    send("#{@results.type_filter}_search")
  else
    ...
    topic_search
  end

  add_more_topics_if_expected
  @results
...
end

# Add more topics if we expected them
def add_more_topics_if_expected
  expected_topics = 0
  expected_topics = Search.facets.size unless @results.type_filter.present?
  expected_topics = Search.per_facet * Search.facets.size if @results.type_filter == 'topic' 
  expected_topics -= @results.posts.length
  if expected_topics > 0
    extra_posts = posts_query(expected_topics * Search.burst_factor)
    extra_posts = extra_posts.where("posts.topic_id NOT in (?)", @results.posts.map(&:topic_id)) if @results.posts.present?
    extra_posts.each do |post|
      @results.add(post)
      expected_topics -= 1
      break if expected_topics == 0
    end
  end
end

```

There is one **bug** we need to fix - the last page of topic\_search. In this case expected\_topics \> 0, and we added the previous pages’ results back.

Proposed fix:

```
expected_topics = Search.per_facet * Search.facets.size if @results.type_filter == 'topic' && offset <= 0

```

Before sending a PR, I want to understand the original logic.
