public Index buildIndex(String result) {
        try {
            index.start(result);
            Files.walk(Paths.get(directory)).forEach(file -> {
                if (Files.isRegularFile(file)) {
                    service.submit(new ProcessFileTask(file, index));
                }
            });

            service.shutdown();

            try {
                service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            index.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return index;
    }