posts - 27,comments - 2,trackbacks - 0
          1,添加索引文件中的一條新的索引     
                          Question addQ = new Question();//新添加的一條數(shù)據(jù),對象id在索引文件中沒有
                          addQ.setId("999999999");
                          addQ.setQuestionname("新添加的一條數(shù)據(jù)名稱");
                          Analyzer sa = new SmartChineseAnalyzer(Version.LUCENE_40);
          IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40, sa);
          iwc.setOpenMode(OpenMode.APPEND);
          IndexWriter writer = null;
          try {
          Directory dir1 = FSDirectory.open(new File("F:\\temp"));
          writer = new IndexWriter(dir1, iwc);
          FieldType ft = new FieldType();
          ft.setIndexed(true);
          ft.setStored(true);
          ft.setTokenized(true);
          FieldType ft2 = new FieldType();
          ft2.setIndexed(true);
          ft2.setStored(true);
          ft2.setTokenized(false);
          Document doc = new Document();
          doc.add(new Field("id", addQ.getId(), ft2));
          doc.add(new Field("questionname", addQ.getQuestionname(), ft));
          writer.addDocument(doc);
          writer.close();
          } catch (CorruptIndexException e) {
          e.printStackTrace();
          } catch (LockObtainFailedException e) {
          e.printStackTrace();
          } catch (IOException e) {
          e.printStackTrace();
          } finally{
          try {
          if(writer!=null){
          writer.close();
          }
          if(sa!=null){
          sa.close();
          }
          } catch (CorruptIndexException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          }
          }
          執(zhí)行完程序后,索引文件中已經(jīng)添加新的索引數(shù)據(jù)。
          2,刪除索引文件中的一條新的索引
                          Question delQ = new Question();//索引文件中有的一條數(shù)據(jù),根據(jù)對象id刪
                          delQ.setId("1111111");
                          delQ.setQuestionname("要刪除的一條數(shù)據(jù)");
                          IndexWriter writer = null;
          Analyzer sa = new SmartChineseAnalyzer(Version.LUCENE_40);
          IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40, sa);
          Directory dir1 = null;
          try {
          dir1 = FSDirectory.open(new File("F:\\temp"));
          writer = new IndexWriter(dir1, iwc);
          Term term = new Term("id", delQ.getId());
          writer.deleteDocuments(term);
          writer.commit();
          writer.close();
          } catch (CorruptIndexException e) {
          e.printStackTrace();
          } catch (LockObtainFailedException e) {
          e.printStackTrace();
          } catch (IOException e) {
          e.printStackTrace();
          } finally {
          try {
          if (writer != null) {
          writer.close();
          sa.close();
          }
          } catch (CorruptIndexException e) {
          e.printStackTrace();
          } catch (IOException e) {
          e.printStackTrace();
          }
          }
          System.out.println("索引刪除完成");
          3,更新索引文件中的一條索引
          更新索引文件中的一條索引的理念是:先找到這條索引刪除,然后再添加這條更新后的索引


          posted on 2013-08-16 12:08 魏文甫 閱讀(346) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 元阳县| 肇庆市| 湖北省| 郯城县| 临西县| 阿巴嘎旗| 莆田市| 革吉县| 武义县| 灵川县| 施秉县| 台中市| 两当县| 玛纳斯县| 灵丘县| 溧阳市| 汾阳市| 巴林右旗| 辛集市| 出国| 镇康县| 丘北县| 舟山市| 宜川县| 祁东县| 图们市| 迁西县| 安溪县| 北辰区| 南安市| 筠连县| 新余市| 普安县| 武清区| 五大连池市| 汾西县| 鄄城县| 信阳市| 兰州市| 镇巴县| 塔城市|