Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Bing API search in dotCMS (using JSON) - With page numbering, spelling suggestion, and "deep links"
- ## Search form on page submits to same page
- ## THIS CODE WILL NOT WORK AS OF AUGUST 1, 2012 - The Bing Search API has been deprecated in favor of a paid service.
- ## We're switching to Google Site Search XML
- <form action="${VTLSERVLET_URI}">
- <div>
- <input id="box" type="text" name="q" size="15" maxlength="255" value="" />
- <input type="submit" value="Submit" /><br />
- </div>
- </form>
- ####################Bing API Search results
- ##### Set some variables manually
- #set($resultsperpage=5)
- #set($sites=$UtilMethods.encodeURL("(site:www.yoursite1.com OR site:www.yoursite2.com OR site:www.yoursite3.com)"))
- ##### Set some variables using the query string (e.g. www.yoursite.com/?q=kermit&p=4)
- #set($mySearch=$UtilMethods.encodeURL($request.getParameter("q")))
- ## Get page number from query string if it exists, otherwise default to "1"
- #if($UtilMethods.isSet($request.getParameter("p")))
- #set($pagenumber=$webapi.parseInt($request.getParameter("p")))
- #else
- #set($pagenumber=1)
- #end
- ## Determine what the next and previous page numbers are
- #set($nextpage=$math.add($pagenumber,1))
- #set($prevpage=$math.sub($pagenumber,1))
- ## Use the page number and results per page to figure out the offset of results
- #set($resultsoffset=$math.mul($resultsperpage,$math.sub($pagenumber,1)))
- #if($pagenumber!=1)
- #set($resultsoffset=$math.add($resultsoffset,1))
- #end
- ## Load the JSON from Bing
- #set($appId="YOURBINGAPPID") ## Get it here: http://www.bing.com/developers/
- #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")
- #set($myjson = $json.fetch("$path"))
- ## Displays the JSON address for debugging purposes. Can safely be removed
- <p><b>Address:</b> $path</p>
- <h1>Searching for "$!{request.getParameter('q')}"</h1>
- ## Use Bing's spelling correction if it thinks there's a more likely spelling
- #foreach ($spelling in $myjson.SearchResponse.Spell.Results)
- <p>(maybe you meant "$!{spelling.Value}"?))</p>
- #end
- ### Figure out which of results we're showing - The first result is "1" on the first page, even though offset is "0"
- #set($firstshown = $resultsoffset)
- #if($firstshown ==0)
- #set($firstshown=1)
- #end
- #set($lastshown = $math.sub($math.add($firstshown,$resultsperpage),1))
- #set($lastshowndisplay = $lastshown)
- ## Tweak the display to show correct last number if on the last set of results
- #if($lastshown > $myjson.SearchResponse.Web.Total)
- #set($lastshowndisplay = $myjson.SearchResponse.Web.Total)
- #end
- <p>Showing results: $firstshown to $lastshowndisplay of $myjson.SearchResponse.Web.Total</p>
- <ul>
- #foreach($result in $myjson.SearchResponse.Web.Results)
- <li>
- <h2><a href="$result.Url">$result.Title</a></h2>
- <p>$!{result.Description}</p>
- <a href="$result.Url">$result.DisplayUrl</a> ( <a href="$result.CacheUrl">Cached</a> )
- #if($UtilMethods.isSet($result.DeepLinks))
- <ul>
- #foreach($deeplink in $result.DeepLinks)
- <li><a href="$!{deeplink.Url}">$!{deeplink.Title}</a> </li>
- #end
- </ul>
- #end
- </li>
- #end
- </ul>
- ##Only display "previous" link if we're not on the first page
- #if($myjson.SearchResponse.Web.Offset != 0)
- <a href="${VTLSERVLET_URI}?q=$UtilMethods.encodeURL($request.getParameter('q'))&p=${prevpage}" class="previousresults">Previous Results</a>
- #else
- <span class="previousresults">Previous Results</span>
- #end
- ##Only display "next" link if we're not on the last page
- #if($lastshown <= $myjson.SearchResponse.Web.Total)
- <a href="${VTLSERVLET_URI}?q=$UtilMethods.encodeURL($request.getParameter('q'))&p=${nextpage}" class="nextresults">Next Results</a>
- #else
- <span class="nextresults">Next Results</span>
- #end
Advertisement
Add Comment
Please, Sign In to add comment