Beispiel #1
0
        /// <summary>
        /// 索引磁盘上的文件
        /// </summary>
        public static void IndexFiles()
        {
            var client    = new ElasticClient(setting);
            var indexName = nameof(DiskFileIndex).ToLower();

            try
            {
                client.CreateIndexIfNotExists(indexName, x => x.GetCreateIndexDescriptor <DiskFileIndex>());
            }
            catch (Exception e)
            {
                e.AddErrorLog();
            }

            var txtFiles = new string[] {
                ".txt", ".cs", ".css", ".js", ".cshtml", ".html"
                , ".readme", ".json", ".config", ".md"
            };

            var maxSize = Com.MbToB(1);

            Com.FindFiles("D:\\XXXXXXXXXXXXXXXXXXXX\\", (f) =>
            {
                Thread.Sleep(100);
                try
                {
                    if (!txtFiles.Any(x => ConvertHelper.GetString(f.Name).ToLower().EndsWith(x)) ||
                        ConvertHelper.GetString(f.FullName).ToLower().Contains("elasticsearch") ||
                        f.Length > maxSize)
                    {
                        Console.WriteLine($"跳过文件:{f.FullName}");
                        return;
                    }
                    var model       = new DiskFileIndex();
                    model.Path      = f.FullName;
                    model.Name      = f.Name;
                    model.Extension = f.Extension;
                    model.Size      = f.Length;
                    model.Content   = File.ReadAllText(model.Path);
                    var data        = new List <DiskFileIndex>()
                    {
                        model
                    };
                    client.AddToIndex(indexName, data.ToArray());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }, (c) =>
            {
                Console.WriteLine($"剩余任务:{c}");
                if (c > 5000)
                {
                    Thread.Sleep(400);
                }
            });
            Console.ReadLine();
        }