zoukankan      html  css  js  c++  java
  • lucene 添删改查代码

    package com.xl.lucene;

    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.document.TextField;
    import org.apache.lucene.index.*;
    import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
    import org.apache.lucene.queryparser.classic.QueryParser;
    import org.apache.lucene.search.*;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    import org.junit.Test;
    import org.wltea.analyzer.lucene.IKAnalyzer;

    import java.io.File;
    import java.io.IOException;

    public class LuceneManager {

    public IndexWriter getIndexWriter() throws Exception {

    Directory directory = FSDirectory.open(new File("E:\WorkSpace\allOtherItem\luceneresult"));
    Analyzer analyzer = new StandardAnalyzer();
    IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
    return new IndexWriter(directory, config);
    }

    // 全删除
    @Test
    public void testAllDelete() throws Exception {
    IndexWriter indexWriter = getIndexWriter();
    indexWriter.deleteAll();
    indexWriter.close();
    }

    // 根据条件
    @Test
    public void testDelete() throws Exception {
    IndexWriter indexWriter = getIndexWriter();
    Query query = new TermQuery(new Term("fileName", "java.txt"));
    indexWriter.deleteDocuments(query);
    indexWriter.close();
    }

    // 修改
    @Test
    public void testUpdate() throws Exception {
    IndexWriter indexWriter = getIndexWriter();
    Document document = new Document();
    document.add(new TextField("fileN", "测试文件名", Field.Store.YES));
    document.add(new TextField("fileN", "测试文件内容", Field.Store.NO));
    indexWriter.updateDocument(new Term("fileName", "java.txt"), document, new IKAnalyzer());
    indexWriter.close();
    }

    // indexReader IndexSearcher
    public IndexSearcher getIndexSearcher() throws IOException {
    //查询所有
    Directory directory = FSDirectory.open(new File("E:\WorkSpace\allOtherItem\luceneresult"));
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    return new IndexSearcher(indexReader);
    }

    // 执行查询结果
    public void printResult(IndexSearcher indexSearcher, Query query) throws Exception {
    TopDocs topDocs = indexSearcher.search(query, 10);

    // 第六步返回查询结果.遍历查询结果并输出
    ScoreDoc[] scoreDocs = topDocs.scoreDocs;
    if (scoreDocs == null) {
    System.out.println("读取失败");
    } else {
    for (ScoreDoc scoreDoc : scoreDocs) {
    int doc = scoreDoc.doc;
    Document document = indexSearcher.doc(doc);
    //文件名称
    String fileName = document.get("fileName");
    //文件内容
    String fileContent = document.get("fileContent");
    //文件大小
    String fileSize = document.get("fileSize");
    //文件路径
    String filePath = document.get("filePath");
    System.out.println(fileName);
    System.out.println(fileContent);
    System.out.println(fileSize);
    System.out.println(filePath);
    System.out.println("------------#----------------------------------------------");

    }
    }
    }

    // 查询所有
    @Test
    public void testMatchAllDocsQuery() throws Exception {
    IndexSearcher indexSearcher = getIndexSearcher();
    Query query = new MatchAllDocsQuery();
    printResult(indexSearcher, query);
    //关闭资源
    indexSearcher.getIndexReader().close();
    }

    // 根据数值范围查询
    @Test
    public void testNumbericRangeQuery() throws Exception {
    IndexSearcher indexSearcher = getIndexSearcher();
    Query query = NumericRangeQuery.newLongRange("fileSize", 0L, 200L, true, true);//NumericRangeQuery.newLongRange("fileSize",0,200,true,true);
    printResult(indexSearcher, query);
    //关闭资源
    indexSearcher.getIndexReader().close();
    }

    // 组合查询
    @Test
    public void testBooleanQuery() throws Exception {
    IndexSearcher indexSearcher = getIndexSearcher();
    BooleanQuery booleanQuery = new BooleanQuery();
    Query query1 = new TermQuery(new Term("fileName", "java.txt"));
    Query query2 = new TermQuery(new Term("fileContent", "good"));
    //Must 是必须有 SHOULD是可以有可以不有 Must_not 是必须没有
    booleanQuery.add(query1, BooleanClause.Occur.SHOULD);

    booleanQuery.add(query2, BooleanClause.Occur.SHOULD);

    printResult(indexSearcher, booleanQuery);
    //关闭资源
    indexSearcher.getIndexReader().close();
    }

    // 条件解析的对象查询
    @Test
    public void testQueryParser() throws Exception {
    IndexSearcher indexSearcher = getIndexSearcher();
    //参数1 默认查询的域,参数2 采用的分析器
    QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer());
    //*:*是查询所有部分域和值
    // Query query = queryParser.parse("*:*");
    // 默认是fileName:java.txt 相当于 Query query = queryParser.parse("fileName:java.txt");
    // Query query = queryParser.parse("java.txt");
    Query query = queryParser.parse("*:*");
    printResult(indexSearcher, query);
    //关闭资源
    indexSearcher.getIndexReader().close();
    }

    // 条件解析的对象查询 多个默认域
    public void testMultiFieldParser() throws Exception {
    IndexSearcher indexSearcher = getIndexSearcher();
    //多个默认域
    String[] fields = {"fileName", "fileContent"};
    MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields, new IKAnalyzer());
    // fileName:java.txt fileName:good fileContent:java.txt fileContent :good
    Query query = queryParser.parse("java.txt good");
    printResult(indexSearcher, query);
    //关闭资源
    indexSearcher.getIndexReader().close();
    }

    }
  • 相关阅读:
    java 类加载与初始化
    字符串匹配
    二分查找
    一般css样式开头公共部分
    js或jquery实现图片轮播
    w3chtml页面和css书写规范
    前端学习网站
    相关学习网站
    char对比varchar
    用抽象类或者接口?
  • 原文地址:https://www.cnblogs.com/lovetl/p/11897834.html
Copyright © 2011-2022 走看看