SMILA/Documentation/Search

This page describes the search service and related parts of SMILA. This includes the query and result helpers, the processing of search requests internally, and the sample servlet used to create a simple web-based GUI for search.

What happens behind the scenes when you enter a query string and submit the form, is that, on the server side, a servlet creates a SMILA record from the HTTP parameters, uses the search service to process the search with this record, receives an enriched version of the query record and also a list of result records in XML form, and uses an XSLT stylesheet to create a result page in HTML format.

The processing inside the search service is done by calling a simple (builtin) javascript file to do the work .

If you want to use the default search servlet for your own search page, you are encouraged to use the two XSLT files creating these HTML pages as a reference or basis when building your pages.

Search Processing

As mentioned before, the internal processing is done via the search service which makes use of javascript for keeping an high level of flexbility. There is also the possibility to use BPEL workflows to steer the query throughout the system. If you want to dig deeper, a more thorough documentation of both concepts can be found at:

Since both chapters rather address the indexing side of both concepts, the following two paragraphs provide an explanation of how they're to be used in query scenarios.

Search Scripts

Available since SMILA 1.3Scripting was introduced in SMILA 1.3

In the scripting environment, this follows the same record-in, record-out semantics you may already know. Difference is, that the input record contains the query and maybe some additional parameters while the output record holds the search result. The best explanation is given by looking at the actual JS which has been used in the demo search described above. You'll find the file search.js in configuration/org.eclipse.smila.scripting.

Search Pipelines

Search workflows (or pipelines) look just like indexing pipelines, they are only used a bit differently: Instead of pushing lists of records corresponding to data source objects through them, they are invoked with a single record representing the search request. This record contains the values of the parameters which were defined by the Search API (see below). The request object can be analyzed and enriched with additional information during the workflow before the actual search on the index takes place. The results of this search are not added to the blackboard as records of their own, but are added to the request record under the key "records". Further pipelets may then do further processing based on the request data and the result record list (e.g. highlighting). Finally, the request record including the search results is returned to the client and can be presented.

Pipelet invocations look the same as in indexing pipelines. See SMILA.application/configuration/org.eclipse.smila.processing.bpel/pipelines/searchpipeline.bpel for a complete example search pipeline (the one used in the above sample).

Search Service API

The actual Search API is quite simple: SMILA registers an OSGi service with the interface org.eclipse.smila.search.api.SearchService. It provides a few methods that take a SMILA query record and the name of a search script or workflow as input, execute the script/workflow with the record, and return the result in different formats:

Available since SMILA 1.3Scripting was introduced in SMILA 1.3

Record searchWithScript(String scriptAndFunction, Record query) throws ProcessingException: This is the basic method of the search service, returning the result records as native SMILA data structures. The other methods call this method for the actual search execution, too, and then just convert the result.

org.w3c.dom.Document searchAsXmlWithScript(String scriptAndFunction, Record query) throws ProcessingException: Returns the search result as an XML DOM document. See below for the schema of the result.

String searchAsXmlStringWithScript(String scriptAndFunction, Record query) throws ProcessingException: Returns the search result as an XML string. See below for the schema of the result.

You can view the result XML when using the sample SMILA search page at http://localhost:8080/SMILA/search if you enable the Show XML result option before submitting the query.

The content of the query record basically depends a lot on the used search services. However, the Search API also includes a recommendation where to put some basic commonly used search parameters which all index integrations should honor (of course they may specify extensions that are not covered by the generic Search API). The following sections describe these recommendations.

Query Parameters

The query record mainly consists of parameters. The Search API defines the names of these parameters, the allowed values as well as the default values for a set of commonly used parameters. All implementations should use these properties if possible, i.e. they should not introduce additional parameters for the same purpose, but it may be possible that certain parameters are not supported because it is not feasible with the underlying technology. For some parameters we also defined default values. All parameters are single-valued unless otherwise specified.

query: Either a search string using a query syntax or a query record describing the query by setting values for attributes (aka fielded search). The implementer for a specific underlying technology may define a query syntax to be able to build complex search criteria in a single string. However, SMILA currently does not define an own query syntax and passes the string as is to its default search engine Solr (see there for handling and interpretation).

maxcount: The maximum number of records which should be returned to the search client. Default value is 10. Example:

<Valkey="query">meaning of life</Val><Valkey="maxcount"type="long">3</Val>

offset: The number of hits which, starting from the top, should be skipped in the search result. Default value is 0. Use this parameter to implement result list paging and to provide the user a means to navigate through the result pages: If resultSize=10, the "next page" queries can be identical to the initial query, but with resultOffset=10, 20, ... Example:

<Valkey="query">meaning of life</Val><Valkey="maxcount"type="long">3</Val><Valkey="offset"type="long">3</Val>

threshold: The minimal value of the relevance score that a result must have to be returned to the search client. Default is 0.0.

<Valkey="query">meaning of life</Val><Valkey="threshold"type="double">0.5</Val>

language: The natural language of the query. No default value. This parameter may be required for language-specific pipelets/services that need to know in which language the user is expressing his or her query to be able to deliver feasible results. Example:

<Valkey="query">sinn des lebens</Val><Valkey="language">de</Val>

indexname: Some index services (like Solr) can manage multiple indices at once. When doing so, they can use this parameter to select the index which is to be searched with the current request. However, when using such a scenario, it is recommended to configure a default index name, so that search requests will succeed without having this parameter set explicitly. Example:

<Valkey="query">meaning of life</Val><Valkey="indexname">wikipedia</Val>

resultAttributes: A multi-valued parameter containing the names of the attributes which the search engine should add to the result records. Since including too many attributes will decrease performance, the list should contain only those attributes that are needed by some pipelets for further processing after the search has taken place or for displaying the results in the end. Omitting the parameter results in getting all available attributes. Example:

<Valkey="query">meaning of life</Val><Seqkey="resultAttributes"><Val>author</Val><Val>title</Val></Seq>

highlight: A sequence of string values specifying the attribute names for which highlighting should be returned. Example:

<Valkey="query">meaning of life</Val><Seqkey="highlight"><Val>content</Val></Seq>

sortby: A sequence of maps each containing the key "attribute" (any string) and the key "order" ("ascending" | "descending") specifying that the search result should be sorted by the named attributes in the given order. Omitting this parameter results in a search result sorting by descending relevance (score, similarity, ranking, ....). Multiple maps can be added and should be evaluated in the order of their appearance. Example:

<Valkey="query">meaning of life</Val><Seqkey="sortby"><Map><Valkey="attribute">year</Val><Valkey="order">descending</Val></Map><Map><Valkey="attribute">author</Val><Valkey="order">ascending</Val></Map></Seq>

facetby: A sequence of maps each containing the key "attribute" (any string) and the key "maxcount" (long). This causes facets to be returned by the search results for the specified attributes, returning "maxcount" values for each attribute. Optionally, each facetby map may contain a map with key "sortby" with keys "order" ("ascending" | "descending") and "criterion" (any string, e.g. "count" or "value") specifying in which order to return the values (e.g. "count" by number of this per facet or "value" by attribute value name). Example:

since 1.0prior to 1.0 this was named groupby and has been merely renamed, (see mail thread

<Valkey="query">meaning of life</Val><Seqkey="facetby"><Map><Valkey="attribute">year</Val><Valkey="maxcount"type="long">10</Val></Map><Map><Valkey="attribute">author</Val><Mapkey="sortby"><Valkey="criterion">value</Val><Valkey="order">ascending</Val></Map><Valkey="maxcount"type="long">5</Val></Map></Seq>

filter: A sequence of maps describing for certain attributes which values they must have in valid result records. Each of the maps contains a key "attribute" and one or more value descriptions:

<Valkey="query">meaning of life</Val><Seqkey="filter"><Map><Valkey="attribute">author</Val><Seqkey="oneOf"><Val>pratchett</Val><Val>adams</Val></Seq></Map><Map><Valkey="attribute">year</Val><Valkey="atLeast">1990</Val><Valkey="lessThan">2000</Val></Map></Seq>

ranking: A configuration defining how to rank the search results. This is highly depending on the used search engine, so we don't specify this further in SMILA.

Result Annotations

The search result is usually the request record, enriched with result data.

records: A sequence of maps describing the actual search result, meaning the records retrieved from the index. Each record should have an additional attribute "_weight" describing the relevance score of this record with respect to the query. The size of the "record" sequence is limited by the "maxcount" parameter.

_highlight: The annotation of the result record, usually used to highlight relevant sections from the result documents in order to allow the user to see at one glance if it suits what he or she was looking for. What is returned here exactly, depends on the used search engine. For example, the Solr integration in SMILA returns the raw form of the text and information about the matching parts to be highlighted. Example:

<Seqkey="records"><Map>
...
<Mapkey="_highlight"><Mapkey="content"><Valkey="text">... To be or not to be ...</Val><Seqkey="positions"><Map><Valkey="start"type="long">7</Val><Valkey="end"type="long">9</Val><Valkey="quality"type="long">100</Val></Map><Map><Valkey="start"type="long">20</Val><Valkey="end"type="long">22</Val><Valkey="quality"type="long">95</Val></Map></Seq></Map><Map>
...
</Map>

Using the HighlightingPipelet this can be transformed into a highlighted text fragment (here using * as the highlight tag):

<Seqkey="records">

<Map><Valkey="_weight"type="double">0.95</Val><Valkey="_recordid">file:hamlet</Val><Valkey="title">Hamlet</Val><Valkey="author">Shakespeare</Val><Mapkey="_highlight"><Mapkey="content"><Valkey="text">... To *be* or not to *be* ...</Val></Map><Map>
...

</Map>

Helper Classes

There are some classes that help a client to create query records with their annotations and to read result records and their annotation. You can find them in package org.eclipse.smila.search.api.helper:

QueryBuilder: A helper class for building queries and sending the query to search service. Returns a result in the form of the next class:

ResultAccessor: A wrapper for the complete search result. Provides methods to access the basic top-level result annotations and to access each search result record wrapped by a:

ResultRecordAccessor: Defines methods for accessing some of the result record annotations.

See the source code or JavaDocs for more details on the provided methods.

SMILA Search Servlet

In addition to the "search backend", SMILA contains a simple servlet that creates a query record from HTTP parameters and displays the result as an HTML page by converting the XML search result using an XSLT stylesheet. This servlet is intended for quick demos only, not for productive use. It is usually deployed in the Jetty instance that comes with SMILA at /SMILA/search. On first invocation, it currently creates a quite empty query record (it sets some default parameters like maxcount etc.) and processes it with the default pipeline "SearchPipeline". The pipeline should be able to process such a query and return an empty result list, not an error. The XML representation of this empty result is then transformed using the default stylesheet ("SMILASearchDefault") to present an initial search page.

Note that the servlet actually enriches the XML search result a bit, so the input for the XSLT stylsheet does not completely conform to the defined XML schema. Currently, it adds a section containing the names of indices available in Solr so that the search page can display the names for selection on the left side:

<SearchResultxmlns="http://www.eclipse.org/smila/search"><Script>search.process</Script><!-->'Workflow' instead of 'Script' also possible<--><Recordxmlns="http://www.eclipse.org/smila/record"><!-- effective query and embedded result records ---></Record><!-- part added by SearchServlet --><IndexNames><IndexName>test_index</IndexName></IndexNames></SearchResult>

You can use the same mechanism to add other information to the XML that is necessary for displaying purposes in the search form but not contained in the search service result: You just have to implement your own servlet or extend the default servlet. Please refer to the source code for details.

XSLT Stylesheets for SMILA search and result pages

The stylesheets are loaded from the configuration directory org.eclipse.smila.search.servlet and are selected by adding the HTTP parameter "style" to the URL. The value of this parameter must be the filename of the desired stylesheet without the suffix. The file's extension must bei .xsl. The servlet currently uses the hardcoded default name "SMILASearchDefault" if no other value was set.

In the default application, three stylesheets are avaiable:

SMILASearchDefault: The default search page. Use this as a reference on how to describe simple queries and present result lists, including paging through bigger results.

SMILASearchAdvanced: Same layout for the result list but demostrates how to create more complex query records with attribute values and filters.

SMILASearchTest: Primitive layout without paging but demonstrates the setting of even more query features.

In the following we will describe how to set query record features using the servlet. Please have a look at those sample stylesheets for complete examples on how to apply them, as we will not present something like a full tutorial here (-;

Setting parameters

To set a parameter, just use the parameter name as the HTTP parameter name. All values for this HTTP parameter are added to the "parameters" annotation of the query record. E.g., to set the resultSize parameter to 7 using a hidden HTML input field, use:

<inputtype="hidden"name="resultSize"value="7"/>

See below for naming rules for the HTTP parameter names to set attribute literals and annotations. Note that you cannot set a parameter with a name that matches one of these rules.

Setting attributes

You can add literal string values to attributes using "A.<AttributeName>" as the HTTP parameter name. E.g., to set a value from an HTML text input field as a literal in attribute "Title", use:

<inputtype="text"name="A.Title"/>

Setting other parameters

To add a "sortby" parameter for an attribute, use "sortBy.<AttributeName>=<order>", e.g.

<inputtype="hidden"name="sortby.FileSize"value="descending"/>

To create a filter for an attribute, use HTTP params:

"F.val.<AttributeName>" to add filter values to an "oneOf" filter.

"F.min.<AttributeName>" and "F.max.<AttributeName>" to set the lower/upper bounds of an "atLeast"/"atMost" filter.

If both "F.val" and "F.min/F.max" paramaters are set, the servlet will create both an enumeration filter and a range filter with the same filter mode. It depends on the used search engine integration what happens in this case. E.g.

To set a filter for attribute MimeType restricting the result to HTML documents, use:

<inputtype="hidden"name="F.val.MimeType"value="text/html"/>

To set a filter for attribute FileSize restricting the result to document sizes between 1000 and 10000 bytes, use:

To set a value in the ranking parameter for the complete record or an attribute, use "R[.<AttributeName>].<ValueName>". E.g., the following input field adds a parameter "Operator=OR" to attribute "Content":

<inputtype="hidden"name="R.Operator.Content"value="OR"/>

Adding attachments

Attachments can be added to the query record by adding file upload fields to the search form, for example:

<inputtype="file"name="Content"/>

If the user selects a file for this field, it will be uploaded to SMILA and added as attachment "Content". Of course, there must be pipelets in your search pipeline that can process this attachment. Note, that the attachments will be kept in memory in a default SMILA configuration, so they should not be too large.

Record Search Servlet

In addition there exists the very basic Record Search Servlet available at /SMILA/recordsearch.

You can do a POST or GET request on this URL with a SMILA search record in XML representation as the request body. The servlet then parses the given XML and calls the Search Service. The default is to use the SearchPipeline but you can define any other pipeline by adding the _workflow annotation to the search record with the respective pipeline name:

<Record><Valkey="query">meaning of life</Val><Valkey="_workflow">OtherSearchPipeline</Val></Record>

To switch to search processing via Javascript, use the annotation _script:

<Record><Valkey="query">meaning of life</Val><Valkey="_script">search.process</Val></Record>

The servlet returns the XML representation of the record returned by the Search Service as is, in which you can find the search results (see above).