Performing a Simple Search
The following example code demonstrates how to retrieve properties of the SearchResultCollection and how to iterate over the results in the collection:
Import the application package.
import PT_SEARCH:*;
Create an instance of SearchQueryService and SearchFactory.
Local PT_SEARCH:SearchFactory &factory = create PT_SEARCH:SearchFactory(); Local PT_SEARCH:SearchQueryService &svc = &factory.CreateQueryService("default");
Instantiate the SearchQuery object.
Local PT_SEARCH:SearchQuery &srchQuery = &svc.CreateQuery();
Initialize the search query by setting properties.
&srchQuery.QueryText = "some text"; &srchQuery.Language = &lang_cd; &srchQuery.MarkDuplicates = False; &srchQuery.RemoveDuplicates = False;
Execute the search query.
Use the Execute method and specify two integer parameters: start determines the index of the first document, size determines the number of search documents to return "page" of results.
Local number &start = 1; Local number &size = 10; Local PT_SEARCH:SearchResultCollection &results; &results = &srchQuery.Execute(&start, &size);
Process the search query results
The search query returns a SearchResultCollection. To process this collection: The SearchResultCollection contains SearchResult objects. Each SearchResult has its own methods to access its attributes such as Title, Summary, URL Link, and so on. Each SearchResult also optionally has custom attributes that can be accessed through the SearchFieldCollection object.
Local number &i, &j; Local number &score; Local string &title; Local string &summary; Local datetime &lastMod; Local string &url; Local string &name; Local string &value; Rem - Get a couple return properties; Local number &docCount = &results.GetDocumentCount(); Local number &hitCount = &results.GetEstimatedHitCount(); Rem - Iterate through the search results; Local PT_SEARCH:SearchResult &curResult; Local PT_SEARCH:SearchFieldCollection &customs; For &i = 1 To &results.GetDocumentCount() &curResult = &results.Item(&i); &score = &curResult.GetScore(); &title = &curResult.GetTitle(); &summary = &curResult.GetSummary(); &lastMod = &curResult.GetLastModified(); &url = &curResult.GetUrlLink(); /* Do something with these result attributes */ Rem - Get the custom fields of the result if any; &customs = &curResult.GetCustomAttributes(); If (&customs <> Null) Then Rem - Iterate through custom fields of the result; For &j = 1 To &customs.Count() &name = &customs.Item(&j).Name; &value = &customs.Item(&j).Value; /* do something with the custom field */ End-For; End-If; End-For; Rem - Calculate the number of additional pages in the results; Local integer &pages; &pages = Idiv(&hitCount, &size); If Mod(&hitCount, &size) > 0 Then &pages = &pages + 1; End-If; Rem - Render the additional pages as links;
Get the page number for the next request.
Re-execute the search query to retrieve the next page. Increment start to the next page; then invoke the Execute method.
Rem - Get the requested page from the link clicked by the user; Rem - Store the value in the &req_page variable; Local integer &req_page; Rem - Calculate the new start value and re-execute the query; &start = (&req_page - 1) * &size + 1; &results = &srchQuery.Execute(&start, &size);
The preceding example returns the results in chunks, or pages, which provides better query performance than returning all results at once. However, you could elect to return all results in a single search query. In that case, you will need to consider the maximum number of results returned by the search engine. For example, when Elasticsearch is the search engine, 200 is the default value for the maximum number of results. In this case, the Execute method would be invoked as follows to return all results at once:
Local number &start = 1;
Local number &size = 200;
Local PT_SEARCH:SearchResultCollection &results;
&results = &srchQuery.Execute(&start, &size);