lucene_03_索引的增刪改查

lucene索引的添加見 http://www.cnblogs.com/getchen/p/8615276.html 入門代碼。html

 

公共代碼java

  public <T extends Query> void printResult(Query query) throws Exception{
        IndexSearcher indexSearcher = getIndexSearcher();

        TopDocs topDocs = indexSearcher.search(query, 10);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        int i=0;
        for (ScoreDoc doc : scoreDocs) {
            int docIndex = doc.doc;
            Document document = indexSearcher.doc(docIndex);
            String fileName = document.get("fileName");
            System.out.println(fileName);
            String fileSize = document.get("fileSize");
            System.out.println("size:=="+fileSize);
            String filePath = document.get("filePath");
            System.out.println(filePath);
            String fileContent = document.get("fileContent");
            System.out.println(fileContent);
            System.out.println("==========="+ ++i +"===============");
        }
        indexSearcher.getIndexReader().close();
    }




 public IndexSearcher getIndexSearcher() throws Exception{
      //  第一步: 建立一個Directory 對象,也就是索引庫存放的位置。
        Directory directory = FSDirectory.open(new File("F:\\lucene\\indexDatabase"));
//        第二步: 建立一個indexReader 對象,須要指定Directory 對象。
        IndexReader indexReader = DirectoryReader.open(directory);
//        第三步: 建立一個indexsearcher 對象,須要指定InclexReader 對象。
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        return indexSearcher;
    }

 

lucene索引的查詢數據庫

查詢所有spa

 @Test
    public void queryAll() throws Exception{
        Query query = new MatchAllDocsQuery();
        printResult(query);


    }

區間查詢code

    @Test
    public void queryRange() throws Exception{
        Query query =  NumericRangeQuery.newLongRange("fileSize",0L,800L,true,true);
        printResult(query);


    }

組合條件查詢htm

    /**
     * 組合條件查詢
     */
    @Test
    public void queryBoolean() throws Exception{
        BooleanQuery query = new BooleanQuery();
        TermQuery query1 = new TermQuery(new Term("fileName", "java"));
        TermQuery query2 = new TermQuery(new Term("fileName", "lucene"));
        query.add(query1, BooleanClause.Occur.MUST);
        query.add(query2, BooleanClause.Occur.MUST);

//        Query query3 = query;
        printResult(query);



    }

其中BooleanClause.Occur 中有三個選項:MUST,NOT_MUST,SHOULD.等同於數據庫中的and,not,or對象


lucene索引的添加
//
    @Test
    public void add() throws  Exception{
        IndexWriter indexWriter = getIndexWriter();
        Document document = new Document();
        document.add(new TextField("fileContent","電話號碼", Field.Store.YES));
        indexWriter.addDocument(document);
        indexWriter.close();
    }

lucene 索引的更新blog

 /**
     * 更新:刪除原有的並增長鬚要更改的。
     * 刪掉一個添加一個
     * @throws Exception
     */
    @Test
    public void update() throws Exception{
        IndexWriter indexWriter = getIndexWriter();
        Term term = new Term("fileContent", "電話號碼");
      /*  Document document = new Document();
        document.add(new TextField("fileContent","電話號碼1", Field.Store.YES));*/
        Document document = getDocumentByFile(new File("F:\\lucene\\test.txt"));
        indexWriter.updateDocument(term,document,new IKAnalyzer());
        indexWriter.close();
    }

    public Document getDocumentByFile(File file) throws Exception{
        Document document = new Document();
        //獲取文件的名稱
        String fileName = file.getName();
        //建立textfield,保存文件名(key,value,是否存儲)
        TextField fileNameField = new TextField("fileName",fileName, Field.Store.YES);
        //文件大小
        long fileSize = FileUtils.sizeOf(file);

//                NumericDocValuesField fileSizeField = new NumericDocValuesField("fileSize", fileSize);
        System.out.println(fileSize);

//                SortedNumericDocValuesField fileSizeField = new SortedNumericDocValuesField("fileSize", fileSize);
//                LongPoint fileSizeField = new LongPoint("fileSize", fileSize);
        StringField fileSizeField = new StringField("fileSize", String.valueOf(fileSize), Field.Store.YES);
        //文件路徑
        String filePath = file.getPath();
        StoredField filePathField = new StoredField("filePath", filePath);
        //文件內容
        String fileContent = FileUtils.readFileToString(file,"gbk");
        TextField fileContentField = new TextField("fileContent", fileContent, Field.Store.YES);
        document.add(fileNameField);
        document.add(fileSizeField);
        document.add(filePathField);
        document.add(fileContentField);
        return document;
    }

lucene索引的刪除索引

 

  //刪除所有
    @Test
    public  void deleteAll() throws Exception{
        IndexWriter indexWriter = getIndexWriter();
        indexWriter.deleteAll();
        indexWriter.close();

    }

    //刪除部分
    @Test
    public void delete() throws  Exception{
        IndexWriter indexWriter = getIndexWriter();
        Term term = new Term("fileContent", "電話號碼"); //索引文件中包含的有電話號碼,分詞器也分析出了電話號碼一詞,刪除以後就沒法經過該關鍵詞索引到文件了。
        indexWriter.deleteDocuments(term);
        indexWriter.close();
    }
相關文章
相關標籤/搜索