0

I want to be able to query Wikimedia commons to be able to find a page or category.

Naïvely I thought https://commons.wikimedia.org/w/api.php?action=query&list=search&srsearch=Albert%20Einstein would do, however as you can see it does not return the page I want (Albert Einstein).

Also is there any way to restrict the search to only return categories? For example: Category: Albert Einstein

EDIT: I figured out what I wanted, this will give back categories and pages.

https://commons.wikimedia.org/w/api.php?action=query&generator=search&prop=info&gsrsearch=Albert%20Einstein&gsrnamespace=14|0

Termininja
  • 6,620
  • 12
  • 48
  • 49
thewormsterror
  • 1,608
  • 14
  • 27

1 Answers1

1

By default your query returns only 10 results, so add srlimit=500 in your request and you will see Albert Einstein.

https://commons.wikimedia.org/w/api.php?action=query&list=search&srsearch=Albert%20Einstein&srlimit=500

To restrict the search to only return categories you need to use srnamespace=14:

https://commons.wikimedia.org/w/api.php?action=query&list=search&srsearch=Albert%20Einstein&srlimit=500&srnamespace=14
Termininja
  • 6,620
  • 12
  • 48
  • 49
  • Actually now I just realized the query I wrote did have Albert Eistein as the first result. It's just that I was expecting a page id, would you know how to get the page_id returned? The api docs https://commons.wikimedia.org/w/api.php?action=help&modules=query%2Bsearch don't mention anything. – thewormsterror May 25 '16 at 21:24
  • Nevermind, I figured it out, thanks for your help though :) – thewormsterror May 25 '16 at 21:44
  • PageID you can get with generator: https://commons.wikimedia.org/w/api.php?action=query&generator=search&gsrsearch=Albert%20Einstein – Termininja May 26 '16 at 03:25