Configuring global search programmatically
1C:Enterprise language has an object model for global search operations. It allows you to catch many global search events and change the standard global search behavior.
For example, you can exclude some search types.
As already mentioned, the global search runs everywhere, and the number of search result items (especially with significant amounts of data) might be too large, and the search might slow down significantly.
For example, if you enter the sales expression in the search string, in addition to the data you need, you will get many help sections with the "?" icon (fig. 19.9).

Fig. 19.9. Search by the sales expression
To exclude unnecessary results, set up a global search manager upon the system startup. To access the manager, use the GlobalSearch global context property. The manager has a SearchPlan property. This is a collection that contains the search types to be performed.
If you do not want the search to be performed in the help or somewhere else, just delete this search type from the collection.
In Designer mode
Go back to Designer and right-click the root of the configuration object tree. Then clickOpen application module.
Create the OnStart event handler in the application module and fill it in as follows (listing 19.1).
Listing 19.1. The OnStart() event handler
Procedure OnStart()
// Get the existing search type.
SearchPlan = GlobalSearch.GetPlan();
// Exclude Help and the "Functions for technician" menu from the search scope. SearchPlan.Delete(StandardGlobalSearchType.FunctionsForTechnicalSpecialist);
SearchPlan.Delete(StandardGlobalSearchType.Help);
// Apply the changes.
GlobalSearch.SetPlan(SearchPlan);
EndProcedure
In this handler, you first use the GetPlan() method to get the entire search plan collection of the GlobalSearch manager. Then you remove standard global search types that you do not need from this collection. Finally, you replace the old search plan with a new truncated one using the SetPlan() method.
In 1C:Enterprise mode
Start 1C:Enterprise and enter sales in the search string. The search result will be as follows (fig. 19.10).

Fig. 19.10. Search by the sales expression
Now perform your own search by a pseudo command.
For example, in the application, a common task is to search for service documents by a part of a document number.
If you just enter a part of a number into the search string, you will get a lot of results, as the global search searches everywhere. It might also not be fast enough, as the full-text data search is used (fig 19.11).

Fig. 19.11. Standard search by a part of a document number
To fix this issue, you can make the search by document number a separate "search type" and implement your own "pseudo command" search algorithm for it.
For example, if a user wants to find a service document by a part of its number, they first need to enter "No" and then a part of the number they are looking for. For example, "No1".
In Designer mode
To do this, use the OnGlobalSearch event. This event occurs when the user pauses while typing something in the search field. Process this event in the application module.
- If the first character in the search string is "No", remove all the items from the search plan and add your own item there. Write the operation algorithm in the SearchDocsByNumber() procedure and put it in the GlobalSearchInData common module (see listing 19.3).
- Create the OnGlobalSearch() event handler in the application module and fill it in as follows (see listing 19.2).
Listing 19.2. The OnGlobalSearch() event handler
Procedure OnGlobalSearch(SearchString, SearchPlan)
If StrFind(SearchString, "No") = 1 Then
// Make a custom search type what would look up only by document number.
SearchPlan.Clear();
SearchPlan.Insert(0, " SearchDocsByNumber", "GlobalSearchInData", True, False);
EndIf;
EndProcedure
- Then create the GlobalSearchInData common non-global server module in the configuration (the Global checkbox is cleared, and the Server checkbox is selected by default). Put the SearchDocsByNumber() procedure in it and fill it as follows (listing 19.3).
Listing 19.3. The SearchForDocumentsByNumber() procedure
Procedure SearchForDocumentsByNumber(SearchString, SearchResult, AddlParameters) Export
NumberFragment = Right(SearchString, StrLen(SearchString) - 1);
//If the search string contains only "No", do nothing;
If NumberFragment = "" Then
Return;
EndIf;
// Select service documents with a number that contains the entered string.
Query = New Query("SELECT Ref, Number, Presentation
|FROM Document.Services
|Where Number LIKE &NumberFragment");
Query.SetParameter("NumberFragment", "%" + NumberFragment + "%");
Result = Query.Execute();
Selection = Result.Select();
While Selection.Next() Do
// Create a search result item and add it to the collection.
ResultItem = New GlobalSearchResultItem(
Selection.Ref,
StrFindAndHighlightByAppearance(Selection.Presentation, NumberFragment));
SearchResult.Add(ResultItem);
EndDo;
EndProcedure
In this procedure, you receive only those services documents whose number contains the required string fragment. Then, as a result of the global search, you add your own items. Each item contains a link to the found document and a presentation with the found fragment highlighted.
- To highlight items, use the StrFindAndHighlightByAppearance() function, to which you pass the line that will be displayed in the search result as the first parameter, and the fragment that needs to be highlighted in this line as the second parameter.
Now you only need to supplement the search description with new features that you have just implemented.
- The global search has a hint. To open it, click Search for. You can change this hint.
To do this, add the code snippet highlighted below (listing 19.4) to the OnStart() event handler in the application module.
Listing 19.4. The OnStart() event handler
Procedure OnStart()
// Get the existing search type.
SearchPlan = GlobalSearch.GetPlan();
// Exclude Help and the "Functions for technician" menu from the search scope.
SearchPlan.Delete(StandardGlobalSearchType.FunctionsForTechnicalSpecialist);
SearchPlan.Delete(StandardGlobalSearchType.Help);
// Apply the changes.
GlobalSearch.SetPlan(SearchPlan);
// Set a custom search box hint.
GlobalSearch.SetDescription("Enter a search string, reference or arithmetical expression." +
Chars.LF +
"To search for a service document, enter ""No."" followed by the document number (without whitespaces).");
EndProcedure
In 1C:Enterprise mode
Start 1C:Enterprise and enter "No1" in the search string. The search result will be as follows.
As you can see, the search result will contain much fewer items, and the search will be performed much faster. If you do not enter "No" as the first character in the search string, the global search will run as usual.
When you click Search for, you will see the hint given above.