public async Task TestApiKey() { var addOne = await _index1.SaveObjectAsync(new SecuredApiKeyStub { ObjectID = "one" }); var addTwo = await _index2.SaveObjectAsync(new SecuredApiKeyStub { ObjectID = "one" }); addOne.Wait(); addTwo.Wait(); SecuredApiKeyRestriction restriction = new SecuredApiKeyRestriction { ValidUntil = DateTime.UtcNow.AddMinutes(10).ToUnixTimeSeconds(), RestrictIndices = new List <string> { _index1Name } }; string key = BaseTest.SearchClient.GenerateSecuredApiKeys(TestHelper.SearchKey1, restriction); SearchClient clientWithRestriciton = new SearchClient(TestHelper.ApplicationId1, key); SearchIndex index1WithoutRestriction = clientWithRestriciton.InitIndex(_index1Name); SearchIndex index2WithRestriction = clientWithRestriciton.InitIndex(_index2Name); await index1WithoutRestriction.SearchAsync <SecuredApiKeyStub>(new Query()); AlgoliaApiException ex = Assert.ThrowsAsync <AlgoliaApiException>(() => index2WithRestriction.SearchAsync <SecuredApiKeyStub>(new Query())); Assert.That(ex.Message.Contains("Index not allowed with this API key")); Assert.That(ex.HttpErrorCode == 403); }
public async Task AddEntity <T>(T entity, string indexName) where T : class { SearchIndex index = _client.InitIndex(indexName); await index.SaveObjectAsync(entity); ListProducts(); }
public async Task <CreateIndexResponseDto> CreateIndexAsync <T>(CreateIndexRequestDto <T> dto) where T : class, new() { try { SearchIndex index = client.InitIndex(dto.IndexName); await index.SaveObjectsAsync(JArray.FromObject(dto.Data), autoGenerateObjectId : false); return(new CreateIndexResponseDto("Index created successfully", true)); } catch (Exception e) { return(new CreateIndexResponseDto(e.Message, false)); } }
public List <Item> Search(List <string> text) { // perform 3 queries in a single API call: // - 1st query targets index `categories` // - 2nd and 3rd queries target index `products` var indexQueries = new List <MultipleQueries>(); foreach (var item in text) { indexQueries.Add(new MultipleQueries() { IndexName = "Items", Params = new Query(item) }); } MultipleQueriesRequest request = new MultipleQueriesRequest { Requests = indexQueries }; SearchClient client = new SearchClient("BIW6EL1FTD", "8ae9274c008b76fdd8046bd43447044b"); SearchIndex index = client.InitIndex("Items"); var res = client.MultipleQueries <Item>(request); List <Item> results = new List <Item>(); foreach (var item in res.Results) { results = results.Union(item.Hits).ToList(); } ; return(results.Distinct(new ItemComparer()).ToList()); }
public IActionResult Put(string id, [FromBody] object content) { SqlClient sqlClient = new SqlClient(); SearchClient searchClient = new SearchClient( Environment.GetEnvironmentVariable(ServiceConfiguration.AlgoliaAppId), Environment.GetEnvironmentVariable(ServiceConfiguration.AdminKey)); SearchIndex index = searchClient.InitIndex(ServiceConfiguration.SearchIndex); if (Get(id) == null) { return(NotFound()); } CompetitiveEvent updatedEvent = JsonConvert.DeserializeObject <CompetitiveEvent>(content.ToString()); CompetitiveEvent currentEvent = sqlClient.GetCompetitiveEvent( SqlCommands.GetCompetitiveEvent, id); currentEvent = SqlOperations.UpdateCompetitiveEvent(currentEvent, updatedEvent); sqlClient.CreateOrInsert( SqlCommands.UpdateCompetitiveEvent, CompetitiveEvent.ToDictionary(currentEvent)); index.PartialUpdateObject( currentEvent, createIfNotExists: false); return(Ok()); }
public async Task FunctionHandler(DynamoDBEvent dynamoEvent, ILambdaContext context) { if (dynamoEvent?.Records == null) { context.Logger.LogLine("No dynamo stream record to process"); return; } context.Logger.LogLine($"Beginning to process {dynamoEvent?.Records.Count} records..."); var paymentBatch = dynamoEvent.Records.Where(x => x.EventName.Value != "REMOVE") /* filter for different entities && x.Dynamodb.Keys["Type"].S == "Payment"*/ .Select(record => { var doc = Document.FromAttributeMap(record.Dynamodb.NewImage); var jsonDoc = doc.ToJson(); var chargeRequested = JsonConvert.DeserializeObject <AlgoliaChargeRequested>(jsonDoc); context.Logger.LogLine($"DynamoDB Record of type {record.EventName}:"); context.Logger.LogLine(SerializeStreamRecord(record.Dynamodb)); context.Logger.LogLine(jsonDoc); return(chargeRequested); }).ToList(); var client = new SearchClient(new SearchConfig("FDPS0J74WQ", "e4e69e7bc80a5cf2f1ba429c1d058553")); var index = client.InitIndex("test"); var response = await index.SaveObjectsAsync(paymentBatch); var count = response.Responses.Sum(r => r.ObjectIDs.Count()); context.Logger.LogLine($"Algolia added {count} records"); context.Logger.LogLine($"Stream processing complete. Added {paymentBatch.Count} records"); }
public IActionResult Post([FromBody] object content) { SqlClient sqlClient = new SqlClient(); SearchClient searchClient = new SearchClient( Environment.GetEnvironmentVariable(ServiceConfiguration.AlgoliaAppId), Environment.GetEnvironmentVariable(ServiceConfiguration.AdminKey)); SearchIndex index = searchClient.InitIndex(ServiceConfiguration.SearchIndex); CompetitiveEvent competitiveEvent = JsonConvert.DeserializeObject <CompetitiveEvent>(content.ToString()); competitiveEvent.CreatedTimestamp = competitiveEvent.ModifiedTimestamp = DateTime.Now; competitiveEvent.Id = competitiveEvent.ObjectID = Guid.NewGuid().ToString(); if (Get(competitiveEvent.Id) != null) { competitiveEvent.Id = competitiveEvent.ObjectID = Guid.NewGuid().ToString(); } competitiveEvent.Version = 1; sqlClient.CreateOrInsert( SqlCommands.InsertCompetitiveEvent, CompetitiveEvent.ToDictionary(competitiveEvent)); index.SaveObject( competitiveEvent, autoGenerateObjectId: false); return(Ok()); }
static async Task Main(string[] args) { InitKeys(); // Init the client SearchClient client = new SearchClient(_appKey, _apiKey); // Init index SearchIndex index = client.InitIndex("AlgoliaDotnetConsole"); // Push data from Json using (StreamReader re = File.OpenText("AlgoliaConsole/Datas/Actors.json")) using (JsonTextReader reader = new JsonTextReader(re)) { JArray batch = JArray.Load(reader); var ret = await index.SaveObjectsAsync(batch); ret.Wait(); } // Get data var actor = await index.GetObjectAsync <Actor>("551486310"); Console.WriteLine(actor.ToString()); // Search var search = await index.SearchAsync <Actor>(new Query("monica")); Console.WriteLine(search.Hits.ElementAt(0).ToString()); Environment.Exit(0); }
public SearchIndexResult Search(string term, int page = 1, ISearchConfig config = null) { if (!(config is IAlgoliaSearchConfig cfg)) { cfg = _algoliaSearchConfig; } try { var client = new SearchClient(cfg.ApplicationId, cfg.SearchApiKey); var index = client.InitIndex(cfg.IndexName); var results = index.Search <JObject>(new Query(term) { AttributesToHighlight = new string[0], AttributesToRetrieve = _algoliaSearchHelper.GetAllAttributeAliases(), AttributesToSnippet = new string[0], Page = page - 1 }); return(new SearchIndexResult { PageNumber = results.Page + 1, Results = results.Hits?.ConvertAll(_searchIndexEntryHelper.Convert).Where(e => e != null), TotalPages = results.NbPages, TotalResults = results.NbHits }); } catch (Exception ex) { _logger.Error <AlgoliaSearchApplianceService>(ex, "Could not retrieve search results from {application}:{index} for '{term}'", cfg.ApplicationId, cfg.IndexName, term); return(new SearchIndexResult { PageNumber = 1, Results = new ISearchIndexEntry[0], TotalPages = 1 }); } }
public List <ClubAlgolia> Algolia() { // Add the data to Algolia SearchClient client = new SearchClient("ZJ5YQA6729", "ef857c06f1ebf56ed75841fc6c2df18b"); SearchIndex index = client.InitIndex("ClubFoot"); List <ClubAlgolia> clubs = new List <ClubAlgolia>(); foreach (Club club in _context.Clubs.ToList()) { var clubNew = new ClubAlgolia() { ObjectID = club.IdClub.ToString(), Name = club.Name, Address = club.Address, Phone = club.Phone, Email = club.Email, OpeningTime = club.OpeningTime, ClosingTime = club.ClosingTime }; clubs.Add(clubNew); } index.ClearObjects(); // Fetch from DB or a Json file index.SaveObjects(clubs); return(clubs); }
/// <summary> /// Constructor /// </summary> /// <param name="appId">Application id provided by Algolia</param> /// <param name="apiKey">API Key provided by Algolia</param> /// <param name="indexName">Name of index used for queries</param> /// <param name="loggerFactory">Logger factory</param> /// <param name="order">The order this plugin should process incoming messages</param> public AlgoliaPlugin( string?appId, string?apiKey, string?indexName, ILoggerFactory loggerFactory, string searchCommand = "search", string searchCommandHelp = "search or end with ?", string searchDescriptionHelp = "Search the docs and return top 3 results", int order = 0) { _ = appId ?? throw new ArgumentNullException(nameof(appId)); _ = apiKey ?? throw new ArgumentNullException(nameof(apiKey)); _ = indexName ?? throw new ArgumentNullException(nameof(apiKey)); _searchCommand = searchCommand; _isDefaultSearch = searchCommand == "search" ? true : false; _searchCommandHelp = searchCommandHelp; _searchDescriptionHelp = searchDescriptionHelp; _logger = loggerFactory.CreateLogger <AlgoliaPlugin>(); _algoliaSearchClient = new SearchClient(appId, apiKey); _algoliaIndex = _algoliaSearchClient.InitIndex(indexName); _orderOfProcessingMessages = order; }
public SearchCountResult GetTotalRecords(ISearchConfig config = null) { if (!(config is IAlgoliaSearchConfig cfg)) { cfg = _algoliaSearchConfig; } try { var client = new SearchClient(cfg.ApplicationId, cfg.UpdateApiKey); var index = client.InitIndex(cfg.IndexName); var results = index.BrowseFrom <JObject>(new BrowseIndexQuery() { HitsPerPage = 1 }); return(new SearchCountResult { Success = true, TotalRecords = results.NbHits }); } catch (Exception ex) { return(new SearchCountResult { Error = ex.Message }); } }
public async Task <ActionResult <string> > Refresh() { var h4GJobs = await GetAllJobs(); var client = new SearchClient( Environment.GetEnvironmentVariable("ALGOLIA_APP_ID"), Environment.GetEnvironmentVariable("ALGOLIA_ADMIN_KEY") ); var index = client.InitIndex("jobs"); try { var algoliaJobs = _mapper.Map <List <AlgoliaJob> >(h4GJobs); index.ReplaceAllObjects(algoliaJobs); } catch (Exception ex) { Console.WriteLine(ex.Message); } // var alljobs = index.ReplaceAllObjects(); return("success"); }
public List <Item> RandomItems() { SearchClient client = new SearchClient("BIW6EL1FTD", "a5af55b1831c11747c108cc179f2d790"); SearchIndex index = client.InitIndex("Items"); IndexIterator <Item> indexIterator = index.Browse <Item>(new BrowseIndexQuery { }); var hits = new List <Item>(); int max = 0; foreach (var hit in indexIterator) { if (hit.ImageURL != "null") { hits.Add(hit); } if (max > 100) { break; } else { max++; } } Random rnd = new Random(); return(hits.Where(t => t.ImageURL.ToLower() != "null").OrderBy(x => rnd.Next()).Take(20).ToList()); }
public async Task <IActionResult> NewJobsWebhook(WebhookRoot webhookRoot) { var client = new SearchClient( Environment.GetEnvironmentVariable("ALGOLIA_APP_ID"), Environment.GetEnvironmentVariable("ALGOLIA_ADMIN_KEY") ); var index = client.InitIndex("jobs"); if (webhookRoot.jobs == null) { return(Ok()); } var algoliaJobs = _mapper.Map <List <AlgoliaJob> >(webhookRoot.jobs.data); index.SaveObjects(algoliaJobs); //send notifications var playerIds = await _context.Devices.Select(d => d.PlayerID).ToListAsync(); var result = await SendNotificationsToPlayerIDs(playerIds, webhookRoot.jobs.data); return(Created(new Uri("https://hack4goodsgf.com"), algoliaJobs)); }
public List <Item> Search(string text) { SearchClient client = new SearchClient("BIW6EL1FTD", "8ae9274c008b76fdd8046bd43447044b"); SearchIndex index = client.InitIndex("Items"); var result = index.Search <Item>(new Query(text)); return(result.Hits); }
// Start is called before the first frame update void Start() { var appId = System.Environment.GetEnvironmentVariable("ALGOLIA_APPLICATION_ID"); var apiKey = System.Environment.GetEnvironmentVariable("ALGOLIA_SEARCH_KEY"); _searchClient = new SearchClient(appId, apiKey); _searchIndex = _searchClient.InitIndex("Planets"); _viewPort = GameObject.Find("Viewport"); }
private async Task UpdateSearchAppliance(CancellationToken token, ICollection <ISearchIndexEntry> entries, ICollection <UpdateItemReference> updatedItems, IAlgoliaSearchConfig config) { if (token.IsCancellationRequested) { return; } var client = new SearchClient(config.ApplicationId, config.UpdateApiKey); var index = client.InitIndex(config.IndexName); if (token.IsCancellationRequested) { return; } // Make sure the settings match what we expect var settings = _algoliaSearchHelper.GetIndexSettings(); if (settings != null) { await index.WaitTaskAsync((await index.SetSettingsAsync(settings)).TaskID); } if (token.IsCancellationRequested) { return; } // Delete any existing entries (or more importantly previously removed children) for the items being inserted var filterItems = updatedItems.Select(i => i.IncludeDescendents ? $"path:{i.ContentUdi}" : $"objectID:{i.ContentUdi}"); var filters = string.Join(" OR ", filterItems); if (!string.IsNullOrWhiteSpace(filters)) { await index.WaitTaskAsync((await index.DeleteByAsync(new Query() { Filters = filters })).TaskID); if (token.IsCancellationRequested) { return; } } if (entries.Count > 0) { var responses = (await index.SaveObjectsAsync(entries)).Responses; foreach (var response in responses) { await index.WaitTaskAsync(response.TaskID); } } }
private async void Search(object sender, EventArgs e) { Articles.Clear(); ArticlesView.ItemsSource = Articles; SearchIndex index = client.InitIndex("articles"); var result = await index.SearchAsync <Article>(new Query(text.Text)); Articles = new ObservableCollection <Article>(result.Hits); ArticlesView.ItemsSource = Articles; }
/// <summary> /// Constructor /// </summary> /// <param name="appId">Application id provided by Algolia</param> /// <param name="apiKey">API Key provided by Algolia</param> /// <param name="indexName">Name of index used for queries</param> /// <param name="loggerFactory">Logger factory</param> /// <param name="order">The order this plugin should process incoming messages</param> public AlgoliaPlugin( string?appId, string?apiKey, string?indexName, ILoggerFactory loggerFactory, int order = 0) { _ = appId ?? throw new ArgumentNullException(nameof(appId)); _ = apiKey ?? throw new ArgumentNullException(nameof(apiKey)); _ = indexName ?? throw new ArgumentNullException(nameof(apiKey)); _logger = loggerFactory.CreateLogger <AlgoliaPlugin>(); _algoliaSearchClient = new SearchClient(appId, apiKey); _algoliaIndex = _algoliaSearchClient.InitIndex(indexName); _orderOfProcessingMessages = order; }
public IHttpActionResult Postitem(item item) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.items.Add(item); db.SaveChanges(); SearchClient client = new SearchClient("7J56ZYTP02", "b2c0772d562cc1ce06d53a2f2a76f562"); SearchIndex index = client.InitIndex("Items"); index.SaveObject(item, autoGenerateObjectId: true); return(CreatedAtRoute("DefaultApi", new { id = item.Id }, item)); }
public async Task TestRetryStrategyEndToEnd() { // Create a index with a valid client var indexName = TestHelper.GetTestIndexName("test_retry_e2e"); var index = BaseTest.SearchClient.InitIndex(indexName); var res = await index.SaveObjectAsync(new { title = "title" }, autoGenerateObjectId : true); res.Wait(); // Create a client with a bad host to test that the retry worked as expected var hosts = new List <StatefulHost> { // Bad host, will fail with // System.Net.Http.HttpRequestException: // The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: new StatefulHost { Url = "expired.badssl.com", Up = true, LastUse = DateTime.UtcNow, Accept = CallType.Read | CallType.Write, }, new StatefulHost { Url = $"{TestHelper.ApplicationId1}-dsn.algolia.net", Up = true, LastUse = DateTime.UtcNow, Accept = CallType.Read | CallType.Write, } }; // Warning /!\ Only use search key here /!\ SearchConfig config = new SearchConfig(TestHelper.ApplicationId1, TestHelper.SearchKey1) { CustomHosts = hosts }; var client = new SearchClient(config); var idx = client.InitIndex(indexName); var search = await idx.SearchAsync <Object>(new Query("")); Assert.AreEqual(1, search.NbHits); }
public IActionResult Delete(string id) { SqlClient sqlClient = new SqlClient(); SearchClient searchClient = new SearchClient( Environment.GetEnvironmentVariable(ServiceConfiguration.AlgoliaAppId), Environment.GetEnvironmentVariable(ServiceConfiguration.AdminKey)); SearchIndex index = searchClient.InitIndex(ServiceConfiguration.SearchIndex); if (Get(id) == null) { return(NotFound()); } sqlClient.DeleteCompetitiveEvent( SqlCommands.DeleteCompetitiveEvent, id); index.DeleteObject( id); return(Ok()); }
public void TestRetryStrategyWithWrongUrl() { List <StatefulHost> wrongUrlHosts = new List <StatefulHost> { new StatefulHost { Url = "wrong", Up = false, }, }; SearchConfig configWithCustomHosts = new SearchConfig(TestHelper.ApplicationId1, TestHelper.AdminKey1) { CustomHosts = wrongUrlHosts }; SearchClient clientWithCustomConfig = new SearchClient(configWithCustomHosts); Assert.That(Assert.Throws <AlgoliaUnreachableHostException>(() => clientWithCustomConfig.InitIndex(_indexName).Search <string>(new Query(""))).Message, Is.Not.Empty); }
public void SavePlaceObject(AlgoliaPlacesExportModel places) { _index = _client.InitIndex((_settings.GetConfigSetting <string>(SettingKeys.Integration.Algolia.Index))); _index.SaveObject(places); }
public AlgoliaFunctions(AlgoliaSettings settings) { client = new SearchClient(settings.ApplicationId, settings.ApiKey); index = client.InitIndex(settings.IndexName); }
protected override async Task <IEnumerable <IDocument> > ExecuteConfigAsync(IDocument input, IExecutionContext context, IMetadata values) { string applicationId = values.GetString(ApplicationId) ?? throw new ExecutionException("Invalid application ID"); string apiKey = values.GetString(ApiKey) ?? throw new ExecutionException("Invalid search API key"); string indexName = values.GetString(IndexName) ?? throw new ExecutionException("Invalid search index name"); IReadOnlyList <string> indexKeys = values.GetList <string>(IndexKeys) ?? throw new ExecutionException("Invalid index keys"); SearchClient client = new SearchClient(applicationId, apiKey); SearchIndex index = client.InitIndex(indexName); // Get the entire index HashSet <JObject> existing = new HashSet <JObject>(new SearchIndexItemEqualityComparer()); BrowseIndexResponse <JObject> results; int page = 0; do { RequestOptions options = new RequestOptions { QueryParameters = new Dictionary <string, string> { { "page", page.ToString() } } }; results = await index.BrowseFromAsync <JObject>(new BrowseIndexQuery(), options, context.CancellationToken); existing.AddRange(results.Hits); page++; }while (page < results.NbPages); // Figure out what we need to add and remove List <JObject> adds = new List <JObject>(); foreach (IDocument document in context.Inputs) { // Create a JObject for the item JObject item = new JObject(); foreach (string indexKey in indexKeys) { if (document.ContainsKey(indexKey)) { string key = indexKey.Substring(0, 1).ToLower() + indexKey.Substring(1); string value = document.GetString(indexKey); item.Add(key, value); } } // Is it already in the index? if (!existing.Remove(item)) { // It wasn't matched, so this is a new one adds.Add(item); } } context.LogInformation($"Search index \"{indexName}\": deleting {existing.Count}, adding {adds.Count}"); // Update the search index if (existing.Count > 0) { await index.DeleteObjectsAsync(existing.Select(x => x.Property("objectID").Value.ToString()), ct : context.CancellationToken); } if (adds.Count > 0) { await index.SaveObjectsAsync( adds, ct : context.CancellationToken, autoGenerateObjectId : true); } return(context.Inputs); }