Voici une petite requête qui permet d’extraire le script de création des index fulltext d’une base de données. Il génère également l’instruction de création du catalogue (CREATE FULLTEXT CATALOG) auquel chaque index est lié. Donc si l’on a plusieurs index attachés au même catalogue, le script teste si le catalogue existe déjà .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | DECLARE @cr char(2) = CHAR(13) + CHAR(10) , @tab char(1) = CHAR(9) SELECT 'IF NOT EXISTS (SELECT * FROM sys.fulltext_catalogs WHERE name = ''' + FC.name + ''')' + @cr + 'CREATE FULLTEXT CATALOG [' + FC.name + ']' + CASE FC.is_default WHEN 1 THEN ' AS DEFAULT' ELSE '' END + CASE FC.is_accent_sensitivity_on WHEN 1 THEN ' WITH ACCENT_SENSITIVITY = ON' ELSE '' END + ';' + @cr + @cr + 'IF NOT EXISTS(SELECT * FROM sys.fulltext_indexes WHERE object_id = OBJECT_ID(''[' + S.name + '].[' + T.name + ']''))' + @cr + 'CREATE FULLTEXT INDEX ON [' + S.name + '].[' + T.name + ']' + @cr + '(' + @cr + @tab + REPLACE(LEFT(FIS.column_list, LEN(FIS.column_list) - 1), ',', @cr + @tab + ', ') + @cr + ')' + @cr + 'KEY INDEX [' + I.name + '] ON ([' + FC.name + '], FILEGROUP [' + FG.name + '])' + CASE WHEN LEN(FIO.fulltext_index_options) > 0 THEN 'WITH ' + RIGHT(FIO.fulltext_index_options, LEN(FIO.fulltext_index_options) - 2) ELSE '' END + ';' + @cr + @cr FROM sys.fulltext_catalogs AS FC INNER JOIN sys.fulltext_indexes AS FI ON FI.fulltext_catalog_id = FC.fulltext_catalog_id INNER JOIN sys.indexes AS I ON I.object_id = FI.object_id AND I.index_id = FI.unique_index_id INNER JOIN sys.tables AS T ON FI.object_id = T.object_id INNER JOIN sys.schemas AS S ON S.schema_id = T.schema_id INNER JOIN sys.filegroups AS FG ON FG.data_space_id = FI.data_space_id CROSS APPLY ( SELECT C.name + ' LANGUAGE ' + FL.name + ', ' FROM sys.fulltext_index_columns AS FIC INNER JOIN sys.columns AS C ON FIC.object_id = C.object_id AND FIC.column_id = C.column_id INNER JOIN sys.fulltext_languages AS FL ON FIC.language_id = FL.lcid WHERE FI.object_id = FIC.object_id FOR XML PATH('') ) AS FIS(column_list) CROSS APPLY ( SELECT CASE WHEN FI.change_tracking_state_desc 'AUTO' THEN ', CHANGE_TRACKING = ''' + FI.change_tracking_state_desc + '''' ELSE '' END + CASE WHEN FI.stoplist_id > 0 THEN ', STOPLIST = ' + FSL.name ELSE '' END + CASE WHEN FI.property_list_id IS NOT NULL THEN ', SEARCH PROPERTY LIST = ' + FRSPL.name ELSE '' END COLLATE database_default FROM sys.fulltext_indexes AS FIO LEFT JOIN sys.fulltext_stoplists AS FSL ON FI.stoplist_id = FSL.stoplist_id LEFT JOIN sys.registered_search_property_lists AS FRSPL ON FI.property_list_id = FRSPL.property_list_id WHERE FIO.fulltext_catalog_id = FI.fulltext_catalog_id AND FIO.object_id = FI.object_id FOR XML PATH ('') ) AS FIO(fulltext_index_options) |
Bonne indexation fulltext !