【问题标题】:Lucene: building an indexLucene:建立索引
【发布时间】:2018-06-11 09:27:17
【问题描述】:

我很久以前就使用 Lucene 4.6 构建了一个项目。现在目前想升级到7.3。

这个项目有三个文件,每个文件都有一个类(与文件同名):Main、Indexer、Search。

我在 Indexer.java 中遇到了问题,更具体地说是在 buildIndex() 中。

Main.java中,我已经定义了数据目录的位置和索引的位置,并通过将路径发送到buildIndex()开始创建索引em> 必须建在哪里:

File dataDirectory = new File("C:\\datalocation");
File indexDirectory = new File("C:\\indexlocation");
(...)

IndexWriter index = Indexer.createIndex(indexDirectory);
Indexer.buildIndex(index, dataDirectory, indexDirectory);
Indexer.closeIndex(index);
System.out.println("Index built");

Indexer.java 的内部

static void buildIndex(IndexWriter index, File dataDirectory,
            File IndexDirectory) throws IOException {
        File[] files = dataDirectory.listFiles();
        for (int i = 0; i < files.length; i++) {
            Document document = new Document();

            Reader reader = new FileReader(files[i]);
//the following line is where error 1 appears:
            document.add(new Field("contents", reader));

            String path = files[i].getCanonicalPath();
//the following line is where error 2 appears:
            document.add(new Field("path", path, Field.Store.YES,Field.Index.NOT_ANALYZED));

            index.addDocument(document);
        }
    }

我遇到的问题是:

  1. 构造函数 Field(String, Reader) 未定义。

  2. 索引无法解析或不是字段

我该如何解决这个问题?

将参数“reader”转换为“IndexableFieldType”

不能将“阅读器”类型更改为“IndexableFieldType”。

注意:数据目录路径中有一个.txt文件,里面只写了“Maven”。

【问题讨论】:

    标签: java lucene


    【解决方案1】:

    Field 的这两个构造函数在 Lucene 4 中都标记为 deprecated,并在后来被删除。

    在这两种情况下推荐的课程都是TextField,或者在第二种情况下也是StringField

    所以第一个可能看起来像:

    document.add(new TextField("contents", reader));
    

    第二个:

    document.add(new StringField("path", path, Field.Store.YES));
    

    请注意,我找不到与 Field.Index.NOT_ANALYZED 参数明确等效的参数(StringField 未标记,TextField 已标记,不知道是否与此直接相关)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      相关资源
      最近更新 更多