Search

After getting access to the object, which enables the user to search, prepare context with search settings. To create a context, call the ISearchExecutor.CreateContext method. Depending on what will be searched (slices in indexed structures of sources by dimension element names or repository object metadata), cast method result to one of two interfaces: ISolrSourceDataSearchContext and ISolrMbObjectsSearchContext.

Var
    //...
    MB: IMetabase;
    SharedParams: ISharedParams;
    SearchEngine: ISolrSearchEngineService;
    SearchSchema: ISolrSearchEngineSchema;
    SearchExecutor: ISearchExecutor;
    SourceContext: ISolrSourceDataSearchContext;
    MbObjContext: ISolrMbObjectsSearchContext;
    //...
Begin
    //...
    MB := MetabaseClass.Active;
    //Search and indexing parameters specified for repository
    SharedParams := MB.SpecialObject(MetabaseSpecialObject.SharedParams).Bind As ISharedParams;
    SearchEngine := SharedParams.SearchEngine As ISolrSearchEngineService;
    SearchSchema := SearchEngine.SearchEngineSchema As ISolrSearchEngineSchema;
    //To search by dimension elements
    SearchExecutor := SearchSchema.SearchExecutor(SearchEngineTargetType.SourceData);
    SourceContext := SearchExecutor.CreateContext As ISolrSourceDataSearchContext;
    //...
    //To search for metadata of repository objects
    SearchExecutor := SearchSchema.SearchExecutor(SearchEngineTargetType.MbObject);
    MbObjContext := SearchExecutor.CreateContext As ISolrMbObjectsSearchContext;
    //...

Searching by Dimension Elements

To search and create data source slices by indexed names of dimension elements in context, determine the following minimum settings: in the Locale property determine language used for search (language affects selection of Solr instances used for work) and in the Text property determine search text. If required, it is possible to specify determine search parameters; for details see description of the ISolrSearchContext interface properties.

Minimum search settings:

Var
    //...
    SearchSchema: ISolrSearchEngineSchema;
    SearchExecutor: ISearchExecutor;
    SourceContext: ISolrSourceDataSearchContext;
    //...
Begin
    //...
    //To search by dimension elements
    SearchExecutor := SearchSchema.SearchExecutor(SearchEngineTargetType.SourceData);
    SourceContext := SearchExecutor.CreateContext As ISolrSourceDataSearchContext;
    SourceContext.Locale := LocaleCodeID.Russian;
    SourceContext.Text := "GDP Russia";
    //...

Search with filtering by field values:

Var
    //...
    SearchSchema: ISolrSearchEngineSchema;
    SearchExecutor: ISearchExecutor;
    SourceContext: ISolrSourceDataSearchContext;
    FilterCondition: ISolrFilterCondition;
    ConditionValues: ISolrFilterFieldValues;
    Values: Array Of Variant;
    //...
Begin
    //...
    //To search by dimension elements
    SearchExecutor := SearchSchema.SearchExecutor(SearchEngineTargetType.SourceData);
    SourceContext := SearchExecutor.CreateContext As ISolrSourceDataSearchContext;
    SourceContext.Locale := LocaleCodeID.Russian;
    SourceContext.Text := "GDP Russia";
    //Filtering parameters
    FilterCondition := SourceContext.FilterContext.FilterCondition;
    FilterCondition.SolrField := SearchSchema.Fields(SearchEngineTargetType.SourceData).FindById("name");
    FilterCondition.Operator_ := LogicalOperator.And_;
    ConditionValues := FilterCondition.Values;
    ConditionValues.Operator_ := LogicalOperator.Or_;
    Values := New Variant[2];
    Values[0] := "Income";
    Values[1] := "Expense";
    //...

After determining all search parameters, send the configured context to the ISearchExecutor.Search method. On executing the method the query will be created and sent to Apache Solr search platform, the search will be executed, and the answer with search results will be obtained. The result is the collection of elements where each element contains information about data slice. The output collection can be cast to the ISolrSearchResults interface, and each collection element can be cast to the ISolrSearchResult or ISearchSourceDataResult interfaces.

Var
    //...
    Schema: ISolrSearchEngineSchema;
    SearchExecutor: ISearchExecutor;
    SearchContext: ISolrSourceDataSearchContext;
    Results: ISolrSearchResults;
    Result: ISearchResult;
    i: Integer;
    //...
Begin
    //...
    SearchExecutor := Schema.SearchExecutor(SearchEngineTargetType.SourceData);
    //...
    //Set up search parameters
    //...
    //Search and get results
    Results := SearchExecutor.Search(SearchContext) As ISolrSearchResults;
    //View search results
    For i := 0 To Results.Count - 1 Do
        Result := Results.Item(i);
        Debug.WriteLine("Name: """ + Result.Name + """. Description: """ + Result.Description + '"');
        If Result.HighlightedName <> "" Then
            Debug.Indent;
            Debug.WriteLine("values with highlighting of searched value:");
            Debug.WriteLine("Name: """ + Result.HighlightedName + """. Description: """ + Result.HighlightedDescription + '"');
            Debug.Unindent;
        End If;
    End For;

Searching by Repository Objects Metadata

In the same way as on searching by dimension elements, determine the following minimum settings to search for objects by their indexed metadata in search context: in the Locale property determine the language used for search and in the Text property determine search text. If required, it is possible to specify additional search parameters, for details see description of the ISolrMbObjectsSearchContext interface properties.

Minimum search settings:

Var
    //...
    SearchSchema: ISolrSearchEngineSchema;
    SearchExecutor: ISearchExecutor;
    MbObjContext: ISolrMbObjectsSearchContext;
    //...
Begin
    //...
    //To search by indexed metadata
    SearchExecutor := SearchSchema.SearchExecutor(SearchEngineTargetType.MbObject);
    MbObjContext := SearchExecutor.CreateContext As ISolrMbObjectsSearchContext;
    MbObjContext.Locale := LocaleCodeID.Russian;
    MbObjContext.Text := "Cube";
    //...

Search with filtering by field values:

Var
    //...
    SearchSchema: ISolrSearchEngineSchema;
    SearchExecutor: ISearchExecutor;
    MbObjContext: ISolrMbObjectsSearchContext;
    FilterCondition: ISolrFilterCondition;
    ConditionValues: ISolrFilterFieldValues;
    Values: Array Of Variant;
    //...
Begin
    //...
    //To search by indexed metadata
    SearchExecutor := SearchSchema.SearchExecutor(SearchEngineTargetType.MbObject);
    MbObjContext := SearchExecutor.CreateContext As ISolrMbObjectsSearchContext;
    MbObjContext.Locale := LocaleCodeID.Russian;
    MbObjContext.NeedDocumentContent := True;
    MbObjContext.Text := "Document";
    //Filtering parameters
    FilterCondition := MbObjContext.FilterContext.FilterCondition;
    FilterCondition.SolrField := SearchSchema.Fields(SearchEngineTargetType.SourceData).FindById("content");
    FilterCondition.Operator_ := LogicalOperator.And_;
    ConditionValues := FilterCondition.Values;
    ConditionValues.Operator_ := LogicalOperator.Or_;
    Values := New Variant[3];
    Values[0] := "Russia";
    Values[1] := "USA";
    Values[1] := "Canada";
    //...

After determining all search parameters, send the configured context to the ISearchExecutor.Search method. On executing the method the query will be created and sent to Apache Solr search platform, the search will be executed, and the answer with search results will be obtained. The result is the collection of elements where each element contains information about repository object metadata. The output collection can be cast to the ISolrSearchResults interface, and each collection element can be cast to the ISolrSearchResult or ISearchMbObjectResult interfaces.

Var
    //...
    Schema: ISolrSearchEngineSchema;
    SearchExecutor: ISearchExecutor;
    MbObjContext: ISolrMbObjectsSearchContext;
    Results: ISolrSearchResults;
    Result: ISearchMbObjectResult;
    i: Integer;
    //...
Begin
    //...
    SearchExecutor := Schema.SearchExecutor(SearchEngineTargetType.MbObject);
    //...
    //Set up search parameters
    //...
    //Search and get results
    Results := SearchExecutor.Search(MbObjContext) As ISolrSearchResults;
    //View search results
        For i := 0 To Results.Count - 1 Do
        Result := Results.Item(i) As ISearchMbObjectResult;
        Debug.WriteLine(Result.ClassName + '(' + Result.ClassId.ToString + ')');
        Debug.Indent;
        Debug.WriteLine("Object: " + Result.Name + '(' + Result.Id + ')');
        Debug.WriteLine("Data and time of the last change: " + Result.Timestamp);
        Debug.WriteLine("Information with highlighting of search value:");
        If Result.HighlightedName <> "" Then
            Debug.WriteLine("Name: " + Result.HighlightedName);
        End If;
        If Result.HighlightedId <> "" Then
            Debug.WriteLine("Identifier: " + Result.HighlightedId);
        End If;
        If Result.HighlightedDescription <> "" Then
            Debug.WriteLine("Description: " + Result.HighlightedDescription);
        End If;
        If Result.Content <> "" Then
            Debug.WriteLine("Document content: " + Result.Content);
            If Result.HighlightedContent <> "" Then
                Debug.WriteLine("Content with highlighting of search value: " + Result.HighlightedContent);
            End If;
        End If;
        Debug.Unindent;
    End For;

See also:

Introduction