/// <summary> /// AddOrUpdate Document /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="indexName"></param> /// <param name="model"></param> /// <returns></returns> public virtual async Task AddOrUpdateAsync <T, TKey>(string indexName, T model) where T : class { var exits = _esClient.DocumentExists(DocumentPath <T> .Id(new Id(model)), dd => dd.Index(indexName)); if (exits.Exists) { var result = await _esClient.UpdateAsync(DocumentPath <T> .Id(new Id(model)), ss => ss.Index(indexName).Doc(model).RetryOnConflict(3)); if (result.ServerError == null) { return; } throw new ElasticSearchException($"Update Document failed at index{indexName} :" + result.ServerError.Error.Reason); } else { var result = await _esClient.IndexAsync <T>(model, ss => ss.Index(indexName)); if (result.ServerError == null) { return; } throw new ElasticSearchException($"Insert Document failed at index {indexName} :" + result.ServerError.Error.Reason); } }
public bool CheckOrSave(string endpoint, Int32 bucket, long position) { //Logger.Debug("Endpoint {0} attempting to claim bucket {1} at position {2}", endpoint, bucket, position); if (!_client.DocumentExists <Competer>($"{endpoint}:{bucket}").Exists) { _client.Index(new Competer { Id = $"{endpoint}:{bucket}", Discriminator = Discriminator, Endpoint = endpoint, Bucket = bucket, Heartbeat = DateTime.UtcNow, Position = position }, x => x.Consistency(Elasticsearch.Net.Consistency.All)); Thread.Sleep(new Random().Next(100, 2000)); var check = _client.Get <Competer>($"{endpoint}:{bucket}"); if (check.Source?.Discriminator == Discriminator) { Logger.Info("Endpoint {0} successfully claimed bucket {1}", endpoint, bucket); } else { Logger.Info("Endpoint {0} failed to claim bucket {1}. Error: {2}", endpoint, bucket, "Discriminator mismatch"); } return(check.Source?.Discriminator == Discriminator); } return(false); }
/// <summary> /// 判断文件是否存在 /// </summary> public static bool DocExist_ <T>(this IElasticClient client, string indexName, string uid) where T : class, IElasticSearchIndex { var response = client.DocumentExists(client.ID <T>(indexName, uid)); response.ThrowIfException(); return(response.Exists); }
protected virtual void RemoveCore(IElasticClient client, TUmbracoEntity entity, string indexName = null) { var idValue = IdFor(entity); var documentPath = DocumentPath <TUmbracoDocument> .Id(idValue); if (client.DocumentExists(documentPath, d => d.Index(indexName)).Exists) { client.Delete(documentPath, d => d.Index(indexName)); } }
protected override IExistsResponse ExecuteCore(IElasticClient client, string index) { return(client.DocumentExists <T>(DocumentPath <T> .Id(_id), desc => BuildQueryCore(desc).Index(index))); }