stephenjbell

Bing API search in dotCMS (using JSON)

May 5th, 2011
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. ## Bing API search in dotCMS (using JSON) - With page numbering, spelling suggestion, and "deep links"
  2. ## Search form on page submits to same page
  3.  
  4.  
  5.  
  6. ## THIS CODE WILL NOT WORK AS OF AUGUST 1, 2012 - The Bing Search API has been deprecated in favor of a paid service.
  7. ## We're switching to Google Site Search XML
  8.  
  9.  
  10. <form action="${VTLSERVLET_URI}">
  11. <div>
  12. <input id="box" type="text" name="q" size="15" maxlength="255" value="" />
  13.  
  14. <input type="submit" value="Submit" /><br />
  15.  
  16. </div>
  17. </form>
  18.  
  19.  
  20. ####################Bing API Search results
  21.  
  22.  
  23. ##### Set some variables manually
  24.  
  25. #set($resultsperpage=5)
  26. #set($sites=$UtilMethods.encodeURL("(site:www.yoursite1.com OR site:www.yoursite2.com OR site:www.yoursite3.com)"))
  27.  
  28.  
  29. ##### Set some variables using the query string (e.g. www.yoursite.com/?q=kermit&p=4)
  30.  
  31. #set($mySearch=$UtilMethods.encodeURL($request.getParameter("q")))
  32.  
  33. ## Get page number from query string if it exists, otherwise default to "1"
  34. #if($UtilMethods.isSet($request.getParameter("p")))
  35. #set($pagenumber=$webapi.parseInt($request.getParameter("p")))
  36. #else
  37. #set($pagenumber=1)
  38. #end
  39.  
  40. ## Determine what the next and previous page numbers are
  41. #set($nextpage=$math.add($pagenumber,1))
  42. #set($prevpage=$math.sub($pagenumber,1))
  43.  
  44. ## Use the page number and results per page to figure out the offset of results
  45. #set($resultsoffset=$math.mul($resultsperpage,$math.sub($pagenumber,1)))
  46. #if($pagenumber!=1)
  47. #set($resultsoffset=$math.add($resultsoffset,1))
  48. #end
  49.  
  50.  
  51.  
  52. ## Load the JSON from Bing
  53. #set($appId="YOURBINGAPPID") ## Get it here: http://www.bing.com/developers/
  54. #set($path = "http://api.bing.net/json.aspx?AppId=$!{appId}&Version=2.2&Query=$!{mySearch}+$!{sites}&Sources=web+spell&Web.Count=$!{resultsperpage}&Web.Offset=$!{resultsoffset}&JsonType=raw")
  55. #set($myjson = $json.fetch("$path"))
  56.  
  57. ## Displays the JSON address for debugging purposes. Can safely be removed
  58. <p><b>Address:</b> $path</p>
  59.  
  60. <h1>Searching for "$!{request.getParameter('q')}"</h1>
  61.  
  62. ## Use Bing's spelling correction if it thinks there's a more likely spelling
  63. #foreach ($spelling in $myjson.SearchResponse.Spell.Results)
  64. <p>(maybe you meant "$!{spelling.Value}"?))</p>
  65. #end
  66.  
  67. ### Figure out which of results we're showing - The first result is "1" on the first page, even though offset is "0"
  68. #set($firstshown = $resultsoffset)
  69. #if($firstshown ==0)
  70. #set($firstshown=1)
  71. #end
  72.  
  73.  
  74. #set($lastshown = $math.sub($math.add($firstshown,$resultsperpage),1))
  75. #set($lastshowndisplay = $lastshown)
  76.  
  77. ## Tweak the display to show correct last number if on the last set of results
  78. #if($lastshown > $myjson.SearchResponse.Web.Total)
  79. #set($lastshowndisplay = $myjson.SearchResponse.Web.Total)
  80. #end
  81.  
  82. <p>Showing results: $firstshown to $lastshowndisplay of $myjson.SearchResponse.Web.Total</p>
  83. <ul>
  84. #foreach($result in $myjson.SearchResponse.Web.Results)
  85. <li>
  86. <h2><a href="$result.Url">$result.Title</a></h2>
  87. <p>$!{result.Description}</p>
  88. <a href="$result.Url">$result.DisplayUrl</a> ( <a href="$result.CacheUrl">Cached</a> )
  89. #if($UtilMethods.isSet($result.DeepLinks))
  90. <ul>
  91. #foreach($deeplink in $result.DeepLinks)
  92. <li><a href="$!{deeplink.Url}">$!{deeplink.Title}</a> </li>
  93. #end
  94. </ul>
  95. #end
  96. </li>
  97. #end
  98. </ul>
  99.  
  100.  
  101. ##Only display "previous" link if we're not on the first page
  102. #if($myjson.SearchResponse.Web.Offset != 0)
  103. <a href="${VTLSERVLET_URI}?q=$UtilMethods.encodeURL($request.getParameter('q'))&p=${prevpage}" class="previousresults">Previous Results</a>
  104. #else
  105. <span class="previousresults">Previous Results</span>
  106. #end
  107.  
  108. ##Only display "next" link if we're not on the last page
  109. #if($lastshown <= $myjson.SearchResponse.Web.Total)
  110. <a href="${VTLSERVLET_URI}?q=$UtilMethods.encodeURL($request.getParameter('q'))&p=${nextpage}" class="nextresults">Next Results</a>
  111. #else
  112. <span class="nextresults">Next Results</span>
  113. #end
Advertisement
Add Comment
Please, Sign In to add comment