Java API index search classes and examples
The IxnMemTextSearch and IxnMemTermSearch interactions support flexible search.
For complete details, see the Java™ SDK Javadoc.
- IxnMemTextSearch
- Provides full-text search capability against a configured operational server full-text index. The API provides interfaces to either retrieve matches by using a traditional row list approach, or by using a new composite class structure. The row list API is provided as a convenience for those who want to use full-text search without significantly changing existing application code. The composite class structure is an instance of MemTextSearchResult, which handles the details of mapping data records to their corresponding entity record identifiers. This structure also allows the retrieval of stored values that are associated with documents within the index.
- IxnMemTermSearch
- Provides term search capability against a configured operational server full-text index. Search criteria is provided in the form of field name and word prefix. The field name must be a configured entity in the index and the word prefix must be at least two characters in length. Results of the search are obtained by using the getResult method, which returns a List of MemTermSearchResults.
Example: simple search
This example performs a simple search with a query specified on the command line.package your.search.example;
import madison.mpi.Context;
import madison.mpi.GetType;
import madison.mpi.MemName;
import madison.mpi.MemRow;
import madison.mpi.MemRowList;
import madison.mpi.search.IxnMemTextSearch;
public class SimpleExample
{
public static void main( String[] args)
{
//Query string comes from the command line.
String query = args[0];
//Substitute values appropriate for your MDS.
Context ctx = new Context("localhost", 9080, "mdmadmin", "mdmadmin");
//Search for members that match.
//We'll only look at the name attributes and active values.
IxnMemTextSearch ixn = new IxnMemTextSearch( ctx);
ixn.setSegCodeFilter("MEMHEAD,MEMNAME");
ixn.setRecStatFilter("A");
//Execute the interaction.
MemRowList rows = new MemRowList();
if( !ixn.execute( query, rows, GetType.ASENTITY) ){
throw new RuntimeException("Ixn failed. " + ixn.getErrText());
}
//Display the results.
for( int i = 0; i < rows.size(); ++i ){
MemRow row = rows.rowAt(i);
if( !(row instanceof MemName) ) continue;
MemName name = (MemName) row;
System.out.println( name.getSrcCode() + "/" + name.getMemIdnum()
+ " " + name.getAttrCode()
+ " " + name.getOnmFirst() + " " + name.getOnmLast());
}
}
}Example: simple search using MemTextSearchResult
This example does a simple search with a query specified on the command line but uses the MemTextSearchResult object for results. Use the MemTextSearchResult interaction to organize results by member, include the full-precision score in results, or when you want access to the stored fields.The full-precision score might differ from the score that is returned by MemHead.getMatchScore() because the full-precision score is a Java double and the MemHead score is a Java short. Stored fields are useful when you do not want to retrieve all member details in the search results. You can configure the index to hold a brief summary of a member in a stored field. The summary field can then be used to display the search results.
package your.search.example;
import java.util.Map;
import madison.mpi.Context;
import madison.mpi.GetType;
import madison.mpi.MemHead;
import madison.mpi.search.IxnMemTextSearch;
import madison.mpi.search.MemTextHitResult;
import madison.mpi.search.MemTextSearchResult;
public class SimpleExample2
{
public static void main( String[] args)
{
//Query string comes from the command line.
String query = args[0];
//Substitute values appropriate for your MDS.
Context ctx = new Context("localhost", 9080, "mdmadmin", "mdmadmin");
//Search for members that match.
//We will not retrieve any attributes.
IxnMemTextSearch ixn = new IxnMemTextSearch( ctx);
ixn.setSegCodeFilter("MEMHEAD");
ixn.setRecStatFilter("A");
//Execute the interaction.
if( !ixn.execute( query, GetType.ASENTITY) ){
throw new RuntimeException("Ixn failed. " + ixn.getErrText());
}
//Loop for each member in the result
MemTextSearchResult result = ixn.getResult();
for( MemTextHitResult hit: result.getHits() ){
//Display member identifier and score
MemHead head = hit.getMemHead();
System.out.println( head.getSrcCode() + "/" + head.getMemIdnum() + " " +
hit.getScore());
//Display any stored values.
Map storedValues = hit.getStoredValues();
for( Map.Entry entry: storedValues.entrySet() ){
System.out.println( entry.getKey() + "=" + entry.getValue());
}
}
}
}
Example: performing a term search
This example uses the IxnMemTermSearch interaction to search for matching terms. Term searches must specify a field to search. All matching terms that start with the specified prefix are returned, up to a specified maximum. The prefix must be at least two characters in length, otherwise no results are returned.
The term search is useful for implementing "instant" functionality where matching terms can be shown while you are typing a query.
package your.search.example;
import madison.mpi.Context;
import madison.mpi.search.IxnMemTermSearch;
import madison.mpi.search.MemTermSearchResult;
public class SimpleExample3
{
public static void main( String[] args)
{
//Field name, prefix and max come from the command line.
String fieldName = args[0];
String prefix = args[1];
int max = Integer.parseInt( args[2]);
//Substitute values appropriate for your MDS.
Context ctx = new Context("localhost", 9080, "mdmadmin", "mdmadmin");
//Limit the number of answers we get.
IxnMemTermSearch ixn = new IxnMemTermSearch( ctx);
ixn.setMaxRows( max);
//Execute the interaction.
if( !ixn.execute( fieldName, prefix) )
throw new RuntimeException("Ixn failed. " + ixn.getErrText());
//Display the results.
for( MemTermSearchResult match: ixn.getResult() )
System.out.println( match.getTerm() + " " + match.getFrequency());
}
}