lucene3.1.0+luke3.5.0开发中遇到的问题解决办法
在虚拟机上解压完lucene3.1.0后,运行luke(基于lucene的查看索引的第三方工具),运行命令#java -jar lukeall-3.5.0.jar,出来图形界面后,要求选择索引目录,我当时不知道索引目录在哪?lucene安装包自带索引吗?还是要自己生成呢?我随便选了一个目录,提示:No Valid directory at this location.Try another
在虚拟机上解压完lucene3.1.0后,运行luke(基于lucene的查看索引的第三方工具),运行命令
#java -jar lukeall-3.5.0.jar,出来图形界面后,要求选择索引目录,我当时不知道索引目录在哪?lucene安装包自带索引吗?还是要自己生成呢?我随便选了一个目录,提示:No Valid directory at this location.Try another location。
然后我就纳闷了,在网上搜,发现别人选的目录不管路径前面是什么,反正后面都是/index。后来终于知道要通过命令生成index目录,解决方法:
1.lucene-3.1.0.tar.gz解压后,重命名为lucene,然后编辑环境变量#vi /etc/profile ,追加内容为:
export LUCENE_HOME=/home/lvyuan/lucene
export CLASSPATH=.:$LUCENE_HOME/lucene-core-3.1.0.jar:$LUCENE_HOME/contrib/demo/lucene-demo-3.1.0.jar
保存并退出。使环境变量立即生效 # source /etc/profile 回车
2.为任意目录生成索引,生成成功后就会出现index文件夹;
输入命令#java org.apache.lucene.demo.IndexFiles -docs /home/lvyuan/test 成功后test目录下面会生成一个index文件夹,不过有时候index会出现在lucene根目录下面。
3.网上下载luke包,将lukeall-3.5.0.jar放到任意目录(我放在/home/下面),然后进入该目录,输入命令#java -jar lukeall-3.5.0.jar,出来图形界面后,要求选择索引目录,这时就可以选择刚才生成的index文件夹了(我这里是/home/lvyuan/test/index),确定后,可以查看索引了。ok
下面贴上我的代码
//生成索引流程
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class IndexFiles {
/**
* @param args
* @throws IOException
* @throws LockObtainFailedException
* @throws CorruptIndexException
*/
public static void main(String[] args) throws CorruptIndexException, LockObtainFailedException, IOException {
File path=new File("e:/tts");
Directory dir=FSDirectory.open(path);
Analyzer analyzer=new IKAnalyzer();
IndexWriterConfig conf=new IndexWriterConfig(Version.LUCENE_31, analyzer);
//创建索引模式:create(覆盖模式);append(追加模式)
conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
IndexWriter indexWriter=new IndexWriter(dir, conf);
Document document=new Document();
document.add(new Field("name","zhangs",Store.YES,Index.NOT_ANALYZED_NO_NORMS));
document.add(new Field("age","30",Store.YES,Index.NOT_ANALYZED_NO_NORMS));
indexWriter.addDocument(document);
indexWriter.commit();
indexWriter.close();
dir.close();
}
}
//搜索流程
import java.io.File;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class SearchFiles {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File path=new File("e:/tts");
Directory directory=FSDirectory.open(path);
IndexReader reader=IndexReader.open(directory);
IndexSearcher searcher=new IndexSearcher(reader);
Query query=new TermQuery(new Term("name","zhangs"));
TopDocs doc=searcher.search(query, 10);
ScoreDoc[] scoreDocs=doc.scoreDocs;
for(int i=0;i<scoreDocs.length;i++){
Document document=searcher.doc(scoreDocs[i].doc);
System.out.println(document.get("name"));
}
searcher.close();
reader.close();
directory.close();
}
}
更多推荐
所有评论(0)