Exemple #1
0
        public void TestClientWithMock()
        {
            var client = new AlgoliaClient("test", "test", null, getEmptyHandler());

            Assert.Equal(JObject.Parse("{\"items\":[]}").ToString(), client.ListIndexes().ToString());
            Assert.Equal(JObject.Parse("{\"results\":[]}").ToString(), client.InitIndex("{indexName}").GetObjects(new string[] { "myID" }).ToString());
        }
        public void TestRetryStrategy_Build()
        {
            var applicationId = "test";
            var mockHttp      = new MockHttpMessageHandler();
            var hosts         = new string[] { applicationId + "-1.algolianet.com", applicationId + "-2.algolianet.com" };

            mockHttp.When(HttpMethod.Get, "https://" + hosts[0] + "/1/indexes/").Respond(HttpStatusCode.RequestTimeout);
            mockHttp.When(HttpMethod.Get, "https://" + hosts[0] + "/1/indexes/test/settings").Respond("application/json", "{\"fromFirstHost\":[]}");
            mockHttp.When(HttpMethod.Get, "https://" + hosts[0] + "/1/indexes/test/browse").Respond("application/json", "{\"fromFirstHost\":[]}");

            mockHttp.When(HttpMethod.Get, "https://" + hosts[1] + "/1/indexes/").Respond("application/json", "{\"fromSecondHost\":[]}");
            mockHttp.When(HttpMethod.Get, "https://" + hosts[1] + "/1/indexes/test/settings").Respond("application/json", "{\"fromSecondHost\":[]}");
            mockHttp.When(HttpMethod.Get, "https://" + hosts[1] + "/1/indexes/test/browse").Respond("application/json", "{\"fromSecondHost\":[]}");

            var client = new AlgoliaClient("test", "test", hosts, mockHttp);

            client._dsnInternalTimeout = 2;
            Assert.AreEqual(JObject.Parse("{\"fromSecondHost\":[]}").ToString(), client.ListIndexes().ToString());

            //first host back up again but no retry because lastModified < _dsnInternalTimeout, stick with second host
            Assert.AreEqual(JObject.Parse("{\"fromSecondHost\":[]}").ToString(), client.InitIndex("test").GetSettings().ToString());
            Thread.Sleep(10000);
            //lastModified > _dsnInternalTimeout, retry on first host
            Assert.AreEqual(JObject.Parse("{\"fromFirstHost\":[]}").ToString(), client.InitIndex("test").Browse().ToString());
        }
        public void TestDeleteIndex()
        {
            clearTest();
            var task = _index.AddObject(JObject.Parse(@"{""firstname"":""Jimmie"", ""lastname"":""Barninger""}"));

            _index.WaitTask(task["taskID"].ToString());
            var res = _index.Search(new Query(""));

            Assert.AreEqual(1, res["nbHits"].ToObject <int>());
            Assert.AreEqual("Jimmie", res["hits"][0]["firstname"].ToString());
            res = _client.ListIndexes();
            Assert.IsTrue(Include((JArray)res["items"], "name", safe_name("àlgol?à-csharp")));
            task = _client.DeleteIndex(safe_name("àlgol?à-csharp"));
            _index.WaitTask(task["taskID"].ToObject <String>());
            res = _client.ListIndexes();
            Assert.IsFalse(Include((JArray)res["items"], "name", safe_name("àlgol?à-csharp")));
        }
Exemple #4
0
        public async void BadClientCreation()
        {
            string[] _hosts = new string[] { "localhost.algolia.com:8080", "" };
            try
            {
                new AlgoliaClient("", _testApiKey);
                Assert.Fail();
            }
            catch (Exception)
            { }
            try
            {
                new AlgoliaClient(_testApplicationID, "");
                Assert.Fail();
            }
            catch (Exception)
            { }
            try
            {
                new AlgoliaClient(_testApplicationID, "", _hosts);
                Assert.Fail();
            }
            catch (Exception)
            { }
            try
            {
                new AlgoliaClient("", _testApiKey, _hosts);
                Assert.Fail();
            }
            catch (Exception)
            { }
            try
            {
                var badClient = new AlgoliaClient(_testApplicationID, _testApiKey, null);
                Assert.Fail();
            }
            catch (Exception)
            { }
            try
            {
                var badClient = new AlgoliaClient(_testApplicationID, _testApiKey, _hosts);
                await badClient.ListIndexes();

                Assert.Fail();
            }
            catch (Exception)
            { }
        }
        public void TestDnsTimeout()
        {
            var hosts = new List <string> {
                _testApplicationID + "-dsn.algolia.biz",
                _testApplicationID + "-dsn.algolia.net",
                _testApplicationID + "-1.algolianet.com",
                _testApplicationID + "-2.algolianet.com",
                _testApplicationID + "-3.algolianet.com"
            };

            var _client = new AlgoliaClient(_testApplicationID, _testApiKey, hosts);

            _client.setTimeout(0.5, 0.5);
            var startTime = DateTime.Now;
            var index     = _client.ListIndexes();

            Assert.IsTrue(startTime.AddSeconds(0.5) < DateTime.Now);
        }
Exemple #6
0
        public void Process(PipelineArgs args)
        {
            Log.Info("InitIndexes", this);
            var client = new AlgoliaClient(Settings.ApiApplicationId, Settings.ApiAdminKey);

            var indexes = client.ListIndexes().GetValue("items").ToObject <List <IndexInfo> >();

            indexes = indexes.Where(i => i.Name.StartsWith(Settings.PageIndexesPrefix)).ToList();

            Log.Info("Number of indexes:" + indexes.Count, this);

            foreach (IndexInfo indexInfo in indexes)
            {
                var index = client.InitIndex(indexInfo.Name);

                Log.Info("Index:" + indexInfo.Name, this);

                var settings = index.GetSettings().ToObject <IndexSettings>();

                if (settings.AttributesToIndex == null)
                {
                    // settings.AttributesToIndex = new List<string> { "title", "content", "name" };
                    settings.AttributesToIndex = Settings.IndexingSettings.IncludedFields
                                                 .Where(f => f.Indexed)
                                                 .Select(f => f.FieldName)
                                                 .ToList();

                    settings.AttributesForFaceting = Settings.IndexingSettings.IncludedFields
                                                     .Where(f => f.FieldType == FieldType.Facet)
                                                     .Select(f => f.FieldName)
                                                     .ToList();
                    var idsFields = settings.AttributesForFaceting.Select(a => a + "_ids").ToList();
                    settings.AttributesForFaceting.AddRange(idsFields);

                    index.SetSettings(JObject.FromObject(settings), true);
                }
            }
        }
        public AlgoliaIndexInfo GetIndexInfo()
        {
            var response = _client.ListIndexes();

            return(AlgoliaIndexInfo.LoadFromJson(response, _indexName));
        }