2

I want to fetch some questions from the API and I would like to sort them. However, I don't want to sort by the default options (activity, votes, creations, hot, week, month), but instead, for example, by answer_count or upvote_count or other criteria mentioned in the filter.

Is there any way to do it directly from the API? And if it can't be done, what script should be used to do the work?

2 Answers 2

3

You can filter (not sort) questions (and to some extent answers) by using the /search/advanced route of the API.

The q parameter allows most of the Advanced Search Options.

For example, this will fetch questions tagged python with at least 2 answers, and that have a score of 50 or more:

/2.2/search/advanced?page=1&pagesize=100&q=score:50&answers=2&tagged=python&site=stackoverflow


Related questions:

3
  • That makes sense, but what about a filter option which can't be searched in advanced search? Like delete vote count or up/downvote count? That'd need to fetch all questions needed and sort them. Commented Aug 12, 2019 at 15:48
  • @double-beep, Yes the API -- and the site itself -- do not allow all criteria that you can think of. That will always be the case. So, yes, you would need to (A) brute-force it with the API, or (B) accept week-old data with SEDE, or (C) use a hybrid approach whereby the API just fetches changes since your SQL-dump's (or SEDE's) last update. Commented Aug 12, 2019 at 16:00
  • However, SEDE doesn't have all features SE API has. E.g. it doesn't provide information on bounties, del/cv counts, etc., right? I guess I will have to work with the API. Commented Aug 12, 2019 at 20:54
2

No, the API has a limited number of sort options (just like the website itself). The best you can do with the API is to fetch all results and then perform a client-side sort.

Alternatively, use SEDE which is much more flexible when it comes to sort orders, but it has the disadvantage of not providing real-time data. And it doesn't have an API, so it's only an option for 'manual' investigations.

3
  • The problem is exactly that - I need real-time data, so I need to use the API. I will take a look at the paging, anyway. Commented Aug 12, 2019 at 12:01
  • @double-beep, there are 18 million questions on Stack Overflow. That means it would take you 18 days to download all questions (and their answers, if you set the filter right) via the API. That assumes that you registered for a key that raised your quota to 10K. ... But, the system would probably block you before you could finish. Commented Aug 12, 2019 at 15:00
  • 1
    @BrockAdams I want to search questions where their recent activity is between {date} and {date}. We are talking about ~70k questions. With pagesize 100, this would take only 700 calls > 700 quota. With a key, I won't have any problem. Commented Aug 12, 2019 at 15:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .