1.创建一个javabean,用来存放article对象
/*** 1、创建一个article对象,把该对象放入到索引库中* 2、从索引库中把article对象检索出来* @author Think**/public class HelloWorld {/*** 创建索引*/@Testpublic void testCreateIndex() throws Exception{/*** 1、创建一个article对象* 2、把article对象转化成document对象* 3、把该document对象放入到索引库中*/Article article = new Article();article.setId(1L);article.setTitle("lucene是一个全文检索引擎");article.setContent("baidu,google是很好的搜索引擎");/*** 第一个参数* 放入到索引库里面的名称* 第二个参数* 该名称对应的值*//*** 把对象放入到document中* 1、把对象中的属性封装在Field中* 2、再把field放入到Document中* 3、 Store.YES往内容库中存储* Store.NO 不往内容库中存储* 4、 Index.NO 不往目录库中存储* Index.NOT_ANALYZED 往目录库中存储,但是不分词* 例如:存储"lucene是全文检索引擎",那么直接存储这句话* Index.ANALYZED 往目录库中存储,并且分词存储* 例如:存储"lucene是全文检索引擎" 存储的内容:"lucene" "全文检索" "引擎"*/Field idFiled = new Field("id", article.getId().toString(), Store.YES, Index.NOT_ANALYZED);Field titleFiled = new Field("title", article.getTitle().toString(),Store.YES,Index.ANALYZED);Field contentFiled = new Field("content", article.getContent(),Store.YES,Index.ANALYZED);Document document = new Document();document.add(idFiled);document.add(titleFiled);document.add(contentFiled);/*** 创建一个索引库*///指向索引库的位置Directory directory = FSDirectory.open(new File("./indexDir"));//因为要把title和content中的内容拆分成关键词,所以应用有分词器Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);//MaxFieldLength.LIMITED 该字段在索引库中能够存储的最大的长度/*** 创建一个IndexWriter*/IndexWriter indexWriter = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);indexWriter.addDocument(document);indexWriter.close();}/*** 对索引进行检索*/@Testpublic void testSearchIndex() throws Exception{Directory directory = FSDirectory.open(new File("./indexDir"));/*** 创建indexSearch检索器* 参数为指明索引库的位置*/IndexSearcher indexSearcher = new IndexSearcher(directory);Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);/*** 第一个参数* 版本号* 第二个参数* 在哪个字段中进行检索* 第三个参数* 分词器 对检索的词进行分词*///QueryParser queryParser = new QueryParser(Version.LUCENE_30,"title",analyzer);QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30,new String[]{"title","content"},analyzer);/*** 参数为关键词*/Query query = queryParser.parse("baidu");/*** query中包含有关键词* 提取出前N条记录*///TopDocs=Top DocumentsTopDocs topDocs = indexSearcher.search(query, 1);//根据关键词得到的总的记录数int count = topDocs.totalHits;/*** 每一个scoreDoc是由关键词和下标组成的*/ScoreDoc[] scoreDocs = topDocs.scoreDocs;List<Article> articles = new ArrayList<Article>();for(int i=0;i<scoreDocs.length;i++){/*** 得到关键词所对应的下标*/int index = scoreDocs[i].doc;/*** 根据下标得到相应的document*/Document document = indexSearcher.doc(index);/*** 由document转化成article*/Article article = new Article();article.setId(Long.parseLong(document.get("id")));article.setTitle(document.get("title"));article.setContent(document.get("content"));articles.add(article);}for(Article article:articles){System.out.println(article.getId());System.out.println(article.getTitle());System.out.println(article.getContent());}}}