Exemplo n.º 1
0
        public async Task DeleteAllAsync()
        {
            await _elasticClient.DeleteByQueryAsync <GlobalSearchModel>(del => del
                                                                        .Index(indexGlobalSearch).Query(q => q.QueryString(qs => qs.Query("*"))));

            await _elasticClient.DeleteByQueryAsync <PhotoArchive>(del => del
                                                                   .Index(indexPhotoArchive).Query(q => q.QueryString(qs => qs.Query("*"))));

            await _elasticClient.DeleteByQueryAsync <PageNews>(del => del
                                                               .Index(indexPageNews).Query(q => q.QueryString(qs => qs.Query("*"))));
        }
Exemplo n.º 2
0
        public async Task DeleteOrphanedEventsByStackAsync(JobContext context)
        {
            // get approximate number of unique stack ids
            var stackCardinality = await _elasticClient.SearchAsync <PersistentEvent>(s => s.Aggregations(a => a
                                                                                                          .Cardinality("cardinality_stack_id", c => c.Field(f => f.StackId).PrecisionThreshold(40000))));

            var uniqueStackIdCount = stackCardinality.Aggregations.Cardinality("cardinality_stack_id")?.Value;

            if (!uniqueStackIdCount.HasValue || uniqueStackIdCount.Value <= 0)
            {
                return;
            }

            // break into batches of 500
            const int batchSize = 500;
            int       buckets   = (int)uniqueStackIdCount.Value / batchSize;

            buckets = Math.Max(1, buckets);
            int totalOrphanedEventCount = 0;
            int totalStackIds           = 0;

            for (int batchNumber = 0; batchNumber < buckets; batchNumber++)
            {
                await RenewLockAsync(context);

                var stackIdTerms = await _elasticClient.SearchAsync <PersistentEvent>(s => s.Aggregations(a => a
                                                                                                          .Terms("terms_stack_id", c => c.Field(f => f.StackId).Include(batchNumber, buckets).Size(batchSize * 2))));

                var stackIds = stackIdTerms.Aggregations.Terms("terms_stack_id").Buckets.Select(b => b.Key).ToArray();
                if (stackIds.Length == 0)
                {
                    continue;
                }

                totalStackIds += stackIds.Length;

                var stacks = await _elasticClient.MultiGetAsync(r => r.SourceEnabled(false).GetMany <Stack>(stackIds));

                var missingStackIds = stacks.Hits.Where(h => !h.Found).Select(h => h.Id).ToArray();


                if (missingStackIds.Length == 0)
                {
                    _logger.LogInformation("{BatchNumber}/{BatchCount}: Did not find any missing stacks out of {StackIdCount}", batchNumber, buckets, stackIds.Length);
                    continue;
                }

                totalOrphanedEventCount += missingStackIds.Length;
                _logger.LogInformation("{BatchNumber}/{BatchCount}: Found {OrphanedEventCount} orphaned events from missing stacks {MissingStackIds} out of {StackIdCount}", batchNumber, buckets, missingStackIds.Length, missingStackIds, stackIds.Length);
                await _elasticClient.DeleteByQueryAsync <PersistentEvent>(r => r.Query(q => q.Terms(t => t.Field(f => f.StackId).Terms(missingStackIds))));
            }

            _logger.LogInformation("Found {OrphanedEventCount} orphaned events from missing stacks out of {StackIdCount}", totalOrphanedEventCount, totalStackIds);
        }
Exemplo n.º 3
0
        public async Task DeleteSchemaRecordsForOwnerAsync(string ownerId)
        {
            var deleteResponse = await _client.DeleteByQueryAsync <SchemaInfo>(s => s.Query(q => QueryByOwnerId(q, ownerId)));

            if (!deleteResponse.IsValid)
            {
                throw new SchemaStoreException(
                          $"Failed to delete schema record for all schemas owned by {ownerId}");
            }
            await _client.RefreshAsync(Indices.Index <SchemaInfo>());
        }
Exemplo n.º 4
0
        public async Task ReIndex()
        {
            await elasticClient.DeleteByQueryAsync <ProductViewModel>(q => q.Index(defaultIndex).MatchAll());

            var products = productRepository.GetAll().ToArray();

            foreach (var product in products)
            {
                await elasticClient.IndexAsync(product, d => d.Index(defaultIndex).Id(product.ProductCode));
            }
        }
Exemplo n.º 5
0
        public async Task <BaseResponse> DeleteLocation(string locationID)
        {
            var location = GetLocationByID(locationID);

            try
            {
                var response = await _elasticClient.DeleteByQueryAsync <Location>(s => s
                                                                                  .Query(q => q.Term(f => f.ID.Suffix("keyword"), locationID)
                                                                                         )
                                                                                  );

                if (!response.IsValid)
                {
                    return(new ErrorResponse(ConstMessages.QueryFailedCode, ConstMessages.QueryFailedMsg));
                }
                else
                {
                    _webhookSender.WebhookStorage(Resources.Delete, location);
                    return(new SuccessResponse(ConstMessages.DeleteSuccess));
                }
            }
            catch (Exception ex)
            {
                return(new ErrorResponse(ConstMessages.UnidentifiedErrorCode, ex.ToString()));
            }
        }
Exemplo n.º 6
0
        public async Task <int> ReIndex()
        {
            await _elasticClient.DeleteByQueryAsync <StudentDataTransfer>(q => q.MatchAll());

            var allStudents = await _context.Set <Student>().Select(e => new StudentDataTransfer
            {
                FirstName      = e.FirstName,
                LastName       = e.LastName,
                GraduationYear = e.GraduationYear,
                Phone          = e.Phone,
                StudentId      = e.StudentId,
                UniversityName = e.UniversityName,
            }).ToArrayAsync();

            var res = await _elasticClient.IndexManyAsync(allStudents);

            if (res.Errors)
            {
                foreach (var itemWithError in res.ItemsWithErrors)
                {
                    _logger.LogError(itemWithError.Error.ToString(), itemWithError.Id);
                }
                return(-1);
            }
            return(allStudents.Count());
        }
Exemplo n.º 7
0
 private static IEnumerable <Func <object> > DocumentCommandsAsync(IElasticClient elastic)
 {
     return(new List <Func <object> >
     {
         () => elastic.BulkAsync(
             new BulkRequest("test_index")
         {
             Operations = new List <IBulkOperation>
             {
                 new BulkCreateOperation <Post>(new Post {
                     Id = 1, Title = "BulkCreateOperation"
                 })
             }
         }),
         () => elastic.CreateAsync(new CreateRequest <Post>(new Post {
             Id = 2, Title = "CreateRequest"
         }, "test_index")),
         () => elastic.CreateDocumentAsync(new Post {
             Id = 3, Title = "CreateDocument"
         }),
         () => elastic.CountAsync <Post>(),
         () => elastic.SearchAsync <Post>(s => s.MatchAll()),
         () => elastic.DeleteByQueryAsync(new DeleteByQueryRequest("test_index")
         {
             Size = 0
         })
     });
 }
Exemplo n.º 8
0
 /// <summary>
 /// Deletes an article by its id.
 /// </summary>
 /// <param name="client">The client to use.</param>
 /// <param name="index">The index to use.</param>
 /// <param name="articleId">The article id to delete.</param>
 private static void DeleteByIdAsync(IElasticClient client, string index, string articleId)
 {
     client.DeleteByQueryAsync <Article>(descriptor =>
                                         descriptor.Index(index)
                                         .Refresh()
                                         .Query(q => q.MatchPhrase(m => m
                                                                   .Field(f => f.Id)
                                                                   .Query(articleId))));
 }
Exemplo n.º 9
0
        public async Task ReIndex(List <TDocument> documents)
        {
            await _elasticClient.DeleteByQueryAsync <TDocument>(q => q.MatchAll());

            foreach (var doc in documents)
            {
                await _elasticClient.IndexDocumentAsync(doc);
            }
        }
Exemplo n.º 10
0
 public Task DeleteExternalPoisBySource(string source)
 {
     return(_elasticClient.DeleteByQueryAsync <Feature>(d =>
                                                        d.Index(EXTERNAL_POIS)
                                                        .Query(q =>
                                                               q.Term(t => t.Field($"{PROPERTIES}.{FeatureAttributes.POI_SOURCE}").Value(source.ToLower()))
                                                               )
                                                        ));
 }
Exemplo n.º 11
0
        /// <summary>
        /// 通过条件删除
        /// </summary>
        public static async Task <DeleteByQueryResponse> DeleteByQueryAsync_ <T>(this IElasticClient client, string indexName, QueryContainer where) where T : class, IESIndex
        {
            var query = new DeleteByQueryRequest <T>(indexName)
            {
                Query = where
            };

            var response = await client.DeleteByQueryAsync(query);

            return(response);
        }
Exemplo n.º 12
0
        public async Task <IActionResult> ReIndex()
        {
            await _elasticClient.DeleteByQueryAsync <AutoPostSearchDTO>(q => q.MatchAll());

            IEnumerable <AutoPost> allPosts = await _postService.Get <AutoPost>();

            IEnumerable <AutoPostSearchDTO> postsToIndex = _mapper.Map <IEnumerable <AutoPost>, IEnumerable <AutoPostSearchDTO> >(allPosts);
            await _elasticClient.IndexManyAsync(postsToIndex);

            return(Ok($"{postsToIndex.Count()} post(s) reindexed"));
        }
Exemplo n.º 13
0
        public async Task <bool> DeleteJobsForOwnerAsync(string ownerId)
        {
            var deleteResponse = await _client.DeleteByQueryAsync <JobInfo>(s => s.Query(q => QueryByOwnerId(ownerId)));

            if (!deleteResponse.IsValid)
            {
                throw new JobStoreException(
                          $"Failed to delete all jobs owned by {ownerId}");
            }
            return(true);
        }
Exemplo n.º 14
0
        /// <summary>
        /// 通过条件删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="client"></param>
        /// <param name="indexName"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static async Task DeleteByQueryAsync_ <T>(this IElasticClient client, string indexName, QueryContainer where) where T : class, IElasticSearchIndex
        {
            var query = new DeleteByQueryRequest <T>(indexName)
            {
                Query = where
            };

            var response = await client.DeleteByQueryAsync(query);

            response.ThrowIfException();
        }
Exemplo n.º 15
0
        public async Task <DeleteByQueryResponse> RemoveOldMessages(int days)
        {
            var response = await elasticClient.DeleteByQueryAsync <ChatMessage>(_ =>
                                                                                _.Query(q => q
                                                                                        .DateRange(r => r
                                                                                                   .Field(f => f.PostedTime)
                                                                                                   .LessThanOrEquals(DateTime.UtcNow.AddDays(-days))
                                                                                                   )
                                                                                        ));

            return(response);
        }
Exemplo n.º 16
0
        public Task DeleteAllAsync(
            CancellationToken cancellationToken)
        {
            var readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <TReadModel>();

            _log.Information($"Deleting ALL '{typeof(TReadModel).PrettyPrint()}' read models from index '{readModelDescription.IndexName}'");

            return(_elasticClient.DeleteByQueryAsync <TReadModel>(d => d
                                                                  .Index(readModelDescription.IndexName.Value)
                                                                  .Type <TReadModel>()
                                                                  .Query(q => q.MatchAll())));
        }
Exemplo n.º 17
0
        public async Task <IActionResult> ReIndex()
        {
            await _elasticClient.DeleteByQueryAsync <Product>(q => q.MatchAll());

            var allProducts = (await _productService.GetProducts(int.MaxValue)).ToArray();

            foreach (var product in allProducts)
            {
                await _elasticClient.IndexDocumentAsync(product);
            }

            return(Ok($"{allProducts.Length} product(s) reindexed"));
        }
Exemplo n.º 18
0
        public static Task <IDeleteByQueryResponse> DeleteByQueryAsync <TDocument>(
            this IElasticClient client,
            Func <DeleteByQueryDescriptor <TDocument>, DeleteByQueryDescriptor <TDocument> > selector,
            CancellationToken cancellationToken = default(CancellationToken),
            string indexSuffix = null)
            where TDocument : class
        {
            var index = GetIndex <TDocument>(indexSuffix);

            return(client.DeleteByQueryAsync <TDocument>(
                       s => selector(s.Index(index)),
                       cancellationToken));
        }
Exemplo n.º 19
0
        public async Task <IActionResult> ReIndex()
        {
            await elasticClient.DeleteByQueryAsync <News>(q => q.MatchAll());

            var allNews = await context.News.ToListAsync();

            foreach (var news in allNews)
            {
                await elasticClient.IndexDocumentAsync(news);
            }

            return(Ok($"{allNews.Count} news reindexed"));
        }
Exemplo n.º 20
0
        public async Task <bool> Remove(Guid entryId)
        {
            var request = new DeleteByQueryRequest <T>(_Index)
            {
                Query = new TermQuery()
                {
                    Field = "_id", Value = entryId.ToString(), IsStrict = true
                }
            };

            var response = await _Client.DeleteByQueryAsync(request);

            return(response.Took > 0);
        }
Exemplo n.º 21
0
        public async Task <int> ReindexAll()
        {
            await _elasticClient.DeleteByQueryAsync <SearchGoods>(q => q.MatchAll());

            var allGoods = await FindAll();

            foreach (var item in allGoods)
            {
                // 키워드 검색대상 추가
                item.terms = $"{item.nameKr} {item.category1} {item.category2} {item.category3} {item.terms}";
                await _elasticClient.IndexDocumentAsync(item);
            }
            return(allGoods.Count);
        }
Exemplo n.º 22
0
        public async Task RemoveDocumentsFromFolder(FolderIdentity folderId, CancellationToken cancellationToken)
        {
            if (folderId == null)
            {
                throw new ArgumentNullException(nameof(folderId));
            }

            var response = await elasticClient.DeleteByQueryAsync <DocumentInfoModel>(dbqd => dbqd
                                                                                      .Index(options.IndexName)
                                                                                      .Query(qcd => qcd
                                                                                             .Term("parentFoldersPath", folderId.ToString())),
                                                                                      CancellationToken.None);

            CheckResponse(response, $"Error occured during removing items from folder {folderId}");
        }
Exemplo n.º 23
0
        public async Task <DeleteByQueryResponse> DeleteByQuery <T>(Expression <Func <T, bool> > expression, string index = "")
            where T : class, new()
        {
            var indexName = index.GetIndex <T>();
            var request   = new DeleteByQueryRequest <T>(indexName);
            var build     = new QueryBuilder <T>();

            request.Query = build.GetQueryContainer(expression);
            var response = await _elasticClient.DeleteByQueryAsync(request);

            if (!response.IsValid)
            {
                throw new Exception("删除失败:" + response.OriginalException.Message);
            }
            return(response);
        }
Exemplo n.º 24
0
        public async Task <bool> DeleteDatasetsForOwnerAsync(string ownerId)
        {
            if (ownerId == null)
            {
                throw new ArgumentNullException(nameof(ownerId));
            }
            var deleteResponse = await _client.DeleteByQueryAsync <DatasetInfo>(s => s.Query(q => QueryHelper.FilterByOwnerId(ownerId)));

            if (!deleteResponse.IsValid)
            {
                throw new DatasetStoreException(
                          $"Failed to delete all datasets owned by {ownerId}");
            }
            await _client.RefreshAsync(Indices.Index <DatasetInfo>());

            return(true);
        }
Exemplo n.º 25
0
        public async Task <IActionResult> DelDoc([FromForm] int id)
        {
            //判断文档是否存在,这里是根据索引名字(数据库),索引type(表),和该文档的id来判断的(主键)
            IExistsResponse existsResponse = await _esClient.DocumentExistsAsync(new DocumentExistsRequest("users", "appuser", id));

            if (existsResponse.Exists)
            {
                //删除对应id的文档
                var res = await _esClient.DeleteByQueryAsync <AppUser>(d =>
                {
                    return(d.Index("users").Type("appuser").Query(q => q.Term(tm => tm.Field(new Field("id")).Value(id))));
                });

                if (res.IsValid)
                {
                    return(Ok());
                }
                return(BadRequest());
            }
            return(NoContent());
        }
Exemplo n.º 26
0
        public async Task <IActionResult> Delete(string id, [FromServices] IElasticClient elasticClient)
        {
            var result = await elasticClient.GetAsync <Log>(id);

            if (result.Source == null)
            {
                return(NotFound());
            }

            var request = new DeleteByQueryRequest <Log>
            {
                Query = new QueryContainer(new TermQuery
                {
                    Field = "id",
                    Value = id
                })
            };

            await elasticClient.DeleteByQueryAsync(request);

            return(Ok());
        }
 protected override Task <IDeleteResponse> ExecuteCoreAsync(IElasticClient client, string index)
 {
     return(client.DeleteByQueryAsync <T>(desc => BuildQuery(desc).Index(index)));
 }
Exemplo n.º 28
0
 public Task DeleteAllActivitiesFromRepoAsync()
 {
     return(_client.DeleteByQueryAsync <Activity>(del => del
                                                  .Query(q => q.QueryString(qs => qs.Query("*")))));
 }
 protected override async Task <IDeleteByQueryResponse> ExecuteCoreAsync(IElasticClient client, string index)
 {
     return(await client.DeleteByQueryAsync <T>(desc => BuildQuery(desc).Index(index)).ConfigureAwait(false));
 }
Exemplo n.º 30
0
 public async Task DeleteAllAsync()
 {
     await _elasticClient.DeleteByQueryAsync <T>(del => del.Query(q => q.QueryString(qs => qs.Query("*"))));
 }