您的位置:首页 > 其它

Lucene1.索引部分

2015-09-18 14:57 239 查看
1.索引部分

~1.索引创建部分

---1.创建Directory 通过Directory的创建指定索引存放位置

  Directory directory=new RAMDirectory();//创建到内存-速度快,不能持久化

  Directory directory=FSDirectory.open(new File("F:/Lucene_jar/mytestindex"));//创建到硬盘

---2.创建IndexWriter 通过IndexWriterConfig的创建指定索引版本和语言词汇分析器

  //Version.LUCENE_35表示版本的标号   StandardAnalyzer是标准分词器

  IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35));

---3.IndexWriter它的作用是用来写索引文件 

  IndexWriter indexWriter=new IndexWriter(directory, iwc);

---4.创建Docement对象

  Document document=null;

  document=new Document();

  /**

    * 1.Filed.Store.YES或者NO(存储域选项)

   yes表示把这个域的内容完全存储到文件中,方便进行文本的还原

   no表示把这个域的内容不存储到文件中,但是可以被索引,此时的内容无法完全还原(docget(""))

   2.Filed.Index(索引选项)

   Filed.Index.ANALYZED:进行分词和索引,适应标题,内容等

   Index.NOT_ANALYZED:进行索引,但是不进行分词,如果身份证号,姓名,ID等,适用精确搜索

   Index.ANALYZED_NOT_NORMS:进行分词但是不存储norms信息,这个norms中包括了创建索引时间和权值等信息

   Index.NOT_ANALYZED_NOT_NORMS:既不分词也不存储norms信息

   Index.NO:不进行索引

   */

  document.add(new Field("content",new FileReader(file)));

  document.add(new Field("filename",file.getName(),Store.YES,Index.NOT_ANALYZED));//不分词

  document.add(new Field("path",file.getAbsolutePath(),Store.YES,Index.NOT_ANALYZED));

  

---5.通过IndexWriter增添文档到索引中  它的作用是用来写索引文件

  indexWriter.addDocument(document);

---6.关闭IndexWriter

  if (indexWriter!=null) {

    indexWriter.close();

   }

==================================================================================

~2.索引搜索部分

---1.创建Directory

  Directory directory=FSDirectory.open(new File("F:/Lucene_jar/mytestindex"));

---2.创建IndexReader

  IndexReader indexReader=IndexReader.open(directory);

---3.根据IndexReader创建IndexSearcher

  IndexSearcher indexSearcher=new IndexSearcher(indexReader);

---4.创建搜索的parser

  QueryParser parser=new QueryParser(Version.LUCENE_35,"content",new StandardAnalyzer(Version.LUCENE_35));

---5.创建搜索的Query 索引存储lmandlyp520的doc

  Query query=parser.parse("lmandlyp520"); 

---6.根据searcher搜索并返回TopDocs  返回10条

  TopDocs topDocs=indexSearcher.search(query, 10);

---7.根据TopDocs获取ScoreDoc对象获取具体的document

  ScoreDoc[] sds=topDocs.scoreDocs;

---8.得到需要的值

  for (ScoreDoc scoreDoc : sds) {

    //7.根据searcher和ScoreDoc对象获取具体Document对象

    Document d=indexSearcher.doc(scoreDoc.doc);

    //8.根据document对象获取需要的值

    System.out.println(d.get("filename")+"["+d.get("path")+"]");

   }

---9.关闭reader

  indexReader.close();

---10.常见的属性

  System.out.println("maxDoc"+indexReader.maxDoc());

  System.out.println("numDocs"+indexReader.numDocs());

  //删除的索引

  System.out.println("numDeletedDocs"+indexReader.numDeletedDocs());

==================================================================================

~3.删除索引

---1.创建Directory

  Directory directory=FSDirectory.open(new File("F:/Lucene_jar/mytestindex"));//创建到硬盘

---2.创建IndexWriter

  IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35));

---3.创建IndexWriter

  IndexWriter indexWriter=new IndexWriter(directory, iwc);

---4.删除索引

  //这种删除不会完全删除,会在系统中有缓冲   可以删除所有的索引  还可以根据一个query来删除

  //indexWriter.deleteDocuments(new Term("content", "java"));

  

  //完全删除,包括缓存的删除临时文件

  //删除所有索引

  indexWriter.deleteAll();

  //强制删去缓存上的索引

  indexWriter.forceMergeDeletes();

---5.关闭IndexWriter

  indexWriter.close();

==================================================================================

~4.恢复索引

---1.创建Directory

  Directory directory=FSDirectory.open(new File("F:/Lucene_jar/mytestindex"));

---2.创建IndexReader  false 表示不是只读,可以进行操作

  IndexReader indexReader=IndexReader.open(directory, false);

---3.恢复删除的索引文件

  indexReader.undeleteAll();

---4.关闭reader

  indexReader.close();

==================================================================================

~5.更新索引

---1.创建Directory

  Directory directory=FSDirectory.open(new File("F:/Lucene_jar/mytestindex"));//创建到硬盘

---2.创建IndexWriter

  IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35));

---3.创建IndexWriter

  IndexWriter indexWriter=new IndexWriter(directory, iwc);

---4.创建Docement对象  

  Document document=new Document();

  File f=new File("F:/Lucene_jar/mytest/java.txt");

  document.add(new Field("content",new FileReader("F:/Lucene_jar/mytest/java.txt")));

  document.add(new Field("filename",f.getName(),Store.YES,Index.NOT_ANALYZED));//不分词

  document.add(new Field("path",f.getAbsolutePath(),Store.YES,Index.NOT_ANALYZED));

---5.更新索引  本质是先删除后在增添一个

  indexWriter.updateDocument(new Term("filename","java.txt"),document);

---6.关闭IndexWriter 

  indexWriter.close();

==================================================================================

~索引中特殊部分

---1.数字格式和时间格式的处理  

  //数字格式的  true表示索引

  document.add(new NumericField("age",Store.YES,true).setIntValue(ages[i]));

  //时间格式的

  document.add(new NumericField("date",Store.YES,true).setLongValue(dates[i].getTime()));

---2.进行对权值的操作

  if(document.get("name").contains("@my")){

     document.setBoost(1.5f);

    }else{

     document.setBoost(0.5f);

    }

---3.InderReader单例设计

 //IndexReader别在方法中关掉

 //IndexWriter这里需要提交

 private static IndexReader reader=null;

 private IndexReader IndexReaderUtil(Directory directory){

  try {

   if(reader!=null){

    //有reader 返回新的reader

    IndexReader rr=IndexReader.openIfChanged(reader);

    if(rr!=null){

     reader.close();

     reader=rr;

    }

   }else{

    //没有reader

    reader=IndexReader.open(directory);

   }

   

   return reader;

  } catch (CorruptIndexException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  return null;

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: