How to search russian texts in Lucene index?
I can not understand where I was wrong. My code, where "/ home/test/03m8894---20070213134234.txt" - file with the English text, and "/ home/test/01---20061121103506.txt" - file with the Russian text. Both files are encoded in UTF-8. The result of the execution of the program: 1 0 Ie the program finds only text in English and Russian text ignored. Although if you do
for (int m = 0; m <totalDocs; m + +) {
Document thisDoc = reader.document (m);
System.out.print (thisDoc.get ("partnum"));
the text field partnum correctly, no errors in the output encoding on the screen.
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);
You said the files are UTF-8 encoded, but you use:
InputStreamReader isr = new InputStreamReader(new FileInputStream(f1[x]));
This relies on the default encoding, which is likely not UTF-8. try:
InputStreamReader isr = new InputStreamReader(new FileInputStream(f1[x]), "UTF-8");
It's is not critical, because all files in UTF-8 and default system and file encoding is UTF-8...
精彩评论