Пример #1
0
 public void Update <T>(Id id, object document) where T : class
 {
     WritesMeter.Mark();
     _pendingSize++;
     Logger.Debug("Updating document {0}", id.GetString(_client.ConnectionSettings));
     _pendingDocs.Update <T, object>(x => x.Id(id).Doc(document));
 }
        public void BulkUpdateObject()
        {
            //Lets first insert some documents with id range 5000-6000
            var descriptor = new BulkDescriptor();
            foreach (var i in Enumerable.Range(5000, 1000))
                descriptor.Index<ElasticsearchProject>(op => op.Object(new ElasticsearchProject { Id = i }));

            var result = this._client.Bulk(d=>descriptor);
            result.Should().NotBeNull();
            result.IsValid.Should().BeTrue();

            //Now lets update all of them giving them a name
            descriptor = new BulkDescriptor().Refresh();
            foreach (var i in Enumerable.Range(5000, 1000))
            {
                int id = i;
                descriptor.Update<ElasticsearchProject, object>(op => op
                    .Object(new ElasticsearchProject { Id = id })
                    .Document(new { name = "SufixedName-" + id})
                );
            }

            result = this._client.Bulk(d=>descriptor);
            result.Should().NotBeNull();
            result.IsValid.Should().BeTrue();
            result.Errors.Should().BeFalse();
            result.Items.Count().Should().Be(1000);
            result.Items.All(i => i != null).Should().BeTrue();

            var updatedObject = this._client.Source<ElasticsearchProject>(i=>i.Id(5000));
            Assert.NotNull(updatedObject);
            Assert.AreEqual(updatedObject.Name, "SufixedName-5000");
        }
Пример #3
0
        public static void UpdateElasticSearch()
        {
            var settings = new ConnectionSettings(new Uri("https://search-search-rvezy-sdxhps7oh6vgvoy7f3ctpxnh7q.us-east-2.es.amazonaws.com")).DefaultIndex("rv-local");
            var client   = new ElasticClient(settings);

            var descriptor = new BulkDescriptor();

            for (int i = 0; i < 2; i++)
            {
                var id    = "a306e70e-1ab1-4dde-9c58-31e677762af6";
                var score = 8.8;
                if (i == 1)
                {
                    id    = "8fbfb724-8ebb-4bc0-82b0-5a1cf52fd2cc";
                    score = 10;
                }
                descriptor.Update <RV>(u => u
                                       .Id(id)
                                       .Doc(new RV {
                    Id = new Guid(id), Score = score
                }));
            }

            var result = client.Bulk(descriptor);
        }
Пример #4
0
        /// <summary>
        /// 批量局部更新
        /// 注意:此方法将使用默认索引
        /// </summary>
        /// <param name="models">数据集合</param>
        /// <param name="partial">局部数据集合</param>
        /// <param name="isThrow">抛出异常</param>
        /// <returns></returns>
        public bool UpdateBulk <T>(
            List <T> models,
            List <object> partial,
            bool isThrow = false) where T : class
        {
            if (models == null || partial == null)
            {
                return(false);
            }
            if (models.Count != partial.Count)
            {
                throw new Exception("集合数量不一致");
            }
            var Bulk = new BulkDescriptor().Index(IndiceName);

            for (int i = 0; i < models.Count; i++)
            {
                Bulk.Update <T, object>(u => u.IdFrom(models[i]).Doc(partial[i]));
            }
            var response = ElasticClient.Bulk(Bulk);

            if (!response.IsValid && isThrow)
            {
                throw new ElasticsearchException(response);
            }
            return(response.IsValid);
        }
Пример #5
0
        public async Task <long> UpdateAsync(IList <string> ids, object part, Refresh refresh = Refresh.WaitFor)
        {
            if (!ids.HasItems())
            {
                throw new ArgumentException(string.Format(Utils.ARGUMENT_EMPTY_LIST_MESSAGE, nameof(ids)), nameof(ids));
            }

            if (part == null)
            {
                throw new ArgumentNullException(string.Format(Utils.ARGUMENT_NULL_MESSAGE, nameof(part)), nameof(part));
            }

            var descriptor = new BulkDescriptor();

            foreach (var id in ids)
            {
                descriptor.Update <T, object>(x => x.Id(id)
                                              .Doc(part)
                                              .RetriesOnConflict(_maxRetries)
                                              );
            }

            descriptor.Refresh(refresh);

            var response = await _client.BulkAsync(descriptor);

            return(!(response.IsValid && response.Items.Count > 0) ? 0 : response.Items.Count);
        }
        public virtual bool Update <T>(IList <T> documents) where T : MXSearchDocument
        {
            var descriptor = new BulkDescriptor().FixedPath(indexName.Value);

            foreach (var doc in documents)
            {
                descriptor.Update <T>(op => op
                                      .IdFrom(doc)
                                      .Doc(doc)
                                      );
            }

            var result = Client.Bulk(d => descriptor);

            return(!result.Errors);
        }
Пример #7
0
        private static void AddPostcodeSearch(IHit <Elastic.Common.Entities.Address> address, BulkDescriptor descriptor)
        {
            var id = address.Id;

            try
            {
                descriptor.Update <Elastic.Common.Entities.Address, object>(u => u
                                                                            .Index("addresses")
                                                                            .Id(id)
                                                                            .Script("ctx._source.postcodesearch = ctx._source.postcode.replace(' ', '')")
                                                                            );
            }
            catch (Exception ex)
            {
                Console.WriteLine("Updating address id {0} failed! {1}", id, ex.StackTrace);
            }
        }
Пример #8
0
        /// <summary>
        /// Lấy các link đang ở trạng thái chưa xử lý
        /// </summary>
        /// <param name="page_size">Số lượng link lấy ra</param>
        /// <param name="doi_trang_thai_xu_ly">True: Đối trạng thái xử lý của các link thành DANG_XU_LY, False: Không thay đổi trạng thái xử lý</param>
        /// <returns></returns>
        public List <JobLink> GetJobChuaXuLy(int page_size, bool doi_trang_thai_xu_ly = false)
        {
            var lst = new List <JobLink>();
            var re  = client.Search <JobLink>(s => s.Query(q => q.Term(t => t.Field("loai").Value(LoaiLink.JOB_LINK)) &&
                                                           q.Term(t => t.Field("trang_thai_xu_ly").Value(TrangThaiXuLy.DANG_XU_LY))).Size(page_size));

            lst = re.Hits.Select(x => ConvertDoc(x)).ToList();
            if (doi_trang_thai_xu_ly)
            {
                var bulk = new BulkDescriptor();
                foreach (var item in lst)
                {
                    bulk.Update <JobLink, object>(u => u.Id(item.id).Doc(new { trang_thai_xu_ly = TrangThaiXuLy.DANG_XU_LY }));
                }
                client.Bulk(bulk);
            }
            return(lst);
        }
Пример #9
0
        public void BulkUpdateObject()
        {
            //Lets first insert some documents with id range 5000-6000
            var descriptor = new BulkDescriptor();

            foreach (var i in Enumerable.Range(5000, 1000))
            {
                descriptor.Index <ElasticSearchProject>(op => op.Object(new ElasticSearchProject {
                    Id = i
                }));
            }

            var result = this._client.Bulk(descriptor);

            result.Should().NotBeNull();
            result.IsValid.Should().BeTrue();

            //Now lets update all of them giving them a name
            descriptor = new BulkDescriptor();
            foreach (var i in Enumerable.Range(5000, 1000))
            {
                int id = i;
                descriptor.Update <ElasticSearchProject, object>(op => op
                                                                 .Object(new ElasticSearchProject {
                    Id = id
                })
                                                                 .Document(new { name = "SufixedName-" + id })
                                                                 .Refresh()
                                                                 );
            }

            result = this._client.Bulk(descriptor);
            result.Should().NotBeNull();
            result.IsValid.Should().BeTrue();
            result.Items.Count().Should().Be(1000);
            result.Items.All(i => i != null).Should().BeTrue();
            result.Items.All(i => i.OK).Should().BeTrue();

            var updatedObject = this._client.Get <ElasticSearchProject>(5000);

            Assert.NotNull(updatedObject);
            Assert.AreEqual(updatedObject.Name, "SufixedName-5000");
        }
Пример #10
0
        public void BulkUpdateDocuments()
        {
            var client     = GetElasticClient();
            var descriptor = new BulkDescriptor();

            foreach (var i in Enumerable.Range(0, 10))
            {
                var isca = new IscaPartial {
                    SerialNumber = "0000" + i, LastPosition = DateTime.UtcNow
                };
                descriptor.Update <Isca, IscaPartial>(op => op.Doc(isca).RetriesOnConflict(3).Id(isca.SerialNumber));
            }
            descriptor.Refresh(Refresh.True);
            var result = client.Bulk(descriptor);

            //var client = new ElasticClient(config);
            //var isca = new Isca { NumeroSerie = "00001", Date = DateTime.UtcNow.ToString() };
            //var response = client.Update<Isca, Isca>(new DocumentPath<Isca>("00001"), u => u.Doc(isca));
        }
Пример #11
0
        public bool UpdateNgayHetHan(List <PhanQuyen> lst_id, string nguoi_sua)
        {
            bool             result = false;
            List <PhanQuyen> temp   = new List <PhanQuyen>();

            try
            {
                int  times = 0;
                long now   = XMedia.XUtil.TimeInEpoch(DateTime.Now);
                int  spl   = 500;
                while ((times * spl) <= lst_id.Count && times < lst_id.Count / spl + 1)
                {
                    BulkDescriptor bulk = new BulkDescriptor();
                    temp = lst_id.Skip(times * spl).Take(spl).ToList();
                    times++;
                    if (temp.Count > 0)
                    {
                        foreach (var obj in temp)
                        {
                            bulk.Update <PhanQuyen, object>(u => u.Id(obj.id).Doc(new
                            {
                                ngay_het  = obj.ngay_het,
                                nguoi_sua = nguoi_sua,
                                ngay_sua  = now
                            }).DocAsUpsert(false));
                        }
                        var res = client.Bulk(bulk);
                    }
                    bulk = null;
                    System.Threading.Thread.Sleep(500);
                }
                return(true);
            }
            catch
            {
            }
            finally
            {
                temp = null;
            }
            return(result);
        }
Пример #12
0
        /// <summary>
        /// Insert or updates a list of entities into the index
        /// </summary>
        /// <param name="entities">The entities that will be inserted ot update</param>
        /// <param name="idField">The Id field expression</param>
        /// <param name="refresh">Flag to refresh index state with new data</param>
        public static async void UpsertEntitiesAsync <T>(IList <T> entities, Expression <Func <T, object> > idField, bool refresh = false) where T : EsDocument
        {
            var indexname = "";

            if (entities.Count > 0)
            {
                entities[0].CreateIndex <T>();
                indexname = entities[0].IndexAlias;
            }
            else
            {
                return;
            }
            var esClient   = Manager.EsClient;
            var descriptor = new BulkDescriptor();

            descriptor.TypeQueryString("_doc");
            foreach (var doc in entities)
            {
                var idVal  = Utils.GetObjectValue(idField, doc);
                Id  idData = idVal.ToString();

                descriptor.Update <T>(op => op
                                      .Id(idData)
                                      .Index(indexname)
                                      .Doc(doc)
                                      .DocAsUpsert());
            }
            var response = await esClient.BulkAsync(descriptor);

            if (!response.IsValid)
            {
                throw new Exception(response.OriginalException.Message);
            }
            if (refresh)
            {
                esClient.Indices.Refresh(indexname);
            }
        }
Пример #13
0
        /// <summary>
        /// 批量局部更新
        /// 注意:此方法将使用默认索引
        /// </summary>
        /// <param name="ids">Id集合</param>
        /// <param name="partial">局部数据</param>
        /// <param name="isThrow">抛出异常</param>
        /// <returns></returns>
        public bool UpdateBulk <T>(
            List <object> ids,
            object partial,
            bool isThrow = false) where T : class
        {
            if (ids == null)
            {
                return(false);
            }
            var Bulk = new BulkDescriptor().Index(IndiceName);

            for (int i = 0; i < ids.Count; i++)
            {
                Bulk.Update <T, object>(u => u.Id(new Id(ids[i])).Doc(partial));
            }
            var response = ElasticClient.Bulk(Bulk);

            if (!response.IsValid && isThrow)
            {
                throw new ElasticsearchException(response);
            }
            return(response.IsValid);
        }
Пример #14
0
 public void Update <T>(Id id, Object document) where T : class
 {
     _writesMeter.Mark();
     _pendingSize++;
     _pendingDocs.Update <T, object>(x => x.Id(id).Doc(document));
 }
Пример #15
0
            static void Main(string[] args)
            {
                //Wire up an elastic client
                var elastic = new ElasticClient(
                    new ConnectionSettings(
                        new Uri("http://localhost:9200")));

                //Some useful constants
                const string IndexName = "indexName";
                const string Doc       = "doc";

                //Source and destination indices
                var          sourceIndex    = "test_user_click";
                const string Indices        = "indices";
                const string IndexNameField = "indexName.keyword";

                //Process updates by each index
                var indicesResponse = elastic.Search <dynamic>(s => s
                                                               .Size(0)
                                                               .Type(string.Empty)
                                                               .Index(sourceIndex)
                                                               .Aggregations(a => a
                                                                             .Terms(Indices, at => at
                                                                                    .Field(IndexNameField)
                                                                                    .Size(100))));

                foreach (var item in indicesResponse.Aggregations.Terms(Indices).Buckets)
                {
                    var destinationIndex = item.Key;  //Set the destination index

                    //Scrolling information
                    var pageSize       = 1000;
                    var scrollLifetime = "10s";
                    ISearchResponse <dynamic> searchResponse = null;

                    //scrolling thru the documents to update
                    while (true)
                    {
                        //perform a dynamic scrolling query against the source index
                        if (searchResponse == null)
                        {
                            searchResponse = elastic.Search <dynamic>(s => s
                                                                      .Type(string.Empty) //Ignore types completely
                                                                      .Scroll(scrollLifetime)
                                                                      .Index(sourceIndex) //Feature index to query
                                                                      .Query(q => q
                                                                             .Match(m => m
                                                                                    .Field(IndexName)
                                                                                    .Query(destinationIndex))) //Find features for the destination index
                                                                      .Size(pageSize));
                        }
                        else
                        {
                            //Using scroll to get the remaining pages of feature documents
                            searchResponse = elastic.Scroll <dynamic>(
                                new ScrollRequest(searchResponse.ScrollId, scrollLifetime));
                        }

                        //have we reached the end
                        if (searchResponse.Documents.Count == 0)
                        {
                            break;
                        }

                        //create new bulk descriptior to hold the list of updates
                        var bulkDescriptor = new BulkDescriptor();
                        bulkDescriptor.Index(destinationIndex);
                        bulkDescriptor.Type(Doc);

                        //create a partial update for the search record
                        //set the current userClickBoost for each boosted document
                        foreach (var analyticDoc in searchResponse.Documents)
                        {
                            bulkDescriptor.Update <dynamic, PartialDoc>(s => s
                                                                        .Id((string)analyticDoc.sourceId)
                                                                        .Doc(
                                                                            new PartialDoc {
                                UserClickBoost = (double)analyticDoc.userClickBoost

                                                 // additional feature boosts here
                            }));
                        }

                        //Send the bulk request to destination index
                        var updateResponse = elastic.Bulk(bulkDescriptor);

                        if (updateResponse.Errors)
                        {
                            //Handle errors if needed
                        }
                    }
                }
            }