Objective: Search a text string programmatically using Enterprise Search API. Business scenario is to input text and search account and contacts in relation.

We would be exploring the standard classes to check the connection with ESH & get desired search results.

  • Connection details are obtained from table esh_adm_sc_main.
  • Class CL_ESH_IF_SEARCH_REQUEST is used for search request.
  • Class CL_ESH_IF_SEARCH_RESPONSES is used for search responses.
  • Class CL_ESH_IF_SEARCH_CONNECT for search response of connectors.


Class CL_ESH_IF_SEARCH_REQUEST is the prime API for using Enterprise Search. Important methods for the class are SET_SEARCH_TEXT, SET_SEARCH_SCOPE, SET_START_INDEX.


The Technical Object can be designed to accept a String(Text) as an input and the Number of hits(Hits), the values returned(Values) and the return message(Msg) as output.


CODE SNIPPET

DATA:    ls_esh_admin     TYPE                 esh_adm_sc_main,
              lr_req                  TYPE REF TO   cl_esh_if_search_request,
              lv_text                 TYPE                 string,
              lv_flag                 TYPE                 char01,
              lr_resp                TYPE REF TO   cl_esh_if_search_responses,
              lr_connect          TYPE REF TO   cl_esh_if_search_resp_connect,
              lv_resp_no        TYPE                 i,

              lt_partner            TYPE                zesh_search_account_res,

              lr_cx                    TYPE REF TO  cx_esh_if_engine.

TYPES: BEGIN OF     ty_final,
            connector_id         TYPE        esh_e_if_search_connector_id,
            request_type_id    TYPE        esh_e_if_request_type_id,
            data                      TYPE REF TO data,
            hits                       TYPE         i,
            all_hits                  TYPE         i,
            time                      TYPE         i,
            error                    TYPE         string,
              END OF        ty_final.

  DATA: lt_final TYPE ty_final.

   FIELD-SYMBOLS:  <fs_result>     TYPE          ty_final.
   FIELD-SYMBOLS:  <ft_data>        TYPE ANY TABLE.
   FIELD-SYMBOLS:  <fs_wa>          TYPE         any.
   FIELD-SYMBOLS: <fs_temp>.

  Getting the connection details for Accounts

  SELECT SINGLE * FROM esh_adm_sc_main INTO ls_esh_admin
     WHERE template_id EQ ‘BBPCRM~STANDARD~CRM_ACCOUNT~’ .

   IF sy-subrc EQ 0.

  Instantiating the Class instance for Search

     CREATE OBJECT lr_req.
     MOVE text TO lv_text.
     lr_req->set_search_text  ( iv_text = lv_text ).

  Add Search scope to search for Accounts

     lr_req->add_search_scope  ( EXPORTING    iv_connector_id     =  ls_esh_admin-sc_id
                                                                             iv_request_type_id = ‘CRM_ACCOUNT’
                                                    EXCEPTIONS  not_searchable     = 1
                                                    ).

   Initiating the search
     lr_resp = lr_req->search(  ).

    Getting number of responses

     lv_resp_no = lr_resp->get_number_of_responses(  ).

     WHILE lr_resp->has_next_response( ) = abap_true.
     APPEND INITIAL LINE TO lt_results ASSIGNING <fs_result>.

       TRY.
         lr_connect ?= lr_resp->get_next_response( ).

       ENDTRY.

       lr_connect->get_scope( IMPORTING ev_connector_id    = <fs_result>-connector_id
                                             ev_request_type_id = <fs_result>-request_type_id ).

       TRY.
           lr_connect->get_exception( ) .
       ENDTRY.
      Getting the results in Raw format
       lr_connect->get_results_data_raw( IMPORTING er_raw_data = <fs_result>-data ).
       CHECK <fs_result>-data IS BOUND.
       ASSIGN: <fs_result>-data->* TO <ft_data>.
       CHECK <ft_data> IS ASSIGNED.
       DESCRIBE TABLE <ft_data> LINES <fs_result>-hits.
       <fs_result>-all_hits = lr_connect->get_number_of_total_results( ).

     ENDWHILE.
     ENDIF.

   DESCRIBE TABLE <ft_data> LINES <fs_result>-hits.
   <fs_result>-all_hits = lr_connect->get_number_of_total_results( ).

   Hits = <fs_result>-all_hits.
   if return_hits ne 0.
   LOOP AT <ft_data> ASSIGNING <fs_wa>.

     ASSIGN COMPONENT ‘BP_NUMBER’ OF STRUCTURE <fs_wa> TO <fs_temp>.
     lt_partner-bp_partner = <fs_temp>.
     UNASSIGN <fs_temp>.
     ASSIGN COMPONENT ‘ACCOUNT_NAME’ OF STRUCTURE <fs_wa> TO <fs_temp>.
     lt_partner-account_name = <fs_temp>.
     UNASSIGN <fs_temp>.
     APPEND lt_partner TO Values.
   ENDLOOP.
   else.
     return_message = ‘No Results’.
     endif.


To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply