コード例 #1
0
        /// <summary>
        /// Inserts the passed data into elastic search index
        /// </summary>
        /// <param name="entities">The list of the entities that will be indexed</param>
        /// <param name="refresh">Flag to refresh index state with new data</param>
        public static void BulkInsertAttachment <T>(List <T> entities, bool refresh = true) where T : EsAttachment
        {
            if (entities.Count > 0)
            {
                entities[0].CreateIndex <T>();
                var indexName = entities[0].IndexAlias;

                var esClient = Manager.EsClient;

                var descriptor = new BulkDescriptor();
                descriptor.TypeQueryString("_doc");
                foreach (var doc in entities)
                {
                    descriptor.Index <T>(i => i
                                         .Index(indexName)
                                         .Document(doc)
                                         .Pipeline(doc.PipeLineName));
                }
                var response = esClient.Bulk(descriptor);
                if (!response.IsValid)
                {
                    throw new Exception(response.OriginalException.Message);
                }
                if (refresh)
                {
                    esClient.Indices.Refresh(indexName);
                }
            }
            else
            {
                return;
            }
        }
コード例 #2
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 UpsertAttachmentsAsync <T>(IList <T> entities, Expression <Func <T, object> > idField, bool refresh = false) where T : EsAttachment
        {
            var indexname = "";
            var pipeline  = "";

            if (entities.Count > 0)
            {
                entities[0].CreateIndex <T>();
                indexname = entities[0].IndexAlias;
                pipeline  = entities[0].PipeLineName;
            }
            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.Pipeline(pipeline).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);
            }
        }