def test_create_or_update_index(self, api_key, endpoint, index_name,
                                 **kwargs):
     name = "hotels"
     fields = [
         SimpleField(name="hotelId",
                     type=SearchFieldDataType.String,
                     key=True),
         SimpleField(name="baseRate", type=SearchFieldDataType.Double)
     ]
     cors_options = CorsOptions(allowed_origins=["*"],
                                max_age_in_seconds=60)
     scoring_profiles = []
     index = SearchIndex(name=name,
                         fields=fields,
                         scoring_profiles=scoring_profiles,
                         cors_options=cors_options)
     client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))
     result = client.create_or_update_index(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 = SearchIndex(name=name,
                         fields=fields,
                         scoring_profiles=scoring_profiles,
                         cors_options=cors_options)
     result = client.create_or_update_index(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
Exemplo n.º 2
0
    async def _test_create_or_update_index(self, client):
        name = "hotels-cou"
        fields = fields = [
            SimpleField(name="hotelId",
                        type=SearchFieldDataType.String,
                        key=True),
            SimpleField(name="baseRate", type=SearchFieldDataType.Double)
        ]

        cors_options = CorsOptions(allowed_origins=["*"],
                                   max_age_in_seconds=60)
        scoring_profiles = []
        index = SearchIndex(name=name,
                            fields=fields,
                            scoring_profiles=scoring_profiles,
                            cors_options=cors_options)
        result = await client.create_or_update_index(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 = SearchIndex(name=name,
                            fields=fields,
                            scoring_profiles=scoring_profiles,
                            cors_options=cors_options)
        result = await client.create_or_update_index(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_pack_search_index():
    pattern_analyzer = PatternAnalyzer(
            name="test_analyzer",
            flags=["CANON_EQ"]
        )
    analyzers = []
    analyzers.append(pattern_analyzer)
    pattern_tokenizer = PatternTokenizer(
        name="test_tokenizer",
        flags=["CANON_EQ"]
    )
    tokenizers = []
    tokenizers.append(pattern_tokenizer)
    index = SearchIndex(
        name="test",
        fields=None,
        analyzers=analyzers,
        tokenizers=tokenizers
    )
    result = pack_search_index(index)
    assert isinstance(result.analyzers[0], _PatternAnalyzer)
    assert isinstance(result.analyzers[0].flags, str)
    assert result.analyzers[0].flags == "CANON_EQ"
    assert isinstance(result.tokenizers[0], _PatternTokenizer)
    assert isinstance(result.tokenizers[0].flags, str)
    assert result.tokenizers[0].flags == "CANON_EQ"
def test_unpack_search_index_enum():
    pattern_analyzer = _PatternAnalyzer(
            name="test_analyzer",
            flags=RegexFlags.canon_eq
        )
    analyzers = []
    analyzers.append(pattern_analyzer)
    pattern_tokenizer = _PatternTokenizer(
        name="test_tokenizer",
        flags=RegexFlags.canon_eq
    )
    tokenizers = []
    tokenizers.append(pattern_tokenizer)
    index = SearchIndex(
        name="test",
        fields=None,
        analyzers=analyzers,
        tokenizers=tokenizers
    )
    result = unpack_search_index(index)
    assert isinstance(result.analyzers[0], PatternAnalyzer)
    assert isinstance(result.analyzers[0].flags, list)
    assert result.analyzers[0].flags[0] == "CANON_EQ"
    assert isinstance(result.tokenizers[0], PatternTokenizer)
    assert isinstance(result.tokenizers[0].flags, list)
    assert result.tokenizers[0].flags[0] == "CANON_EQ"
def test_multi_unpack_search_index():
    pattern_analyzer = _PatternAnalyzer(
            name="test_analyzer",
            flags="CANON_EQ|MULTILINE"
        )
    analyzers = []
    analyzers.append(pattern_analyzer)
    pattern_tokenizer = _PatternTokenizer(
        name="test_tokenizer",
        flags="CANON_EQ|MULTILINE"
    )
    tokenizers = []
    tokenizers.append(pattern_tokenizer)
    index = SearchIndex(
        name="test",
        fields=None,
        analyzers=analyzers,
        tokenizers=tokenizers
    )
    result = unpack_search_index(index)
    assert isinstance(result.analyzers[0], PatternAnalyzer)
    assert isinstance(result.analyzers[0].flags, list)
    assert result.analyzers[0].flags[0] == "CANON_EQ"
    assert result.analyzers[0].flags[1] == "MULTILINE"
    assert isinstance(result.tokenizers[0], PatternTokenizer)
    assert isinstance(result.tokenizers[0].flags, list)
    assert result.tokenizers[0].flags[0] == "CANON_EQ"
    assert result.tokenizers[0].flags[1] == "MULTILINE"
def create_index(name, endpoint, key):
    # Create a service client
    client = SearchIndexClient(endpoint, AzureKeyCredential(key))

    fields = [
        SimpleField(name='Id', type=SearchFieldDataType.String, key=True),
        SearchableField(name='FileName', type=SearchFieldDataType.String),
        SimpleField(name='FilePath', type=SearchFieldDataType.String),
        SearchableField(name='KeyPhrases',
                        collection=True,
                        type=SearchFieldDataType.String,
                        analyzer_name="en.lucene"),
        SearchableField(name='People',
                        collection=True,
                        type=SearchFieldDataType.String),
        SearchableField(name='Organisation',
                        collection=True,
                        type=SearchFieldDataType.String),
        SearchableField(name='Location',
                        collection=True,
                        type=SearchFieldDataType.String)
    ]

    cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60)
    scoring_profiles = []

    index = SearchIndex(name=name,
                        fields=fields,
                        scoring_profiles=scoring_profiles,
                        cors_options=cors_options)

    result = client.create_index(index)
Exemplo n.º 7
0
    async def _test_delete_indexes_if_unchanged(self, client):
        # First create an index
        name = "hotels-del-unchanged"
        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 = SearchIndex(name=name,
                            fields=fields,
                            scoring_profiles=scoring_profiles,
                            cors_options=cors_options)
        result = await client.create_index(index)
        etag = result.e_tag
        # get eTag and update
        index.scoring_profiles = []
        await client.create_or_update_index(index)

        index.e_tag = etag
        with pytest.raises(HttpResponseError):
            await client.delete_index(
                index, match_condition=MatchConditions.IfNotModified)
    def test_delete_indexes_if_unchanged(self, api_key, endpoint, index_name,
                                         **kwargs):
        client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))

        # First create an index
        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 = SearchIndex(name=name,
                            fields=fields,
                            scoring_profiles=scoring_profiles,
                            cors_options=cors_options)
        result = client.create_index(index)
        etag = result.e_tag
        # get e tag  and update
        index.scoring_profiles = []
        client.create_or_update_index(index)

        index.e_tag = etag
        with pytest.raises(HttpResponseError):
            client.delete_index(index,
                                match_condition=MatchConditions.IfNotModified)
Exemplo n.º 9
0
async def update_index():
    # [START update_index_async]
    name = "hotels"
    fields = [
        SimpleField(name="hotelId", type=SearchFieldDataType.String, key=True),
        SimpleField(name="baseRate", type=SearchFieldDataType.Double),
        SearchableField(name="description", type=SearchFieldDataType.String, collection=True),
        SearchableField(name="hotelName", type=SearchFieldDataType.String),
        ComplexField(name="address", fields=[
            SimpleField(name="streetAddress", type=SearchFieldDataType.String),
            SimpleField(name="city", type=SearchFieldDataType.String),
            SimpleField(name="state", type=SearchFieldDataType.String),
        ], collection=True)
    ]

    cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60)
    scoring_profile = ScoringProfile(
        name="MyProfile"
    )
    scoring_profiles = []
    scoring_profiles.append(scoring_profile)
    index = SearchIndex(
        name=name,
        fields=fields,
        scoring_profiles=scoring_profiles,
        cors_options=cors_options)

    result = await client.create_or_update_index(index=index)
async def create_indexer():
    # create an index
    index_name = "hotels"
    fields = [
        SimpleField(name="hotelId", type=SearchFieldDataType.String, key=True),
        SimpleField(name="baseRate", type=SearchFieldDataType.Double)
    ]
    index = SearchIndex(name=index_name, fields=fields)
    ind_client = SearchIndexerClient(service_endpoint, AzureKeyCredential(key))
    async with ind_client:
        await ind_client.create_index(index)

    # [START create_indexer_async]
    # create a datasource
    container = SearchIndexerDataContainer(name='searchcontainer')
    data_source_connection = SearchIndexerDataSourceConnection(
        name="indexer-datasource",
        type="azureblob",
        connection_string=connection_string,
        container=container)
    async with ind_client:
        data_source = await ind_client.create_data_source_connection(
            data_source_connection)

    # create an indexer
    indexer = SearchIndexer(name="async-sample-indexer",
                            data_source_name="async-indexer-datasource",
                            target_index_name="indexer-hotels")
    async with indexers_client:
        result = await indexers_client.create_indexer(indexer)
    print("Create new Indexer - async-sample-indexer")
Exemplo n.º 11
0
    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')
        container = SearchIndexerDataContainer(name='searchcontainer')
        data_source_connection = SearchIndexerDataSourceConnection(
            name=ds_name,
            type="azureblob",
            connection_string=con_str,
            container=container
        )
        ds_client = SearchIndexerClient(endpoint, AzureKeyCredential(api_key))
        ds = await ds_client.create_data_source_connection(data_source_connection)

        index_name = id_name
        fields = [
        {
          "name": "hotelId",
          "type": "Edm.String",
          "key": True,
          "searchable": False
        }]
        index = SearchIndex(name=index_name, fields=fields)
        ind_client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))
        ind = await ind_client.create_index(index)
        return SearchIndexer(name=name, data_source_name=ds.name, target_index_name=ind.name)
Exemplo n.º 12
0
def _create_index():
    name = "hotel-index"

    # Here we create an index with listed fields.
    fields = [
        SimpleField(name="hotelId",
                    type=SearchFieldDataType.String,
                    filterable=True,
                    sortable=True,
                    key=True),
        SearchableField(name="hotelName", type=SearchFieldDataType.String),
        SimpleField(name="description", type=SearchFieldDataType.String),
        SimpleField(name="descriptionFr", type=SearchFieldDataType.String),
        SimpleField(name="category", type=SearchFieldDataType.String),
        SimpleField(name="parkingIncluded",
                    type=SearchFieldDataType.Boolean,
                    filterable=True),
        SimpleField(name="smokingAllowed",
                    type=SearchFieldDataType.Boolean,
                    filterable=True),
        SimpleField(name="lastRenovationDate",
                    type=SearchFieldDataType.String),
        SimpleField(name="rating",
                    type=SearchFieldDataType.Int64,
                    sortable=True),
        SimpleField(name="location", type=SearchFieldDataType.GeographyPoint),
    ]
    cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60)

    # pass in the name, fields and cors options and create the index
    index = SearchIndex(name=name, fields=fields, cors_options=cors_options)
    index_client = SearchIndexClient(service_endpoint, AzureKeyCredential(key))
    result = index_client.create_index(index)
    return result
 def _create_index(self, client, index_name):
     fields = [
         SimpleField(name="hotelId",
                     type=SearchFieldDataType.String,
                     key=True),
         SimpleField(name="baseRate", type=SearchFieldDataType.Double),
     ]
     scoring_profile = ScoringProfile(name="MyProfile")
     scoring_profiles = []
     scoring_profiles.append(scoring_profile)
     cors_options = CorsOptions(allowed_origins=["*"],
                                max_age_in_seconds=60)
     index = SearchIndex(name=index_name,
                         fields=fields,
                         scoring_profiles=scoring_profiles,
                         cors_options=cors_options)
     result = client.create_index(index)
def test_listize_flags_for_index():
    pattern_analyzer = _PatternAnalyzer(name="test_analyzer", flags="CANON_EQ")
    analyzers = []
    analyzers.append(pattern_analyzer)
    pattern_tokenizer = _PatternTokenizer(name="test_tokenizer",
                                          flags="CANON_EQ")
    tokenizers = []
    tokenizers.append(pattern_tokenizer)
    index = SearchIndex(name="test",
                        fields=None,
                        analyzers=analyzers,
                        tokenizers=tokenizers)
    result = listize_flags_for_index(index)
    assert isinstance(result.analyzers[0], PatternAnalyzer)
    assert isinstance(result.analyzers[0].flags, list)
    assert result.analyzers[0].flags[0] == "CANON_EQ"
    assert isinstance(result.tokenizers[0], PatternTokenizer)
    assert isinstance(result.tokenizers[0].flags, list)
    assert result.tokenizers[0].flags[0] == "CANON_EQ"
def test_delistize_multi_flags_for_index():
    pattern_analyzer = PatternAnalyzer(name="test_analyzer",
                                       flags=["CANON_EQ", "MULTILINE"])
    analyzers = []
    analyzers.append(pattern_analyzer)
    pattern_tokenizer = PatternTokenizer(name="test_analyzer",
                                         flags=["CANON_EQ", "MULTILINE"])
    tokenizers = []
    tokenizers.append(pattern_tokenizer)
    index = SearchIndex(name="test",
                        fields=None,
                        analyzers=analyzers,
                        tokenizers=tokenizers)
    result = delistize_flags_for_index(index)
    assert isinstance(result.analyzers[0], _PatternAnalyzer)
    assert isinstance(result.analyzers[0].flags, str)
    assert result.analyzers[0].flags == "CANON_EQ|MULTILINE"
    assert isinstance(result.tokenizers[0], _PatternTokenizer)
    assert isinstance(result.tokenizers[0].flags, str)
    assert result.tokenizers[0].flags == "CANON_EQ|MULTILINE"
Exemplo n.º 16
0
    def _prepare_indexer(self, client, index_client, storage_cs, name,
                         container_name):
        data_source_connection = SearchIndexerDataSourceConnection(
            name=f"{name}-ds",
            type="azureblob",
            connection_string=storage_cs,
            container=SearchIndexerDataContainer(name=container_name))
        ds = client.create_data_source_connection(data_source_connection)

        fields = [{
            "name": "hotelId",
            "type": "Edm.String",
            "key": True,
            "searchable": False
        }]
        index = SearchIndex(name=f"{name}-hotels", fields=fields)
        ind = index_client.create_index(index)
        return SearchIndexer(name=name,
                             data_source_name=ds.name,
                             target_index_name=ind.name)
def test_multi_pack_search_index():
    pattern_analyzer = PatternAnalyzer(name="test_analyzer",
                                       flags=["CANON_EQ", "MULTILINE"])
    analyzers = []
    analyzers.append(pattern_analyzer)
    pattern_tokenizer = PatternTokenizer(name="test_analyzer",
                                         flags=["CANON_EQ", "MULTILINE"])
    tokenizers = []
    tokenizers.append(pattern_tokenizer)
    index = SearchIndex(name="test",
                        fields=None,
                        analyzers=analyzers,
                        tokenizers=tokenizers)
    result = index._to_generated()
    assert isinstance(result.analyzers[0], _PatternAnalyzer)
    assert isinstance(result.analyzers[0].flags, str)
    assert result.analyzers[0].flags == "CANON_EQ|MULTILINE"
    assert isinstance(result.tokenizers[0], _PatternTokenizer)
    assert isinstance(result.tokenizers[0].flags, str)
    assert result.tokenizers[0].flags == "CANON_EQ|MULTILINE"
def create_index():
    # [START create_index]
    name = "hotels"
    fields = [
        SimpleField(name="hotelId", type=SearchFieldDataType.String, key=True),
        SimpleField(name="baseRate", type=SearchFieldDataType.Double),
        SearchableField(name="description", type=SearchFieldDataType.String, collection=True),
        ComplexField(name="address", fields=[
            SimpleField(name="streetAddress", type=SearchFieldDataType.String),
            SimpleField(name="city", type=SearchFieldDataType.String),
        ], collection=True)
    ]
    cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60)
    scoring_profiles = []
    index = SearchIndex(
        name=name,
        fields=fields,
        scoring_profiles=scoring_profiles,
        cors_options=cors_options)

    result = client.create_index(index)
def create_schema_from_json_and_upload(schema,
                                       index_name,
                                       admin_client,
                                       url=False):

    cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60)
    scoring_profiles = []
    schema_data = get_schema_data(schema, url)

    index = SearchIndex(name=index_name,
                        fields=schema_data['fields'],
                        scoring_profiles=scoring_profiles,
                        suggesters=schema_data['suggesters'],
                        cors_options=cors_options)

    try:
        upload_schema = admin_client.create_index(index)
        if upload_schema:
            print(f'Schema uploaded; Index created for {index_name}.')
        else:
            exit(0)
    except:
        print("Unexpected error:", sys.exc_info()[0])