谢谢 @sam!我可以看到(即使仅从 GET 请求来看)这应该相当直观:当我想要获取第 2 页时,只需为 page 添加一个额外的选项。我还可以看到,discourse_api 函数允许我自定义“options”:
# frozen_string_literal: true
module DiscourseApi
module API
module Search
# 返回与指定术语匹配的搜索结果。
#
# @param term [String] 搜索术语
# @param options [Hash] 可自定义的选项集合
# @option options [String] :type_filter 返回指定类型的结果。
# @return [Array] 以哈希数组的形式返回结果。
def search(term, options = {})
raise ArgumentError.new("#{term} 是必需的但未指定") unless term
raise ArgumentError.new("#{term} 是必需的但未指定") unless !term.empty?
response = get('/search/query', options.merge(term: term))
response[:body]
end
end
end
end
因此,尝试一下,我期望第 1 页和第 2 页会得到不同的结果。或者为了更明显的区分,我们比较第 1 页和第 3 页。查询针对所有 Q&A 主题:
query = category["name"] + " #" + category["slug"]
=> "Q&A #q-a"
现在使用 discourse_api 客户端获取第 1 页和第 3 页:
topics1 = client.search(query, options={"page": "1"})
topics3 = client.search(query, options={"page": "3"})
我可以查看每个结果中的第一个主题:
=> {"id"=>220, "title"=>"Why am I exceeding the quota?", "fancy_title"=>"Why am I exceeding the quota?", "slug"=>"why-am-i-exceeding-the-quota", "posts_count"=>3, "reply_count"=>0, "highest_post_number"=>3, "image_url"=>nil, "created_at"=>"2018-06-01T12:56:12.120Z", "last_posted_at"=>"2018-06-15T16:41:44.736Z", "bumped"=>true, "bumped_at"=>"2018-06-15T16:41:44.736Z", "unseen"=>false, "pinned"=>false, "unpinned"=>nil, "visible"=>true, "closed"=>false, "archived"=>false, "bookmarked"=>nil, "liked"=>nil, "tags"=>["storage", "quota"], "category_id"=>26, "has_accepted_answer"=>false}
irb(main):148:0> topics3['topics'][0]
=> {"id"=>220, "title"=>"Why am I exceeding the quota?", "fancy_title"=>"Why am I exceeding the quota?", "slug"=>"why-am-i-exceeding-the-quota", "posts_count"=>3, "reply_count"=>0, "highest_post_number"=>3, "image_url"=>nil, "created_at"=>"2018-06-01T12:56:12.120Z", "last_posted_at"=>"2018-06-15T16:41:44.736Z", "bumped"=>true, "bumped_at"=>"2018-06-15T16:41:44.736Z", "unseen"=>false, "pinned"=>false, "unpinned"=>nil, "visible"=>true, "closed"=>false, "archived"=>false, "bookmarked"=>nil, "liked"=>nil, "tags"=>["storage", "quota"], "category_id"=>26, "has_accepted_answer"=>false}
它们完全相同,我认为这意味着 page 变量没有生效?当我在 Chrome 开发者工具中检查时,触发点是向下滚动(因为帖子会在窗口中自动加载),我可以确认 page=2 是正确的参数:
Request URL: https://ask.cyberinfrastructure.org/search?q=Q%26A%20%23q-a&page=2
Request Method: GET
Status Code: 200 (from ServiceWorker)
Referrer Policy: strict-origin-when-cross-origin
或者更简单,直接查看参数列表:
Query String Parameters
q: Q&A #q-a
page: 2
这不是表单提交,因此按照示例,我没有看到任何“Form Data”。