zoukankan      html  css  js  c++  java
  • Lucene增删改查

    IndexManager.java

    package com.witwicky.lucene;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.commons.io.FileUtils;
    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.Store;
    import org.apache.lucene.document.LongField;
    import org.apache.lucene.document.TextField;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.index.Term;
    import org.apache.lucene.queryparser.classic.QueryParser;
    import org.apache.lucene.search.Query;
    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;
    
    /**
     * 
     * Lucene索引管理
     * 
     * @author Administrator
     *
     */
    public class IndexManager {
    
    	/**
    	 * 创建索引
    	 * 
    	 * @throws Exception
    	 */
    	@Test
    	public void IndexCreate() throws Exception {
    
    		String dataPath = "D:\Lucene\searchsource";
    		String indexPath = "D:\Lucene\dic";
    
    		// 文档集合
    		List<Document> listDocument = new ArrayList<>();
    
    		// 读取文件,创建Document列表
    		File file = new File(dataPath);
    		for (File f : file.listFiles()) {
    			String fileName = f.getName();
    			String fileContent = FileUtils.readFileToString(f);
    			long fileSize = FileUtils.sizeOf(f);
    
    			// 创建文档
    			Document doc = new Document();
    			doc.add(new TextField("fileName", fileName, Store.YES));
    			doc.add(new TextField("fileContent", fileContent, Store.YES));
    			doc.add(new LongField("fileSize", fileSize, Store.YES));
    
    			// 放入文档列表
    			listDocument.add(doc);
    		}
    
    		// 创建分词器
    		// Analyzer analyzer = new StandardAnalyzer();
    		Analyzer analyzer = new IKAnalyzer();
    
    		// 创建写入目录
    		Directory directory = FSDirectory.open(new File(indexPath));
    		// 创建写入索引对象的配置对象
    		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
    		// 创建写索引对象
    		IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
    
    		// 将文档添加入写入索引对象的流中
    		for (Document document : listDocument) {
    			indexWriter.addDocument(document);
    		}
    
    		// 提交
    		indexWriter.commit();
    
    		// 关闭流
    		indexWriter.close();
    	}
    
    	/**
    	 * 删除索引
    	 */
    	@Test
    	public void delIndex() throws Exception {
    		Analyzer analyzer = new IKAnalyzer();
    		// Analyzer analyzer = new StandardAnalyzer();
    
    		Directory directory = FSDirectory.open(new File("D:\Lucene\dic"));
    		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
    		IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
    
    		// 删除所有
    		indexWriter.deleteAll();
    
    		// 根据域删除
    		// 1.
    		// QueryParser queryParser = new QueryParser("fileName", analyzer);
    		// Query query = queryParser.parse("fileName:apache");
    		// indexWriter.deleteDocuments(term);
    
    		// 2.
    		// Term term = new Term("fileName", "java");
    		// indexWriter.deleteDocuments(term);
    
    		indexWriter.commit();
    		indexWriter.close();
    	}
    
    	/**
    	 * 更新索引
    	 * 
    	 * @throws Exception
    	 */
    	@Test
    	public void updateIndex() throws Exception {
    		Analyzer analyzer = new IKAnalyzer();
    		// Analyzer analyzer = new StandardAnalyzer();
    
    		Directory directory = FSDirectory.open(new File("D:\Lucene\dic"));
    		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
    		IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
    
    		Term term = new Term("fileName", "adsf");
    		Document doc = new Document();
    		doc.add(new TextField("fileName", "adsf", Store.YES));
    		doc.add(new TextField("fileContent", "vvvvvvvvvvvvvvv", Store.YES));
    		doc.add(new LongField("fileSize", 200L, Store.YES));
    
    		indexWriter.updateDocument(term, doc);
    
    		indexWriter.commit();
    		indexWriter.close();
    	}
    }
    

      

    IndexSearch.java

    package com.witwicky.lucene;
    
    import java.io.File;
    
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.index.DirectoryReader;
    import org.apache.lucene.index.IndexReader;
    import org.apache.lucene.queryparser.classic.QueryParser;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.TopDocs;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.junit.Test;
    import org.wltea.analyzer.lucene.IKAnalyzer;
    
    public class IndexSearch {
    
    	/**
    	 * 索引搜索
    	 */
    	@Test
    	public void indexSearch() throws Exception {
    		String indexPath = "D:\Lucene\dic";
    
    		// 分词器
    		// Analyzer analyzer = new StandardAnalyzer();
    		Analyzer analyzer = new IKAnalyzer();
    
    		// 查询解析对象
    		QueryParser queryParser = new QueryParser("fileName", analyzer);
    		Query query = queryParser.parse("fileName:apache");
    
    		Directory directory = FSDirectory.open(new File(indexPath));
    		IndexReader indexReader = DirectoryReader.open(directory);
    
    		IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    
    		TopDocs topDocs = indexSearcher.search(query, 10);
    
    		System.out.println("总记录数:" + topDocs.totalHits);
    
    		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
    
    		for (ScoreDoc scoreDoc : scoreDocs) {
    			Document doc = indexSearcher.doc(scoreDoc.doc);
    
    			System.out.println("名称:" + doc.get("fileName"));
    			// System.out.println("内容:" + doc.get("fileContent"));
    			System.out.println("大小:" + doc.get("fileSize"));
    		}
    
    		indexReader.close();
    	}
    }
    

      

  • 相关阅读:
    滑动窗口模板
    交换机命令
    针对织梦程序列表字段内可有可无的显示方法
    dedecms中常见问题修改方法
    redis系列之------字典
    1.InfluxDB-官方测试数据导入
    MYSQL第二课
    centos6.8下hadoop3.1.1完全分布式安装指南
    Mysql—添加用户并授权
    什么是全文检索
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/8243560.html
Copyright © 2011-2022 走看看