Debug Template

From DBSight Full-Text Search Engine/Platform Wiki

Compare Template with Java Search API

Java Search API

+ Java Only, although other languages can theoretically be supported by generating interface via Google Protocol Buffer

+ Well documented

+ Stable, and the typed language can prevent many subtle spelling mistakes

+ Requires another layer in front of DBSight, which is more flexible, but more work.

Template

+ The FreeMarker templates are really flexible and easily customizable. It's equal to JSP, which you can modify it as you want.

+ Actually, you can use also JSP to render the results.

+ FreeMarker Templates can scaffold up very quickly.

+ Since you can get the rendering done on DBSight side, it may trim out unnecessary data, which may or may not affect the result size, depending on the actual template rendering.

Choose Between Java Search API and Template

It depends on how much data you need to transmit and how complicated the result page is.

In general, Java Search API is flexible, and the API is very stable and has public Java doc. But demands additional effort to generate the HTML results on the application server side, since there are many different renderings, for pagination, highlighting, etc.

If you want to play it safe, you can start with template. But of course, if your developers are more familiar with JSP and javadoc, you can have them code up the templates in JSP.

And if you need use data only available on your application server when rendering the JSPs, you can fall back to the Java Search API.


Template FAQ

_
Table of contents

_

How to get attributes with multiple values?

Columns from Subsequent Queries can have several values, especially for one-to-many releationships. But during rendering, how to get those values?

You can use

 doc.getValues("attribute_name")

which return a list of attribute values, instead of the normal way:

 doc.get("attribute_name")

which only get one attribute value.

You can check for more details from the java doc for

net.javacoding.xsearch.search.HitDocument

How do I ... ?

FreeMarker can use any java objects. So basically you can do anything in it. Here are some examples:

How to decode base64 encoded strings?

 <#assign base64= new("org.apache.commons.codec.binary.Base64")>
 <#assign util= new("org.apache.commons.codec.binary.StringUtils")>
 ${util.newStringUtf8(base64.decode("RGVzY2jDqm5lcw=="))}

How to include contents from other url?

This is a good way to include contents from other urls, which could be produced via PHP, Perl, etc.

<#assign httpUtil = new("net.javacoding.xsearch.utility.HttpUtil")>
${httpUtil.open("http://www.google.com")}


JSP template

Specifying JSP rendering

If Velocity is not your cup of tea, you can use JSP. In normal search.do url, you can add one more parameter

indexName=my_index_name&templateName=my_template&fileName=main.jsp

Then DBSight will look for templating file

/templates/<indexName>/<templateName>/main.jsp

Objects You can use

All objects for rendering should come from SearchResult class,

net.javacoding.xsearch.search.result.SearchResult.java

Pay some attention to

<%@ page import="net.javacoding.xsearch.search.result.SearchResult" %>
<% SearchResult searchResult = request.getAttribute("searchResult") %>

For JSP, to loop through the "$docs" object as in Velocity would be like

<%@ page import="net.javacoding.xsearch.search.HitDocument" %>
<%@ page import="java.util.*" %>
<% ArrayList<HitDocument> docs = (ArrayList<HitDocument>) searchResult.getDocs();
for(HitDocument doc : docs ){ %>
 <%= doc.get("id") %>
<% } %>