具体文件组成:
具体文件的代码:
package lucene;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import java.io.File;
public class lucene {
String path="D:/workforce/lucene/hello"; //创建文件路径
Version version=Version.LUCENE_4_10_4; //创建Lucene的版本
//设置内容
String content1="走好选择的路,别选择好走的路,你才能拥有真正的自己。";
String content2="惟有身处卑微的人,最有机缘看到世态人情的真相。一个人不想攀高就不怕下跌,也不用倾轧排挤,可以保其天真,成其自然,潜心一志完成自己能做的事。";
String content3="我甘心当个“零”,人家不把我当个东西,我正好可以把看不起我的人看个透 ";
//Lucene的录入
@Test
public void testLucene() throws Exception{
//1.定义Lucene存放文件的位置
Directory directory= FSDirectory.open(new File(path));
//2.配置分词对象
Analyzer analyzer=new StandardAnalyzer();
//3.配置对象
IndexWriterConfig config=new IndexWriterConfig(version,analyzer);
IndexWriter writer=new IndexWriter(directory,config);
//4.往库里写入内容
FieldType type=new FieldType();
type.setStored(true);
type.setIndexed(true);
//创建文档对象
Document doc=new Document();
doc.add(new Field("title","doc1",type));
doc.add(new Field("content",content1,type));
writer.addDocument(doc);
Document doc2=new Document();
doc2.add(new Field("title","doc2",type));
doc2.add(new Field("content",content2,type));
writer.addDocument(doc2);
Document doc3=new Document();
doc3.add(new Field("title","doc3",type));
doc3.add(new Field("content",content3,type));
writer.addDocument(doc3);
//5.提交资源
//6.关闭资源
writer.close();
}
//Lucene的查询
@Test
public void testSearch()throws Exception{
//1.定义Lucene存放文件的位置
Directory directory=FSDirectory.open(new File(path));
//2.创建Reader
IndexReader reader= DirectoryReader.open(directory);
//3.创建读取对象
IndexSearcher searcher=new IndexSearcher(reader);
//第一个参数表示:在哪个字段查询内容
//第二参数:分词对象
Analyzer analyzer=new StandardAnalyzer();
QueryParser parser=new QueryParser("content",analyzer);
Query query=parser.parse("人");
//第二个参数表示符合条件的前n条记录
TopDocs tds=searcher.search(query,10000);
System.out.println("总共命中次数: "+tds.totalHits);
ScoreDoc[] scoreDocs=tds.scoreDocs;
ScoreDoc scoredoc=null;
Document doc=null;
for (int i=0;i<scoreDocs.length;i++){
scoredoc=scoreDocs[i];
System.out.println("文档分数:"+scoredoc.score);
System.out.println("文档编号:"+scoredoc.doc);
doc=searcher.doc(scoredoc.doc); //根据编号获取文档
System.out.println("title>>>>>>>>>>>>>"+doc.get("title")); //获取标题
System.out.println("content>>>>>>>>>>>"+doc.get("content"));//获取文档内容
System.out.println("-------------------------");
}
}
}
结果如下: