|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +from azure.search.documents.indexes.models import ( |
| 5 | + SearchFieldDataType, |
| 6 | + SearchableField, |
| 7 | + SearchIndexer, |
| 8 | + FieldMapping, |
| 9 | + SimpleField, |
| 10 | + IndexingParameters, |
| 11 | + IndexingParametersConfiguration, |
| 12 | + BlobIndexerDataToExtract, |
| 13 | + IndexerExecutionEnvironment, |
| 14 | + BlobIndexerParsingMode, |
| 15 | + FieldMappingFunction, |
| 16 | +) |
| 17 | +from ai_search import AISearch |
| 18 | +from environment import ( |
| 19 | + IndexerType, |
| 20 | +) |
| 21 | +import os |
| 22 | +from text_2_sql_core.utils.database import DatabaseEngine |
| 23 | + |
| 24 | + |
| 25 | +class Text2SqlColumnValueStoreAISearch(AISearch): |
| 26 | + """This class is used to deploy the sql index.""" |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + suffix: str | None = None, |
| 31 | + rebuild: bool | None = False, |
| 32 | + ): |
| 33 | + """Initialize the Text2SqlAISearch class. This class implements the deployment of the sql index. |
| 34 | +
|
| 35 | + Args: |
| 36 | + suffix (str, optional): The suffix for the indexer. Defaults to None. If an suffix is provided, it is assumed to be a test indexer. |
| 37 | + rebuild (bool, optional): Whether to rebuild the index. Defaults to False. |
| 38 | + """ |
| 39 | + self.indexer_type = IndexerType.TEXT_2_SQL_COLUMN_VALUE_STORE |
| 40 | + super().__init__(suffix, rebuild) |
| 41 | + |
| 42 | + self.database_engine = DatabaseEngine[ |
| 43 | + os.environ["Text2Sql__DatabaseEngine"].upper() |
| 44 | + ] |
| 45 | + |
| 46 | + self.parsing_mode = BlobIndexerParsingMode.JSON_LINES |
| 47 | + |
| 48 | + @property |
| 49 | + def excluded_fields_for_database_engine(self): |
| 50 | + """A method to get the excluded fields for the database engine.""" |
| 51 | + |
| 52 | + all_engine_specific_fields = ["Warehouse", "Database", "Catalog"] |
| 53 | + if self.database_engine == DatabaseEngine.SNOWFLAKE: |
| 54 | + engine_specific_fields = ["Warehouse", "Database"] |
| 55 | + elif self.database_engine == DatabaseEngine.TSQL: |
| 56 | + engine_specific_fields = ["Database"] |
| 57 | + elif self.database_engine == DatabaseEngine.DATABRICKS: |
| 58 | + engine_specific_fields = ["Catalog"] |
| 59 | + |
| 60 | + return [ |
| 61 | + field |
| 62 | + for field in all_engine_specific_fields |
| 63 | + if field not in engine_specific_fields |
| 64 | + ] |
| 65 | + |
| 66 | + def get_index_fields(self) -> list[SearchableField]: |
| 67 | + """This function returns the index fields for sql index. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + list[SearchableField]: The index fields for sql index""" |
| 71 | + |
| 72 | + fields = [ |
| 73 | + SimpleField( |
| 74 | + name="Id", |
| 75 | + type=SearchFieldDataType.String, |
| 76 | + key=True, |
| 77 | + analyzer_name="keyword", |
| 78 | + ), |
| 79 | + SimpleField( |
| 80 | + name="Entity", |
| 81 | + type=SearchFieldDataType.String, |
| 82 | + ), |
| 83 | + SimpleField( |
| 84 | + name="Database", |
| 85 | + type=SearchFieldDataType.String, |
| 86 | + ), |
| 87 | + SimpleField( |
| 88 | + name="Warehouse", |
| 89 | + type=SearchFieldDataType.String, |
| 90 | + ), |
| 91 | + SimpleField( |
| 92 | + name="Catalog", |
| 93 | + type=SearchFieldDataType.String, |
| 94 | + ), |
| 95 | + SimpleField( |
| 96 | + name="Column", |
| 97 | + type=SearchFieldDataType.String, |
| 98 | + ), |
| 99 | + SearchableField( |
| 100 | + name="Value", |
| 101 | + type=SearchFieldDataType.String, |
| 102 | + hidden=False, |
| 103 | + ), |
| 104 | + SimpleField( |
| 105 | + name="Synonyms", type=SearchFieldDataType.String, collection=True |
| 106 | + ), |
| 107 | + SimpleField( |
| 108 | + name="DateLastModified", |
| 109 | + type=SearchFieldDataType.DateTimeOffset, |
| 110 | + filterable=True, |
| 111 | + ), |
| 112 | + ] |
| 113 | + |
| 114 | + # Remove fields that are not supported by the database engine |
| 115 | + fields = [ |
| 116 | + field |
| 117 | + for field in fields |
| 118 | + if field.name not in self.excluded_fields_for_database_engine |
| 119 | + ] |
| 120 | + |
| 121 | + return fields |
| 122 | + |
| 123 | + def get_skills(self) -> list: |
| 124 | + """Get the skillset for the indexer. |
| 125 | +
|
| 126 | + Returns: |
| 127 | + list: The skillsets used in the indexer""" |
| 128 | + |
| 129 | + skills = [] |
| 130 | + |
| 131 | + return skills |
| 132 | + |
| 133 | + def get_indexer(self) -> SearchIndexer: |
| 134 | + """This function returns the indexer for sql. |
| 135 | +
|
| 136 | + Returns: |
| 137 | + SearchIndexer: The indexer for sql""" |
| 138 | + |
| 139 | + # Only place on schedule if it is not a test deployment |
| 140 | + if self.test: |
| 141 | + schedule = None |
| 142 | + batch_size = 4 |
| 143 | + else: |
| 144 | + schedule = {"interval": "PT24H"} |
| 145 | + batch_size = 16 |
| 146 | + |
| 147 | + if self.environment.use_private_endpoint: |
| 148 | + execution_environment = IndexerExecutionEnvironment.PRIVATE |
| 149 | + else: |
| 150 | + execution_environment = IndexerExecutionEnvironment.STANDARD |
| 151 | + |
| 152 | + indexer_parameters = IndexingParameters( |
| 153 | + batch_size=batch_size, |
| 154 | + configuration=IndexingParametersConfiguration( |
| 155 | + data_to_extract=BlobIndexerDataToExtract.CONTENT_AND_METADATA, |
| 156 | + query_timeout=None, |
| 157 | + execution_environment=execution_environment, |
| 158 | + fail_on_unprocessable_document=False, |
| 159 | + fail_on_unsupported_content_type=False, |
| 160 | + index_storage_metadata_only_for_oversized_documents=True, |
| 161 | + indexed_file_name_extensions=".jsonl", |
| 162 | + parsing_mode=self.parsing_mode, |
| 163 | + ), |
| 164 | + max_failed_items=5, |
| 165 | + ) |
| 166 | + |
| 167 | + indexer = SearchIndexer( |
| 168 | + name=self.indexer_name, |
| 169 | + description="Indexer to column values", |
| 170 | + target_index_name=self.index_name, |
| 171 | + data_source_name=self.data_source_name, |
| 172 | + schedule=schedule, |
| 173 | + field_mappings=[ |
| 174 | + FieldMapping( |
| 175 | + source_field_name="metadata_storage_last_modified", |
| 176 | + target_field_name="DateLastModified", |
| 177 | + ) |
| 178 | + ], |
| 179 | + output_field_mappings=[ |
| 180 | + FieldMapping( |
| 181 | + source_field_name="/document/Id", |
| 182 | + target_field_name="Id", |
| 183 | + mapping_function=FieldMappingFunction( |
| 184 | + name="base64Encode", |
| 185 | + parameters={"useHttpServerUtilityUrlTokenEncode": False}, |
| 186 | + ), |
| 187 | + ), |
| 188 | + FieldMapping( |
| 189 | + source_field_name="/document/Entity", target_field_name="Entity" |
| 190 | + ), |
| 191 | + FieldMapping( |
| 192 | + source_field_name="/document/Database", |
| 193 | + target_field_name="Database", |
| 194 | + ), |
| 195 | + FieldMapping( |
| 196 | + source_field_name="/document/Warehouse", |
| 197 | + target_field_name="Warehouse", |
| 198 | + ), |
| 199 | + FieldMapping( |
| 200 | + source_field_name="/document/Column", |
| 201 | + target_field_name="Column", |
| 202 | + ), |
| 203 | + FieldMapping( |
| 204 | + source_field_name="/document/Value", |
| 205 | + target_field_name="Value", |
| 206 | + ), |
| 207 | + FieldMapping( |
| 208 | + source_field_name="/document/Synonyms", |
| 209 | + target_field_name="Synonyms", |
| 210 | + ), |
| 211 | + FieldMapping( |
| 212 | + source_field_name="/document/DateLastModified", |
| 213 | + target_field_name="DateLastModified", |
| 214 | + ), |
| 215 | + ], |
| 216 | + parameters=indexer_parameters, |
| 217 | + ) |
| 218 | + |
| 219 | + # Remove fields that are not supported by the database engine |
| 220 | + indexer.output_field_mappings = [ |
| 221 | + field_mapping |
| 222 | + for field_mapping in indexer.output_field_mappings |
| 223 | + if field_mapping.target_field_name |
| 224 | + not in self.excluded_fields_for_database_engine |
| 225 | + ] |
| 226 | + |
| 227 | + return indexer |
0 commit comments