Faceted search queries in enterprise search collections

Use taxonomy browsers to issue a faceted query to the FacetedSearchable object in an enterprise search collection.

Enterprise search collections provide the following types of taxonomy browsers:
  • Time scale taxonomy browser
  • Facet taxonomy browser
  • Flag taxonomy browser
  • Range taxonomy browser
  • Scopes taxonomy browser
  • Rule-based taxonomy browser
The following example shows how you can obtain TaxonomyBrowser objects:
String collectionId = ...; // the collectionId
String taxnomyId = ...;    // the taxnomyId
	
// obtain the specific SIAPI Browse factory implementation
Class cls = Class.forName("com.ibm.es.api.browse.RemoteBrowseFactory");
// new BrowseFactory instance	
BrowseFactory factory = (BrowseFactory) cls.newInstance();
	
// create a valid Application ID that will be used
// by the Search Node to authorize this access to the collection
String applicationName = config.getProperty("applicationName");
ApplicationInfo applicationInfo = factory.createApplicationInfo(applicationName);
// obtain the Browse service implementation
BrowseService browseService = factory.getBrowseService(config);
// get a TaxonomyBrowser for the specified taxonomy id and collection id
TaxonomyBrowser browser = browseService.getTaxonomyBrowser(applicationInfo, collectionId, taxonomyId);
The following example shows how you can get available browsers from BrowseService:
TaxonomyBrowser[] browsers = browseService.getAvailableTaxonomyBrowsers(applicationInfo, collectionId);

Time scale taxonomy browsers

Use a time scale taxonomy browser to issue a faceted query to get the specified date scale counts in an enterprise search collection. The following example shows how to issue a faceted query to get specified date scale counts.

TaxonomyBrowser browser = timescaleBrowser;
// get category id corresponding to the facet path
// /<the date facet name>/<the specified granularity>/
// Avaliable date facet names and granularities can be known by browsing.
Category category = browser.getCategory(getIdFromTaxonomyBrowser(browser, this.facetPath));

QualifiedCategory qualifiedCategory = facetsFactory.createQualifiedCategory(
browser.getTaxonomyInfo().getID(),
category.getInfo());

Constraint constraint = facetsFactory.createConstraint();
constraint.set(Constraint.SUBCATEGORY_COUNT_MODE, 100, false, null);
qualifiedCategory.setConstraint(constraint);

TargetExpressions targetExpressions = facetsFactory.createTargetExpressions();

// if you want to get the correlation value of facet
targetExpressions.addExpression(facetsFactory.createExpression("correlation", "#correlation"));
// if you want to get the expected count value of facet
targetExpressions.addExpression(facetsFactory.createExpression("expected_count", "#expected_count"));
TargetFacet targetFacet = facetsFactory.createTargetFacet(qualifiedCategory, targetExpressions);

FacetContext facetContext = facetsFactory.createFacetContext();
facetContext.add(targetFacet);
query.setFacetContext(facetContext);

// execute the search by calling the FacetdSearchable's search method.
// A SIAPI FacetedResultSet object will be returned
FacetedResultSet rset = searchable.search(query);

Facet taxonomy browsers

Use a facet taxonomy browser to issue a faceted query to get the facets and facet values in an enterprise search collection. The first level child categories of the facet taxonomy browser returns user defined metadata facets.

Use the FacetsFactory object to obtain the QualifiedCategory object to issue a faceted query to get facets or facet values, as shown in the following example:

String facetPath = ...; // the facet path

// obtain the specific SIAPI Facets factory implementation
Class facetsCls = Class.forName("com.ibm.es.api.search.facets.RemoteFacetsFactory");
FacetsFactory facetsFactory = (FacetsFactory) facetsCls.newInstance();

// obtain the Facets Service implementation
FacetsService facetsService = facetsFactory.getFacetsService(config);
// obtain a FacetedSearchable object to the specified collection ID
FacetedSearchable searchable = facetsService.getFacetedSearchable(applicationInfo, collectionId);

// create a new FacetedQuery object using the specified
// query string
FacetedQuery query = facetsFactory.createFacetedQuery(queryString);

// set the target 
Category category = browser.getCategory(facetPath);

QualifiedCategory qualifiedCategory = facetsFactory.createQualifiedCategory(
browser.getTaxonomyInfo().getID(),
category.getInfo());

Constraint constraint = facetsFactory.createConstraint();
constraint.set(Constraint.SUBCATEGORY_COUNT_MODE, 100, false, null);
qualifiedCategory.setConstraint(constraint);

TargetExpressions targetExpressions = facetsFactory.createTargetExpressions();

// if you want to get the correlation value of facet
targetExpressions.addExpression(facetsFactory.createExpression("correlation", "#correlation"));
// if you want to get the expected count value of facet
targetExpressions.addExpression(facetsFactory.createExpression("expected_count", "#expected_count"));
TargetFacet targetFacet = facetsFactory.createTargetFacet(qualifiedCategory, targetExpressions);

FacetContext facetContext = facetsFactory.createFacetContext();
facetContext.add(targetFacet);
query.setFacetContext(facetContext);

// execute the search by calling the FacetdSearchable's search method.
// A SIAPI FacetedResultSet object will be returned
FacetedResultSet rset = searchable.search(query);

Flag, range, scope, and rule-based taxonomy browsers

Use flag, range, scope, and rule-based taxonomy browsers to issue a faceted query to get the flag, range, scope, and rule-based facets in an enterprise search collection. These taxonomy browsers are available if you configure document flagging, range facets, scopes, and rule-based categories in the administration console.

To issue a faceted query to get flag, range, or rule-based facets, add the TargetFacet object that was obtained from the FacetsFactory object to the facet context of a faceted query, as shown in the previous example for facet taxonomy browsers.

Sample programs

The BrowseExample and TimeScaleViewSearchExample sample programs are provided in the ES_INSTALL_ROOT/samples/siapi directory.