lucene建立索引

官方demo修改了下,可以运行
lucene-6.3.0建立索引代码:

public class IndexFiles {
    public static void main(String[] args) {
        String usage = "java org.apache.lucene.demo.IndexFiles [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\nThis indexes the documents in DOCS_PATH, creating a Lucene indexin INDEX_PATH that can be searched with SearchFiles";

        String indexPath = "index";
        String docsPath = null;
        boolean create = true;
        for (int i = 0; i < args.length; ++i) {
            if ("-index".equals(args[i])) {
                indexPath = args[(i + 1)];
                ++i;
            } else if ("-docs".equals(args[i])) {
                docsPath = args[(i + 1)];
                ++i;
            } else if ("-update".equals(args[i])) {
                create = false;
            }
        }

        if (docsPath == null) {
            System.err.println("Usage: " + usage);
            System.exit(1);
        }

        //读取被索引的文件
        Path docDir = Paths.get(docsPath, new String[0]);
        if (!(Files.isReadable(docDir))) {
            System.out
                    .println("Document directory '"
                            + docDir.toAbsolutePath()
                            + "' does not exist or is not readable, please check the path");
            System.exit(1);
        }

        Date start = new Date();
        try {
            System.out.println("Indexing to directory '" + indexPath + "'...");
            //建立索引目录
            Directory dir = FSDirectory.open(Paths
                    .get(indexPath, new String[0]));
            Analyzer analyzer = new StandardAnalyzer();
            //IndexWriterConfig用于配置IndexWriter,建立索引文件
            IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

            if (create) {       iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
            } else              iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
            }
            //建立索引文件写出流
            IndexWriter writer = new IndexWriter(dir, iwc);
            indexDocs(writer, docDir);

            writer.close();

            Date end = new Date();
            System.out.println((end.getTime() - start.getTime())
                    + " total milliseconds");
        } catch (IOException e) {
            System.out.println(" caught a " + e.getClass()
                    + "\n with message: " + e.getMessage());
        }
    }

    //读取要索引的文件或文件夹
    static void indexDocs(IndexWriter writer, Path path) throws IOException {
        if (Files.isDirectory(path, new LinkOption[0])) {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file,
                        BasicFileAttributes attrs) throws IOException {
                    try {
                        IndexFiles.indexDoc(writer, file, attrs
                                .lastModifiedTime().toMillis());
                    } catch (IOException localIOException) {
                    }
                    return FileVisitResult.CONTINUE;

                }
            });
        }

        else {
            System.out.println("else");
            indexDoc(writer, path,
                    Files.getLastModifiedTime(path, new LinkOption[0])
                            .toMillis());
        }
    }

    //建立一个文件的索引
    static void indexDoc(IndexWriter writer, Path file, long lastModified)
            throws IOException {
        System.out.println("file=" + file.toString());
        InputStream stream = Files.newInputStream(file, new OpenOption[0]);
        Throwable localThrowable3 = null;
        try {
            Document doc = new Document();

            Field pathField = new StringField("path", file.toString(),
                    Field.Store.YES);
            doc.add(pathField);

            doc.add(new LongPoint("modified", new long[] { lastModified }));

            doc.add(new TextField("contents", new BufferedReader(
                    new InputStreamReader(stream, StandardCharsets.UTF_8))));
            //是否更新索引
            if (writer.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE) {
                System.out.println("adding " + file);
                writer.addDocument(doc);
            } else {
                System.out.println("updating " + file);
                writer.updateDocument(new Term("path", file.toString()), doc);
            }
        } catch (Throwable localThrowable1) {
        } finally {
            if (stream != null)
                if (localThrowable3 != null) {
                    try {
                        stream.close();
                    } catch (Throwable localThrowable2) {
                        localThrowable3.addSuppressed(localThrowable2);
                    }
                } else
                    stream.close();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值