Enterprise Search – Using Search API programmatically
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, lt_partner TYPE zesh_search_account_res, lr_cx TYPE REF TO cx_esh_if_engine.
TYPES: BEGIN OF ty_final,
DATA: lt_final TYPE ty_final. FIELD-SYMBOLS: <fs_result> TYPE ty_final.
Getting the connection details for Accounts SELECT SINGLE * FROM esh_adm_sc_main INTO ls_esh_admin IF sy-subrc EQ 0. Instantiating the Class instance for Search CREATE OBJECT lr_req. Add Search scope to search for Accounts lr_req->add_search_scope ( EXPORTING iv_connector_id = ls_esh_admin-sc_id Initiating the search Getting number of responses lv_resp_no = lr_resp->get_number_of_responses( ). WHILE lr_resp->has_next_response( ) = abap_true. TRY. ENDTRY. lr_connect->get_scope( IMPORTING ev_connector_id = <fs_result>-connector_id TRY. ENDWHILE. DESCRIBE TABLE <ft_data> LINES <fs_result>-hits. Hits = <fs_result>-all_hits. ASSIGN COMPONENT ‘BP_NUMBER’ OF STRUCTURE <fs_wa> TO <fs_temp>. |
Thanks a lot. Working fine in our system with minor adjustments!