2

i'm working on the migration of my webstite form the Bing Azure API (v2) to the new Bing V5 search API.
On the old API, an object use this "__next" to tell if there's something else after him or not.
But on the new API the json do not return this anymore.
I'm working on upgrading my pagination and i don't know how to do it without this element.
Anyone know what replace this in the new API ?
I can't find any information on their migration guide or in the new V5 API guide.
Thanks.

tonarii
  • 25
  • 6

2 Answers2

0

You should read the totalEstimatedMatches value the first time you call the API, then use the &count and &offset parameters to page through the results as described here: https://msdn.microsoft.com/en-us/library/dn760787.aspx.

0

John is right. You use the count and offset params in conjunction with the totalEstimatedMatches from the value in the json of the first object returned.

Example: Imagine you love rubber-duckies so much that you want every single webpage in existance that contains the term 'rubber-ducky.' WELL TOUGH LUCK BC THATS NOT HOW THE INTERNET WORKS. Don't kill yourself yet however, Bing knows a lot about webpages containing 'rubber-ducky,' and all you'll need to do is paginate through the 'rubber-ducky'-related sites that Bing knows about and rejoice.

  • First, we need to tell the API that we want "some" results by passing 'rubber-ducky' to it(the value of "some" is defined by the count param, 50 is the max).

  • Next, we'll need to look in the first JSON object returned; this will tell us how many 'rubber-ducky' sites that Bing knows about in a field called totalEstimatedMatches.

  • Since we have an insatiable hunger for rubber-ducky-related websites, we're going to set up a while-loop that alternates b/w querying and incrementing offset and that does not stop until totalEstimatedMatches and offset are count distance apart.

Here's some python code for clarification:

>>> import SomeMagicalSearcheInterfaceThatOnlyNeeds3Params as Searcher
>>> 
>>> SearcherInstance = Searcher()
>>> SearcherInstance.q = 'rubber-ducky'
>>> SearcherInstance.count = 50
>>> SearcherInstance.offset = 0
>>> SearcherInstance.totalEstimatedMatches = 0
>>> 
>>> print SearcherInstance.preview_URL
'https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=rubber%2Dducky&count=50&offset=0'
>>> 
>>> json_return_object = SearcherInstance.search_2_json()
>>> 
>>> ## Python just treats JSON as nested dictionaries.
>>> tem = json_return_object['webPages']['totalEstimatedMatches']
>>> print tem
9500000
>>> num_links_returned = len(json_return_object['webPages']['value'])
>>> print num_links_returned
50
>>> 
>>> ## We'll set some vals manually then make our while loop.
>>> SearcherInstance.offset += num_links_returned
>>> SearcherInstance.totalEstimatedMatches = tem
>>>
>>> a_dumb_way_to_store_this_much_data = []
>>>     
>>> while SearcherInstance.offset < SearcherInstance.totalEstimatedMatches:
>>>     json_response = SearcherInstance.search_2_json()
>>>     a_dumb_way_to_store_this_much_data.append(json_response)
>>>     
>>>     actual_count = len(json_return_object['webPages']['value'])
>>>     SearcherInstance.offset += min(SearcherInstance.count, actual_count)

Hope this helps a bit.

Rob Truxal
  • 5,856
  • 4
  • 22
  • 39