Пример #1
0
        public void Initialize(string connectionStringName)
        {
            _searchServiceClient          = factoryWrapper.CreateObject <SearchServiceClient>("contentSearch/searchServiceClient", true);
            _searchServiceClient.Observer = factoryWrapper.CreateObject <ISearchServiceAvailabilityManager>("contentSearch/availabilityManager", true) as IHttpMessageObserver;
            if (ConfigurationManager.ConnectionStrings[connectionStringName] == null)
            {
                return;
            }
            string connectionString1 = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

            foreach (string connectionString2 in connectionString1.Split(new char[1] {
                '|'
            }))
            {
                try
                {
                    AzureSearchServiceClient assc = _searchServiceClient as AzureSearchServiceClient;
                    if (assc != null)
                    {
                        assc.Initialize(_indexName, connectionString2);
                    }
                    else
                    {
                        _searchServiceClient.Initialize(_indexName, connectionString2);
                    }

                    if (_searchServiceClient.IndexExists())
                    {
                        break;
                    }
                    _searchServiceClient.CreateIndex(new IndexDefinition(null, new IndexedField[4]
                    {
                        new IndexedField("key", "Edm.String", true, false, true, false, true, false),
                        new IndexedField("sitecorename", "Edm.String", false, false, true, false, true, false),
                        new IndexedField("indextype", "Edm.String", false, false, true, false, true, false),
                        new IndexedField("servicename", "Edm.String", false, false, true, false, true, false)
                    }));
                    break;
                }
                catch (Exception ex)
                {
                    SearchLog.Log.Error("Failed to initialize CloudSearchProviderIndexName", ex);
                }
            }
        }
Пример #2
0
 private static Index CreateIndex(Index index)
 {
     return(_serviceClient.CreateIndex(index).GetAwaiter().GetResult());
 }
Пример #3
0
        public async Task CreateIndex()
        {
            await using SearchResources resources = SearchResources.CreateWithNoIndexes(this);
            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);

            #region Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex
            Uri    endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            string key      = Environment.GetEnvironmentVariable("SEARCH_API_KEY");

            // Create a service client
            AzureKeyCredential  credential = new AzureKeyCredential(key);
            SearchServiceClient client     = new SearchServiceClient(endpoint, credential);
            /*@@*/ client = resources.GetServiceClient();

            // Create the index
            //@@SearchIndex index = new SearchIndex("hotels")
            /*@@*/ SearchIndex index = new SearchIndex(Recording.Random.GetName())
            {
                Fields =
                {
                    new SimpleField("hotelId",                    SearchFieldDataType.String)
                    {
                        IsKey = true,                             IsFilterable= true, IsSortable  = true
                    },
                    new SearchableField("hotelName")
                    {
                        IsFilterable = true,                      IsSortable  = true
                    },
                    new SearchableField("description")
                    {
                        Analyzer = LexicalAnalyzerName.EnLucene
                    },
                    new SearchableField("tags",                   collection: true)
                    {
                        IsFilterable = true,                      IsFacetable = true
                    },
                    new ComplexField("address")
                    {
                        Fields =
                        {
                            new SearchableField("streetAddress"),
                            new SearchableField("city")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            },
                            new SearchableField("stateProvince")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            },
                            new SearchableField("country")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            },
                            new SearchableField("postalCode")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            }
                        }
                    }
                },
                Suggesters =
                {
                    // Suggest query terms from both the hotelName and description fields.
                    new SearchSuggester("sg", "hotelName", "description")
                }
            };

            client.CreateIndex(index);
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex
        }