posts - 27,comments - 2,trackbacks - 0
          1,添加索引文件中的一條新的索引     
                          Question addQ = new Question();//新添加的一條數據,對象id在索引文件中沒有
                          addQ.setId("999999999");
                          addQ.setQuestionname("新添加的一條數據名稱");
                          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();
          }
          }
          執行完程序后,索引文件中已經添加新的索引數據。
          2,刪除索引文件中的一條新的索引
                          Question delQ = new Question();//索引文件中有的一條數據,根據對象id刪
                          delQ.setId("1111111");
                          delQ.setQuestionname("要刪除的一條數據");
                          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 魏文甫 閱讀(345) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 吴江市| 安吉县| 张掖市| 湘乡市| 文昌市| 保亭| 郑州市| 景泰县| 铜陵市| 新干县| 宜州市| 平凉市| 陆良县| 临高县| 达孜县| 翁源县| 旺苍县| 大冶市| 醴陵市| 萝北县| 正镶白旗| 永安市| 鹰潭市| 建昌县| 乌鲁木齐县| 青州市| 和平区| 元阳县| 剑河县| 蒙阴县| 滦南县| 叙永县| 鹤岗市| 平南县| 任丘市| 凤台县| 偃师市| 合作市| 北票市| 安义县| 卢氏县|