The findTerms
method is the most flexible and powerful way to search for
records within a module. It can be used to run simple single term queries or
complex multi-term searches.
The terms are specified using a Terms
object. Once a Terms
object has
been created, add specific terms to it (using the add
method) and then pass
the Terms
object to the findTerms
method. For example, to specify a
Parties search for records that contain a first name of John
and a last name
of Smith
:
import com.kesoftware.imu.Terms;
Terms search = new Terms();
search.add("NamFirst", "John");
search.add("NamLast", "Smith");
long hits = parties.findTerms(search);
There are several points to note:
- The first argument passed to the
add
method element contains the name of
the column or an alias in the module to be searched.
- An alias associates a supplied value with one or more actual columns.
Aliases are created using the
addSearchAlias
or addSearchAliases
methods.
- The second argument contains the value to search for.
- Optionally, a comparison operator can be supplied as a third argument (see
example here).
The operator specifies how the value supplied as the second argument should
be matched. Operators are the same as those used in TexQL (See Texpress Query Language Guide for details). If not supplied, the operator
defaults to "matches". This is not a real TexQL operator, but is translated
by the search engine as the most "natural" operator for the type of column
being searched. For example, for text columns "matches" is translated as
the
contains
TexQL operator and for integer columns it is translated
as the =
TexQL operator.
Note: Unless it is really necessary to specify an operator, consider using
the "matches" operator, or better still supplying no operator at all
as this allows the server to determine the best type of search.
Examples
- To search for the name
Smith
in the last name field of the Parties module,
the following term can be used:Terms search = new Terms();
search.add("NamLast", "Smith");
- Specifying search terms for other types of columns is straightforward. For
example, to search for records inserted on April 4, 2011:
Terms search = new Terms();
search.add("AdmDateInserted", "Apr 4 2011");
- To search for records inserted before April 4, 2011, it is necessary to
add an operator:
Terms search = new Terms();
search.add("AdmDateInserted", "Apr 4 2011", "<");
- By default, the relationship between the terms is a Boolean
AND
. This
means that to find records which match both a first name containing John
and a last name containing Smith
the Terms
object can be created as
follows:Terms search = new Terms();
search.add("NamFirst", "John");
search.add("NamLast", "Smith");
- A
Terms
object where the relationship between the terms is a Boolean
OR
can be created by passing the enumeration value TermsKind.OR
to
the Terms
constructor:Terms search = new Terms(TermsKind.OR);
search.add("NamFirst", "John");
search.add("NamLast", "Smith");
This specifies a search for records where either the first name contains
John
or the last name contains Smith
.
- Combinations of
AND
and OR
search terms can be created. The
addAnd
method adds a new set of AND
terms to the original
Terms
object. Similarly, the addOr
method adds a new set of
OR
terms. For
example, to restrict the search for a first name of John
and a last name of
Smith
to matching records inserted before April 4, 2011
or on May 1, 2011, specify:Terms search = new Terms();
search.add("NamFirst", "John");
search.add("NamLast", "Smith");
Terms dates = search.addOr();
dates.add("AdmDateInserted", "Apr 4 2011", "<");
dates.add("AdmDateInserted", "May 1 2011");
- To run a search, pass the
Terms
object to the findTerms
method:Module parties = new Module("eparties", session);
Terms search = new Terms();
search.add("NamLast", "Smith");
long hits = parties.findTerms(search);
As with other find methods, the return value contains the estimated number
of matches.
- To use a search alias, call the
addSearchAlias
method to associate the
alias with one or more real column names before calling findTerms
.
Suppose we want to allow a user to search the Catalogue module for keywords.
Our definition of a keywords search is to search the SummaryData,
CatSubjects_tab and NotNotes columns. We could do this by building an
OR
search:String keyword = "⋯";
// ⋯
Terms search = new Terms(TermsKind.OR);
search.add("SummaryData", keyword);
search.add("CatSubjects_tab", keyword);
search.add("NotNotes", keyword);
Another way of doing this is to register the association between the name
keywords and the three columns we are interested in and then pass the name
keywords as the column to be searched:
String keyword = "⋯";
// ⋯
Module catalogue = new Module("ecatalogue", session);
String[] columns =
{
"SummaryData",
"CatSubjects_tab",
"NotNotes"
};
catalogue.addSearchAlias("keywords", columns);
// ⋯
Terms search = new Terms();
search.add("keywords", keyword);
catalogue.findTerms(search);
An alternative to passing the columns as an array of strings is to pass a
single string, with the column names separated by semi-colons:
String keyword = "⋯";
// ⋯
Module catalogue = new Module("ecatalogue", session);
String columns = "SummaryData;CatSubjects_tab;NotNotes";
catalogue.addSearchAlias("keywords", columns);
// ⋯
Terms search = new Terms();
search.add("keywords", keyword);
catalogue.findTerms(search);
The advantage of using a search alias is that once the alias is registered a
simple name can be used to specify a more complex OR
search.
- To add more than one alias at a time, use the IMu
Map
class to build an
associative array of names and columns and call the addSearchAliases
method:Map aliases = new Map();
aliases.put("keywords", "SummaryData;CatSubjects_tab;NotNotes");
aliases.put("title", "SummaryData;TitMainTitle");
catalogue.addSearchAliases(aliases);
[Close]