Searching Using Facets

The following provides an example of searching using facets.

import PT_SEARCH:*;

/* The following 3 lines are same for any implementation */
Local PT_SEARCH:SearchFactory &factory = create PT_SEARCH:SearchFactory();
Local PT_SEARCH:SearchQueryService &svc = &factory.CreateQueryService("default");
Local PT_SEARCH:SearchQuery &qry = &svc.CreateQuery();

/* This is the keywords user is searching for; */
&qry.QueryText = "a";

/*
&qry.StartIndex , For the first request this is always 1
When using pagination to get the next set of results this can be changed to ⇒
DocsRequested + StartIndex ex: here 51 on next request is 101..
The maximum number of results that can come back is 200 which is configurable ⇒
the max results for this query can be fetched from SearchResultCollection ⇒
hit count
*/
&qry.StartIndex = 1;

/*
&qry.DocsRequested , Number of results to be returned by the search , It is ⇒
recommended to keep this default to 50 for performance reasons and show 10 ⇒
results per page and if the end user is requesting for 6th page request with ⇒
start index to 51;
*/
&qry.DocsRequested = 50;

/*
&qry.RemoveDuplicates ,&qry.MarkDuplicates Currently these two should always be ⇒
set to false as our we generate unique id for every search document.
&qry.RemoveDuplicates = False;
*/
&qry.MarkDuplicates = False;
/*
This should be set to empty value to search in documents from all languages .
&qry.Language This can be provided a value to search in language specific documents
Currently the frame work does not allow searching in selected languages , its ⇒
either all or one language
*/
&qry.Language = %Language_User;
/*
&qry.Categories , Array of categories on which the search can be performed on
If not specified we will search in all categories
*/
Local PT_SEARCH:SearchCategory &srchCat = create PT_SEARCH:SearchCategory("MY_SRCH_⇒
CAT");
&qry.Categories = CreateArray(&srchCat);

/*
&qry.RequestedFields , Array of custom attribute values in the search result
Optional , if not specified all the standard information like URL , body , title ⇒
are still returned
*/
&qry.RequestedFields = &srchCat.GetRequestedFields();

/*
&qry.FacetFilters , Array of facets filter the search result by
Optional , If not specified No facets will be returned
Limitation : Only documents having this facet and path (If included) will be ⇒
returned in the search result.
FacetFilter constructor will take two arguments 1.Facet name , 2.Facet Path
FacetFilter is required when dealing with hierarchy facets Ex: Attribute LOCATION ⇒
has COUNTRY/STATE/CITY
To filter results for country USA the request will be FacetName: LOCATION , Facet ⇒
Filter: USA
To filter results for country USA and State CA the request will be FacetName: ⇒
LOCATION , FacetFilter: USA/CA

&qry.FacetFilters = CreateArray(create PT_SEARCH:FacetFilter("SETID", ""));
&qry.FacetFilters.Push(create PT_SEARCH:FacetFilter("STATE", ""));
*/

&qry.FacetFilters = &srchCat.GetFacetFilters();

/*
The other optional facet request related query parameters that can be set when ⇒
 searching for facets
 ReturnFacetValueCounts -- returns the counts only if this is set to true , ⇒
 default is true , set this to false if we do not want to display counts.
 MinmumDocumentsPerFacetValue -- only return facet value whose document hit count ⇒
 is more that this , ex: only facets values which have hit count more than 5 or 1;
 MaximumNumberOfFacetValues -- the children facets can be huge list , this can ⇒
 limit the return list
 SortFacetValuesBy -- ALPHA_DES , ALPHA_ASC , COUNT_DES , COUNT_ASC
 the max fetch returns the only top few children this can be used to provide ⇒
 which  top children to return
*/

/*
Execute the query and get back the results
*/
Local PT_SEARCH:SearchResultCollection &res = &qry.Execute(&qry.StartIndex, ⇒
&qry.DocsRequested);

/*
Find the number of results returned by the search , this will always be less than ⇒
or equal to "&qry.DocsRequested"
*/
Local integer &resultSize = &res.GetDocumentCount();
/*
Find the number of potential results that can be returned;
This can be used to determine if we get the next set of results and how many ⇒
pages that can be shown to the user
*/
Local integer &estmatedResultCount = &res.GetEstimatedHitCount();

Local string &outHtml;

/*
FacetNode will have the facet values and the hit count for the results
GetFacetNodes will return array of the facet nodes in the result
*/
Local array of PT_SEARCH:FacetNode &facetNodes = &res.GetFacetNodes();

/*
Facet Node has various information which can be retrieved.
&facetNodes [&i].DocumentCount will return hit count
&facetNodes [&j].FacetValue , is the facet value before translation
&facetNodes [&i].DisplayValue , translated facet value
&facetNodes [&i].Path , to which the facet value belongs to
&facetNodes [&i].getChildNodes() will return child facet nodes and will have all ⇒
above properties
*/

/*
loop through facet nodes and there children and render it in the UI
*/
If &facetNodes <> Null And
      &facetNodes.Len <> 0 Then
   &outHtml = &outHtml | "<table border='1'><tr><th>Name</th><th>Value⇒
</th><th>DisplayValue</th><th>DocCount</th><th>Path</th><th></th></tr>";
   For &i = 1 To &facetNodes.Len
      &outHtml = &outHtml | "<tr><td>" | &facetNodes [&i].FacetName | "</td>⇒
<td>" | &facetNodes [&i].FacetValue | "</td><td>";
      &outHtml = &outHtml | &facetNodes [&i].DisplayValue | "</td><td>";
      &outHtml = &outHtml | &facetNodes [&i].DocumentCount | "</td><td>" | ⇒
&facetNodes [&i].Path | "</td></tr>";
      If &facetNodes [&i].HasChildren Then
         &outHtml = &outHtml | "<tr><th>Child</th><th>Name</th><th>Value⇒
</th><th>DisplayValue</th><th>DocCount</th><th>Path</th></tr>";
         Local array of PT_SEARCH:FacetNode &childFacetNodes = &facetNodes ⇒
[&i].getChildNodes();
         For &j = 1 To &childFacetNodes.Len
            &outHtml = &outHtml | "<tr><td></td><td>" | &childFacetNodes ⇒
[&j].FacetName | "</td><td>" | &childFacetNodes [&j].FacetValue;
            &outHtml = &outHtml | "</td><td>" | &childFacetNodes [&j].DisplayValue;
            &outHtml = &outHtml | "</td><td>" | &childFacetNodes [&j].⇒
DocumentCount | "</td><td>" | &childFacetNodes [&j].Path | "</td></tr>";
         End-For;
      End-If;
   End-For;
   &outHtml = &outHtml | "</table>";
Else
   &outHtml = &outHtml | "<br><bold>No Facet Nodes</bold>";
End-If;
/*
&res.Item(&i) returns a PT_SEARCH:SearchResult for each search result document ⇒
which has various information which can be retrieved for display purposes
&srchResult.GetUrlLink , returns the URL which can take the user to the ⇒
transaction page
&srchResult.GetTitle , returns the Title for the search result document
&srchResult.GetSummary , returns the body for the search result document
&srchResult.GetScore , returns the score for the search result document
&srchResult.GetCategoryNames , returns the search categories this search result ⇒
document belongs too
&srchResult.GetCustomAttributes , will return a SearchFieldCollection which has ⇒
custom search attribute values
*/

/*
loop through each result and render it in the UI
*/
Local PT_SEARCH:SearchResult &srchResult;
For &i = 1 To &resultSize
   &srchResult = &res.Item(&i);
   &outHtml = &outHtml | "<font size=""2""><hr/>Title: <a href="" " | ⇒
&srchResult.GetUrlLink() | """>" | &srchResult.GetTitle() | "</a>";
   rem &outHtml= &outHtml| "";
   &outHtml = &outHtml | "<br>Summary: " | &srchResult.GetSummary();
   &outHtml = &outHtml | "<br>Score: " | &srchResult.GetScore();
   &outHtml = &outHtml | " Source Group: ";
   If Not &srchResult.GetCategoryNames() = Null Then
      For &j = 1 To &srchResult.GetCategoryNames().Len
         &outHtml = &outHtml | " " | &srchResult.GetCategoryNames()[&j];
      End-For;
   End-If;
   &outHtml = &outHtml | "<font size=""2"" color=#009933><br>Url: " | ⇒
&srchResult.GetUrlLink() | "</font>";
   &outHtml = &outHtml | "<font size=""2"" color=#097054><br>Signature: " | ⇒
&srchResult.GetSignature() | "</font>";
   &outHtml = &outHtml | "<font size=""2"" color=#097054> / Content Length: " ⇒
| &srchResult.GetContentLength() | "</font>";
   &outHtml = &outHtml | "<font size=""2"" color=#097054> / Language: " | ⇒
&srchResult.GetLanguage() | "</font>";
   &outHtml = &outHtml | "<font size=""2"" color=#000000><br>Last Modified: " ⇒
| &srchResult.GetLastModified() | "</font>";
   Local PT_SEARCH:SearchFieldCollection &customs = &srchResult.⇒
GetCustomAttributes();
   If (&customs <> Null) Then
      For &j = 1 To &customs.Count()
         &outHtml = &outHtml | "<br>" | &customs.Item(&j).Name | "=" | ⇒
&customs.Item(&j).Value;
      End-For;
   End-If;
   &outHtml = &outHtml | "</font>";
End-For;
&outHtml = &outHtml | "</BODY></HTML>";