如何在Lucene索引中搜索俄语文本?

时间:2011-01-10 15:19:10

标签: java lucene

我无法理解我错在哪里。 我的代码,其中“/ home / test / 03m8894 --- 20070213134234.txt” - 带有英文文本的文件,和“/ home / test / 01 --- 20061121103506.txt” - 带有俄文文本的文件。 两个文件都以UTF-8编码。 执行程序的结果: 1 0 即程序发现只有英文文本和俄文文本被忽略。 虽然如果你做了

            for (int m = 0; m <totalDocs; m + +) { 

            Document thisDoc = reader.document (m); 
            System.out.print (thisDoc.get ("partnum")); 

文本字段partnum正确,屏幕上的输出编码没有错误。

 RAMDirectory directory = new RAMDirectory();

        IndexWriter writer =
                //new IndexWriter(directory, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
                new IndexWriter(directory, new RussianAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED);
        File f1[] = {new File("/home/test/03m8894---20070213134234.txt"), new File("/home/test/01---20061121103506.txt")};

        String strLine1 = "";
        for (int x = 0; x < f1.length; x++) {
            Document doc = new Document();
            int length = (int) f1[x].length();
            if (length != 0) {
                char[] cbuf = new char[length];
                InputStreamReader isr = new InputStreamReader(new FileInputStream(f1[x]));
                final int read = isr.read(cbuf);
                strLine1 = new String(cbuf, 0, read);
                isr.close();
                doc.add(new Field("partnum", strLine1, Field.Store.YES, Field.Index.NOT_ANALYZED));
                //doc.add(new Field("description", "Illidium Space Modulator", Field.Store.YES, Field.Index.ANALYZED));
                writer.addDocument(doc);
            }

        }
writer.close();

        IndexSearcher searcher = new IndexSearcher(directory);
        IndexReader reader = searcher.getIndexReader();
        int totalDocs = reader.numDocs();



        for (int m = 0; m < totalDocs; m++) {

            Document thisDoc = reader.document(m);

     String tmp_str=thisDoc.get("partnum");

     Query query = new TermQuery(new Term("partnum", tmp_str));

     TopDocs rs = searcher.search(query, null, 10);
            System.out.println(rs.totalHits);

2 个答案:

答案 0 :(得分:1)

您说文件是UTF-8编码的,但您使用:


InputStreamReader isr = new InputStreamReader(new FileInputStream(f1[x]));

这取决于默认编码,可能不是UTF-8。尝试:


InputStreamReader isr = new InputStreamReader(new FileInputStream(f1[x]), "UTF-8");

答案 1 :(得分:0)

这并不重要,因为UTF-8中的所有文件以及默认的系统和文件编码都是UTF-8 ......