コード例 #1
0
        public async Task IndexDocument(string fileName, string filePath, string contents)
        {
            var id = HashUtility.Hash(filePath);

            var doc = new Doc()
            {
                Id       = id,
                FileName = fileName,
                Path     = filePath,
                Text     = contents
            };

            await CreateElasticClient().IndexDocumentAsync(doc);
        }
コード例 #2
0
        static async Task BuildIndex(string startPath)
        {
            Console.WriteLine("Indexes Directory: " + startPath);

            // Use TikaOnDotNet to extract the contents of the document.
            var textExtractor = new TextExtractor();

            // Use ElasticSearch to index the document
            var client = CreateElasticClient();

            // Loop through all files in the passed in directory
            var directory = new DirectoryInfo(startPath);

            if (directory.Exists)
            {
                foreach (var file in directory.GetFiles())
                {
                    var contents = textExtractor.Extract(file.FullName);
                    if (!string.IsNullOrWhiteSpace(contents.Text))
                    {
                        Console.WriteLine("Indexing File " + file.FullName);

                        var id = HashUtility.Hash(file.FullName);

                        var doc = new Doc()
                        {
                            Id       = id,
                            FileName = file.Name,
                            Path     = file.FullName,
                            Text     = contents.Text
                        };
                        await client.IndexDocumentAsync(doc);
                    }
                }

                foreach (var dir in directory.GetDirectories())
                {
                    await BuildIndex(dir.FullName);
                }
            }
        }