【问题标题】:Why indexer doesn't search Persian files?为什么索引器不搜索波斯文件?
【发布时间】:2016-02-05 09:00:21
【问题描述】:

我使用 lucene 3 来索引一些像这样的 txt 文件。

 public static void main(String[] args) throws Exception {

    String indexDir = "file input";
    String dataDir = "file input";
    long start = System.currentTimeMillis();

    indexer indexer = new indexer(indexDir);
    int numIndexed, cnt;
    try {
        numIndexed = indexer.index(dataDir, new TextFilesFilter());

        cnt = indexer.getHitCount("mycontents", "شهردار");
        System.out.println("count of search in contents: " + cnt);
    } finally {
        indexer.close();
    }
    long end = System.currentTimeMillis();
    System.out.println("Indexing " + numIndexed + " files took "
            + (end - start) + " milliseconds");

}

getHitCount 函数返回英语单词的命中数,但波斯语单词返回零!

 public int getHitCount(String fieldName, String searchString)
        throws IOException, ParseException {

    IndexSearcher searcher = new IndexSearcher(directory);

    Term t = new Term(fieldName, searchString);
    Query query = new TermQuery(t);

    int hitCount = searcher.search(query, 1).totalHits;
    searcher.close();
    return hitCount;
}

如何在我的项目中设置 utf-8?我使用 netbeans 并创建了一个简单的 java 项目。 我只需要在文件中进行简单的搜索!

这是我的索引器类:

 private IndexWriter writer;
private Directory directory;

public indexer(String indexDir) throws IOException {
    directory = FSDirectory.open(new File(indexDir));
    writer = new IndexWriter(directory,
            new StandardAnalyzer(
                    Version.LUCENE_30),
            true,
            IndexWriter.MaxFieldLength.UNLIMITED);
}

public void close() throws IOException {
    writer.close();
}

public int index(String dataDir, FileFilter filter)
        throws Exception {
    File[] files = new File(dataDir).listFiles();
    for (File f : files) {
        if (!f.isDirectory()
                && !f.isHidden()
                && f.exists()
                && f.canRead()
                && (filter == null || filter.accept(f))) {
            indexFile(f);
        }
    }
    return writer.numDocs();
}

private static class TextFilesFilter implements FileFilter {

    public boolean accept(File path) {
        return path.getName().toLowerCase()
                .endsWith(".txt");
    }
}

protected Document getDocument(File f) throws Exception {
    Document doc = new Document();
    doc.add(new Field("mycontents", new FileReader(f)));
    doc.add(new Field("filename", f.getName(),
            Field.Store.YES, Field.Index.NOT_ANALYZED));
    doc.add(new Field("fullpath", f.getCanonicalPath(),
            Field.Store.YES, Field.Index.NOT_ANALYZED));
    return doc;
}

private void indexFile(File f) throws Exception {
    System.out.println("Indexing " + f.getCanonicalPath());
    Document doc = getDocument(f);
    writer.addDocument(doc);
}

【问题讨论】:

  • 我们可以看看你的索引器类吗?这似乎是你自己实现的东西
  • @Niklas 我编辑了我的问题。
  • @Niklas 谢谢,我看到了,但我不知道如何更改 getHitCount 函数的字符串

标签: java indexing encoding lucene


【解决方案1】:

我怀疑问题不是 Lucene 的编码本身,而是FileReader。来自 FileReader 文档:

此类的构造函数假定默认字符编码和默认字节缓冲区大小是适当的。

在这种情况下,默认字符编码可能不合适。

代替:

doc.add(new Field("mycontents", new FileReader(f)));

尝试(假设要索引的文件是 UTF-8 编码的):

doc.add(new Field("mycontents", new InputStreamReader(new FileInputStream(f), "UTF8")));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2014-01-11
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2016-10-23
    相关资源
    最近更新 更多