cancel
Showing results for 
Search instead for 
Did you mean: 

How to get all combinations of words in a string to build ABAP search engi

Former Member
0 Kudos

How to get all combinations of words in a string to build search

engine in SAP ABAP?

Suppose I have 3 words like "SAP LABS INDIA", How to get all

combinations like

SAP LABS INDIA

SAP INDIA LABS

LABS INDIA SAP

LABS SAP INDIA

INDIA LABS SAP

INDIA SAP LABS

It is not limited to 3 words only, it can be any no. of words.

I need to pass all the possible combination patterns to the database

for the effective search because user can give the words in any

sequence. I would like to know how to get all the combinations in ABAP

only.

Thanks and Regards,

Bhaskar Madugula

Accepted Solutions (0)

Answers (3)

Answers (3)

VXLozano
Active Contributor
0 Kudos

If you want to do that search using SQL, I suggest to create a range,

I|EQ|string_to_search1

I|EQ|string_to_search2

I|EQ|string_to_search2

if you want to seach ANY of that words. If you want to seach ALL THEM, use a simple SQL statement:

WHERE field LIKE 'str1'

AND field LIKE 'str2'

AND field LIKE 'str3'

Using the AND clauses, it will work too... you can change the range for OR statements in your SQL too...

Former Member
0 Kudos

Code given by Dirk Herzog works excellent.

Thank you Dirk Herzog.

Suggestion given by Vicenç Lozano also works, but if in the field domain Lowercase option is set it is not working.

I am using a field which have Lowercase option set in the domain, which means upper case and lower case are distinguished when you enter values with screen masks. How can we query case insensitive from the database, if in the domain lower case option is set we need to pass the exact Case, otherwise database will not recognise.

One bad option is to select all the entries into internal table and filter it, but this results in performance problems.

Message was edited by: Bhaskar Madugula

VXLozano
Active Contributor
0 Kudos

Not sure if ABAP-SQL will accept it, but when I needed something like you said about UCase, I normally do the search UCas'ing the field... something like this:

SELECT * FROM table WHERE UCASE( field ) LIKE 'SEARCH'

If you can do this thing in ABAP, you will have no care about your casing

Best luck,

Vicenç

Former Member
0 Kudos

Sorry we cannot do that in ABAP SQL.

Former Member
0 Kudos

Why can't you go for Native SQL ?

Regards

Venkat

Former Member
0 Kudos

Why can't you go for Native SQL ?

Regards

Venkat

Former Member
0 Kudos

If you are worried about the case of a description. why not do what is done for the Material master description, table MAKT. This holds two versions of the description, one is case sensitive, and the other is allways upper case. So you search against the upper case description but display the case sensitive description.

MattG.

Former Member
0 Kudos

Hi,

Only possibility seems to be Native SQL?

I am trying to fetch data from already existing database tables which do have have two versions of description and I cannot add one more version description for the table.

Regards,

Bhaskar Madugula

former_member183804
Active Contributor
0 Kudos

Hello Bhaskar,

I would still suggest to use a normalized form during storage and access. If e.g. the words are stored alphabetical order in upper case:

INDIA#LABS#SAP

searching should be no problem.

Best Regards

Klaus

VXLozano
Active Contributor
0 Kudos

If you are not doing it using masks... what are you using? I'm just curious, cannot think in a way to do it without a LIKE statement...

Former Member
0 Kudos

Hi,

SELECT * FROM table WHERE UCASE( field ) LIKE 'SEARCH'

For Native SQL - SQL Server doesn't support this, I am not sure for Oracle.

My database have case sensitive text, I will not be able to change the case or order as there are lot of entries already in the database table.

Thanks,

Bhaskar Madugula

former_member183804
Active Contributor
0 Kudos

Hello Bhaskar,

'SEARCH' has another problem, it cannot benefit from database indicies and thus would result in full table scans. Same applies more or less to case insensitive searches.

If the table is often changed and your search engine seldom used this might be no problem. If the situation is reversed (seldom changes, many search requests) creating a own table for Indexing the other one might be an option.

Kind Regards

Klaus

Former Member
0 Kudos

It should be UPPER( field ) LIKE 'SEARCH'

Regards

Venkat

ulrich_koch
Explorer
0 Kudos

Be careful if you store texts that has to be interpreted with different code pages.

Using anything like UPPER(), UCASE(), and so forth won't work in general: texts in the database are all stored using the same code page (often LATIN1) disregarding the language they actually belong to. For systems using several different code pages the UPPER() function would always do the conversion to upper case regarding just the one code page the database is working with.

Best regards

Ulrich

former_member183804
Active Contributor
0 Kudos

Hello Bhaskar,

I am no expert in search engines but I have the strong feeling building all combination within the db is too expensive in terms of db space (which would also influence performance).

An easy solution seems to sort the words before access. So one need only to store one combination. Db keys should also be limited in their broath.

Building hash keys and mainting conflict lists could avoid too fat db key columns.

Kind Regards

Klaus

Former Member
0 Kudos

This is a sample ABAP to show the solution. I'd check the number of words to not get in conflict with the maths. Maybe restrict it to 7 or so.

Best regards

Dirk

&----


*& Report Z_TEST_PERMU *

*& *

&----


*& *

*& *

&----


REPORT Z_TEST_PERMU .

types: y_s_text type rstxtsh,

y_ts_text type standard table of y_s_text with default key,

y_ts_permu type standard table of string with default key.

data: g_d_text type y_s_text,

g_t_text_table type y_ts_text,

g_d_permu type string,

g_t_permu_table type standard table of string with default key.

g_d_text = 'SAP'.

append g_d_text to g_t_text_table.

g_d_text = 'LABS'.

append g_d_text to g_t_text_table.

g_d_text = 'INDIA'.

append g_d_text to g_t_text_table.

perform permu using g_t_text_table changing g_t_permu_table.

loop at g_t_permu_table into g_d_permu.

write: / g_d_permu.

endloop.

&----


*& Form permu

&----


  • text

----


  • -->P_G_T_TEXT_TABLE text

  • <--P_G_T_PERMU_TABLE text

----


FORM permu USING u_T_TEXT_TABLE type y_ts_text

CHANGING c_T_PERMU_TABLE type y_ts_permu.

data: l_t_text_copy type y_ts_text,

l_d_text type y_s_text,

l_t_permu_copy type y_ts_permu,

l_d_permu type string,

l_d_tabix like sy-tabix.

refresh c_t_permu_table.

describe table u_t_text_table lines l_d_tabix.

if l_d_tabix <= 1.

loop at u_t_text_table into l_d_text.

l_d_permu = l_d_text.

append l_d_permu to c_t_permu_table.

endloop.

else.

do l_d_tabix times.

l_t_text_copy = u_t_text_table.

read table u_t_text_table into l_d_text index sy-index.

delete l_t_text_copy index sy-index.

perform permu using l_t_text_copy

changing l_t_permu_copy.

loop at l_t_permu_copy into l_d_permu.

concatenate l_d_text l_d_permu into l_d_permu.

append l_d_permu to c_t_permu_table.

endloop.

enddo.

endif.

ENDFORM. " permu