Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cant search all in case setting param 'elements' #145

Open
qiyuren opened this issue Jan 30, 2024 · 1 comment
Open

cant search all in case setting param 'elements' #145

qiyuren opened this issue Jan 30, 2024 · 1 comment

Comments

@qiyuren
Copy link

qiyuren commented Jan 30, 2024

RakutenWebService::Ichiba::Item.search(
  keyword: 'apple',
  elements: 'itemCode,itemPrice,shopCode,shopName,shopUrl,tagIds'
).all.count
=> 30

RakutenWebService::Ichiba::Item.search(
  keyword: 'apple'
).all.count
=> 3000

If set elements, it can get only 30 items by all. How can I get 3000 items with param 'elements'?

elementsを指定するとallメソッドで30件(1ページ分)しか帰って来ませんが、どうしたらelementsを指定しつつ3000件返ってくるようになりますか?

@satoryu
Copy link
Member

satoryu commented Feb 2, 2024

@qiyuren Issueの作成、ありがとうございます。

調べたところ、報告していただいた通り、elementsパラメーターを指定した場合はallでは30件しか取得できません。
これはRakuten Web Service APIのレスポンスが、elementsを指定した場合にページに関する情報(pagepageCountなど)を返さないためです。
ご存知かもしれませんが念のため説明を加えますと、ここでいうページとは1回のAPIが返す件数を限定し、ページを指定することで検索結果を順番に取得する方法のことを指します。
rakuten_web_serviceallは、内部でこれらのページの情報をAPIのレスポンスから取得し、自動的にAPIを各ページの情報を取得するAPI呼び出しを実行しています。
ですので、先に書いた通り、このページに関数情報がAPIから返ってこないため、elementsを指定したときはallが期待した動作にならないことがわかりました。

代替案として、allが本来やろうとしたことを以下のように明示的に実装することで取得することができます。

page_count = RakutenWebService::Ichiba::Item.search(keyword: keyword).response.page_count # ここで検索結果の全ページ数を取得

total_items = []
(1..page_count).each do |i|  # 各ページごとに検索結果を取得する
  items = RakutenWebService::Ichiba::Item.search(keyword: keyword, elements: 'itemCode,itemPrice,shopCode,shopName,shopUrl,tagIds', page: i)
  total_items += items.to_a
end

puts "Total items: #{total_items.size}" #=> Total items: 3000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants