def test_create_index(self, api_key, endpoint, index_name, **kwargs): name = "hotels" fields = [ { "name": "hotelId", "type": "Edm.String", "key": True, "searchable": False }, { "name": "baseRate", "type": "Edm.Double" }] scoring_profile = ScoringProfile( name="MyProfile" ) scoring_profiles = [] scoring_profiles.append(scoring_profile) cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60) index = Index( name=name, fields=fields, scoring_profiles=scoring_profiles, cors_options=cors_options) client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)) result = client.create_index(index) assert result.name == "hotels" assert result.scoring_profiles[0].name == scoring_profile.name assert result.cors_options.allowed_origins == cors_options.allowed_origins assert result.cors_options.max_age_in_seconds == cors_options.max_age_in_seconds
def test_create_indexer(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexers_client() indexer = self._prepare_indexer(endpoint, api_key) result = client.create_indexer(indexer) assert result.name == "sample-indexer" assert result.target_index_name == "hotels" assert result.data_source_name == "sample-datasource"
async def _prepare_indexer(self, endpoint, api_key, name="sample-indexer", ds_name="sample-datasource", id_name="hotels"): con_str = self.settings.AZURE_STORAGE_CONNECTION_STRING self.scrubber.register_name_pair(con_str, 'connection_string') credentials = DataSourceCredentials(connection_string=con_str) container = DataContainer(name='searchcontainer') data_source = DataSource( name=ds_name, type="azureblob", credentials=credentials, container=container ) client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)) ds_client = client.get_datasources_client() ds = await ds_client.create_datasource(data_source) index_name = id_name fields = [ { "name": "hotelId", "type": "Edm.String", "key": True, "searchable": False }] index = Index(name=index_name, fields=fields) ind_client = client.get_indexes_client() ind = await ind_client.create_index(index) return Indexer(name=name, data_source_name=ds.name, target_index_name=ind.name)
def test_create_or_update_index(self, api_key, endpoint, index_name, **kwargs): name = "hotels" fields = [ SimpleField(name="hotelId", type=edm.String, key=True), SimpleField(name="baseRate", type=edm.Double) ] cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60) scoring_profiles = [] index = Index( name=name, fields=fields, scoring_profiles=scoring_profiles, cors_options=cors_options) client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexes_client() result = client.create_or_update_index(index_name=index.name, index=index) assert len(result.scoring_profiles) == 0 assert result.cors_options.allowed_origins == cors_options.allowed_origins assert result.cors_options.max_age_in_seconds == cors_options.max_age_in_seconds scoring_profile = ScoringProfile( name="MyProfile" ) scoring_profiles = [] scoring_profiles.append(scoring_profile) index = Index( name=name, fields=fields, scoring_profiles=scoring_profiles, cors_options=cors_options) result = client.create_or_update_index(index_name=index.name, index=index) assert result.scoring_profiles[0].name == scoring_profile.name assert result.cors_options.allowed_origins == cors_options.allowed_origins assert result.cors_options.max_age_in_seconds == cors_options.max_age_in_seconds
def test_delete_indexes(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)) client.delete_index(index_name) import time if self.is_live: time.sleep(TIME_TO_SLEEP) result = client.list_indexes() assert len(result) == 0
def test_delete_indexes(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexes_client() client.delete_index(index_name) import time if self.is_live: time.sleep(TIME_TO_SLEEP) result = client.list_indexes() with pytest.raises(StopIteration): next(result)
def test_list_indexer(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexers_client() indexer1 = self._prepare_indexer(endpoint, api_key) indexer2 = self._prepare_indexer(endpoint, api_key, name="another-indexer", ds_name="another-datasource", id_name="another-index") created1 = client.create_indexer(indexer1) created2 = client.create_indexer(indexer2) result = client.get_indexers() assert isinstance(result, list) assert set(x.name for x in result) == {"sample-indexer", "another-indexer"}
def test_list_indexes(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexes_client() result = client.list_indexes() first = next(result) assert first.name == index_name with pytest.raises(StopIteration): next(result)
def test_list_datasource(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_datasources_client() data_source1 = self._create_datasource() data_source2 = self._create_datasource(name="another-sample") created1 = client.create_datasource(data_source1) created2 = client.create_datasource(data_source2) result = client.get_datasources() assert isinstance(result, list) assert set(x.name for x in result) == {"sample-datasource", "another-sample"}
def test_get_skillsets(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_skillsets_client() s = EntityRecognitionSkill(inputs=[InputFieldMappingEntry(name="text", source="/document/content")], outputs=[OutputFieldMappingEntry(name="organizations", target_name="organizations")]) client.create_skillset(name='test-ss-1', skills=[s], description="desc1") client.create_skillset(name='test-ss-2', skills=[s], description="desc2") result = client.get_skillsets() assert isinstance(result, list) assert all(isinstance(x, Skillset) for x in result) assert set(x.name for x in result) == {"test-ss-1", "test-ss-2"}
def test_endpoint_https(self): credential = AzureKeyCredential(key="old_api_key") client = SearchServiceClient("endpoint", credential) assert client._endpoint.startswith('https') client = SearchServiceClient("https://endpoint", credential) assert client._endpoint.startswith('https') with pytest.raises(ValueError): client = SearchServiceClient("http://endpoint", credential) with pytest.raises(ValueError): client = SearchServiceClient(12345, credential)
def test_create_synonym_map(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)) result = client.create_synonym_map("test-syn-map", [ "USA, United States, United States of America", "Washington, Wash. => WA", ]) assert isinstance(result, dict) assert result["name"] == "test-syn-map" assert result["synonyms"] == [ "USA, United States, United States of America", "Washington, Wash. => WA", ] assert len(client.list_synonym_maps()) == 1
def simple_analyze_text(): # [START simple_analyze_text] from azure.core.credentials import AzureKeyCredential from azure.search.documents import SearchServiceClient, AnalyzeRequest service_client = SearchServiceClient(service_endpoint, AzureKeyCredential(key)) analyze_request = AnalyzeRequest(text="One's <two/>", analyzer="standard.lucene") result = service_client.analyze_text(index_name, analyze_request) print(result.as_dict())
def test_create_skillset(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_skillsets_client() s = EntityRecognitionSkill(inputs=[InputFieldMappingEntry(name="text", source="/document/content")], outputs=[OutputFieldMappingEntry(name="organizations", target_name="organizations")]) result = client.create_skillset(name='test-ss', skills=[s], description="desc") assert isinstance(result, Skillset) assert result.name == "test-ss" assert result.description == "desc" assert result.e_tag assert len(result.skills) == 1 assert isinstance(result.skills[0], EntityRecognitionSkill) assert len(client.get_skillsets()) == 1
async def test_reset_indexer(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexers_client() indexer = await self._prepare_indexer(endpoint, api_key) result = await client.create_indexer(indexer) assert len(await client.get_indexers()) == 1 await client.reset_indexer("sample-indexer") assert (await client.get_indexer_status("sample-indexer")).last_result.status in ('InProgress', 'reset')
async def test_delete_indexer(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexers_client() indexer = await self._prepare_indexer(endpoint, api_key) result = await client.create_indexer(indexer) assert len(await client.get_indexers()) == 1 await client.delete_indexer("sample-indexer") assert len(await client.get_indexers()) == 0
async def test_delete_datasource_async(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_datasources_client() data_source = self._create_datasource() result = await client.create_datasource(data_source) assert len(await client.get_datasources()) == 1 await client.delete_datasource("sample-datasource") assert len(await client.get_datasources()) == 0
async def test_run_indexer(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexers_client() indexer = await self._prepare_indexer(endpoint, api_key) result = await client.create_indexer(indexer) assert len(await client.get_indexers()) == 1 start = time.time() await client.run_indexer("sample-indexer") assert (await client.get_indexer_status("sample-indexer")).status == 'running'
async def test_delete_synonym_map_if_unchanged(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_synonym_maps_client() result = await client.create_synonym_map("test-syn-map", [ "USA, United States, United States of America", "Washington, Wash. => WA", ]) sm_result = build_synonym_map_from_dict(result) etag = sm_result.e_tag await client.create_or_update_synonym_map("test-syn-map", [ "Washington, Wash. => WA", ]) sm_result.e_tag = etag with pytest.raises(HttpResponseError): await client.delete_synonym_map(sm_result, match_condition=MatchConditions.IfNotModified) assert len(client.get_synonym_maps()) == 1
async def test_list_indexes(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexes_client() result = await client.list_indexes() first = await result.__anext__() assert first.name == index_name with pytest.raises(StopAsyncIteration): await result.__anext__()
async def test_delete_synonym_map(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_synonym_maps_client() result = await client.create_synonym_map("test-syn-map", [ "USA, United States, United States of America", "Washington, Wash. => WA", ]) assert len(await client.get_synonym_maps()) == 1 await client.delete_synonym_map("test-syn-map") assert len(await client.get_synonym_maps()) == 0
def test_create_or_update_datasource(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_datasources_client() data_source = self._create_datasource() created = client.create_datasource(data_source) assert len(client.get_datasources()) == 1 data_source.description = "updated" client.create_or_update_datasource(data_source) assert len(client.get_datasources()) == 1 result = client.get_datasource("sample-datasource") assert result.name == "sample-datasource" assert result.description == "updated"
def test_create_or_update_indexer(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexers_client() indexer = self._prepare_indexer(endpoint, api_key) created = client.create_indexer(indexer) assert len(client.get_indexers()) == 1 indexer.description = "updated" client.create_or_update_indexer(indexer) assert len(client.get_indexers()) == 1 result = client.get_indexer("sample-indexer") assert result.name == "sample-indexer" assert result.description == "updated"
def authentication_service_client_with_api_key_credential(): # [START create_search_service_client_with_key] from azure.core.credentials import AzureKeyCredential from azure.search.documents import SearchServiceClient service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT") key = os.getenv("AZURE_SEARCH_API_KEY") search_client = SearchServiceClient(service_endpoint, AzureKeyCredential(key))
async def test_delete_skillset(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_skillsets_client() s = EntityRecognitionSkill(inputs=[InputFieldMappingEntry(name="text", source="/document/content")], outputs=[OutputFieldMappingEntry(name="organizations", target_name="organizations")]) result = await client.create_skillset(name='test-ss', skills=[s], description="desc") assert len(await client.get_skillsets()) == 1 await client.delete_skillset("test-ss") if self.is_live: time.sleep(TIME_TO_SLEEP) assert len(await client.get_skillsets()) == 0
async def test_get_synonym_maps(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_synonym_maps_client() await client.create_synonym_map("test-syn-map-1", [ "USA, United States, United States of America", ]) await client.create_synonym_map("test-syn-map-2", [ "Washington, Wash. => WA", ]) result = await client.get_synonym_maps() assert isinstance(result, list) assert all(isinstance(x, dict) for x in result) assert set(x['name'] for x in result) == {"test-syn-map-1", "test-syn-map-2"}
def test_credential_roll(self): credential = AzureKeyCredential(key="old_api_key") client = SearchServiceClient("endpoint", credential) assert client._headers == { "api-key": "old_api_key", "Accept": "application/json;odata.metadata=minimal", } credential.update("new_api_key") assert client._headers == { "api-key": "new_api_key", "Accept": "application/json;odata.metadata=minimal", }
async def test_delete_indexer_if_unchanged(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_indexers_client() indexer = await self._prepare_indexer(endpoint, api_key) result = await client.create_indexer(indexer) etag = result.e_tag indexer.description = "updated" await client.create_or_update_indexer(indexer) indexer.e_tag = etag with pytest.raises(HttpResponseError): await client.delete_indexer(indexer, match_condition=MatchConditions.IfNotModified)
def test_create_or_update_skillset_if_unchanged(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_skillsets_client() s = EntityRecognitionSkill(inputs=[InputFieldMappingEntry(name="text", source="/document/content")], outputs=[OutputFieldMappingEntry(name="organizations", target_name="organizations")]) ss = client.create_or_update_skillset(name='test-ss', skills=[s], description="desc1") etag = ss.e_tag client.create_or_update_skillset(name='test-ss', skills=[s], description="desc2", skillset=ss) assert len(client.get_skillsets()) == 1 ss.e_tag = etag with pytest.raises(HttpResponseError): client.create_or_update_skillset(name='test-ss', skills=[s], skillset=ss, match_condition=MatchConditions.IfNotModified)
async def test_create_or_update_skillset_inplace(self, api_key, endpoint, index_name, **kwargs): client = SearchServiceClient(endpoint, AzureKeyCredential(api_key)).get_skillsets_client() s = EntityRecognitionSkill(inputs=[InputFieldMappingEntry(name="text", source="/document/content")], outputs=[OutputFieldMappingEntry(name="organizations", target_name="organizations")]) ss = await client.create_or_update_skillset(name='test-ss', skills=[s], description="desc1") await client.create_or_update_skillset(name='test-ss', skills=[s], description="desc2", skillset=ss) assert len(await client.get_skillsets()) == 1 result = await client.get_skillset("test-ss") assert isinstance(result, Skillset) assert result.name == "test-ss" assert result.description == "desc2"