码迷,mamicode.com
首页 > Web开发 > 详细

lucene_03_索引的增删改查

时间:2018-04-02 11:14:04      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:log   search   core   http   blog   sort   ldo   store   index   

lucene索引的添加见 http://www.cnblogs.com/getchen/p/8615276.html 入门代码。

 

公共代码

  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索引的查询

查询全部

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


    }

区间查询

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


    }

组合条件查询

    /**
     * 组合条件查询
     */
    @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 索引的更新

 /**
     * 更新:删除原有的并增加需要更改的。
     * 删掉一个添加一个
     * @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();
    }

 

lucene_03_索引的增删改查

标签:log   search   core   http   blog   sort   ldo   store   index   

原文地址:https://www.cnblogs.com/getchen/p/8690153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!