首页 > 其他分享 >lucene入门实例一(写索引)

lucene入门实例一(写索引)

时间:2023-04-20 16:05:08浏览次数:29  
标签:入门 Field import lucene 索引 new apache org


copy一个 lucene in action 的入门实例代码:

 

 

 

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldSelectorResult;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Index {
	public static void main(String[] args) throws Exception {
		String indexDir = "F:\\workspace\\JavaDemo\\src\\com\\s\\lucene\\index";//存储lucene索引的文件夹
		String dataDir = "F:\\workspace\\JavaDemo\\src\\com\\s\\lucene";//lucene源文件
		
		long start = System.currentTimeMillis();
		Index indexer = new Index(indexDir);//初始化indexWriter
		int numIndexed;
		try{
			numIndexed = indexer.index(dataDir, new TextFilesFilter());//创建索引
		}finally{
			indexer.close();
		}
		
		long end = System.currentTimeMillis();
		System.out.println(end-start);
	}
	
	private IndexWriter writer;
	
	public Index(String indexDir) throws IOException{
		Directory dir = FSDirectory.open(new File(indexDir));//创建索引文件夹
		//创建索引writer 也可以根据IndexWriterConfig创建
		writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED);
	}
	
	public void close() throws IOException{
		writer.close();
	}
	
	public int index(String dataDir,FileFilter filter) throws Exception{
		File[] files = new File(dataDir).listFiles();//列出源文件
		
		for (File f : files) {
			if(!f.isDirectory() && filter.accept(f)){
				indexFile(f);//遍历源文件,创建索引
			}
		}
		return writer.numDocs();
	}
	
	public static class TextFilesFilter implements FileFilter{
		public boolean accept(File path){
			return path.getName().toLowerCase().endsWith(".txt");
		}
	}
	
	protected Document getDocument(File f) throws Exception{
		Document doc = new Document();
		doc.add(new Field("contents",new FileReader(f)));//内容
		doc.add(new Field("filename",f.getName(),
				Field.Store.YES,Field.Index.NOT_ANALYZED));//文件名
		doc.add(new Field("fullpath",f.getCanonicalPath(),
				Field.Store.YES,Field.Index.NOT_ANALYZED)); //文件全路径
		return doc;
	}
	
	private void indexFile(File f) throws Exception{
		Document doc = getDocument(f);//创建document
		writer.addDocument(doc);//添加document到索引
	}
	
	
}

标签:入门,Field,import,lucene,索引,new,apache,org
From: https://blog.51cto.com/u_2465818/6209761

相关文章

  • jdbc 报错 - 索引中丢失 IN 或 OUT 参数:
    jdbc报错-索引中丢失 IN或OUT参数:通常产生这种异常,是因为语句参数类型不一致所导致,如preparedStatement中的参数本应该是int/integer类型,但是设置参数是setString(1,String.valueof(xxx));或是现在流行的hibernate和ibatis的参数类型配置有问题,Integer配置为varchar2了。......
  • 5 04 | 深入浅出索引(上)
    提到数据库索引,我想你并不陌生,在日常工作中会经常接触到。比如某一个SQL查询比较慢,分析完原因之后,你可能就会说“给某个字段加个索引吧”之类的解决方案。但到底什么是索引,索引又是如何工作的呢?今天就让我们一起来聊聊这个话题吧。数据库索引的内容比较多,我分成了上下两篇文章。......
  • 深度学习--PyTorch定义Tensor以及索引和切片
    深度学习--PyTorch定义Tensor一、创建Tensor1.1未初始化的方法​ 这些方法只是开辟了空间,所附的初始值(非常大,非常小,0),后面还需要我们进行数据的存入。torch.empty():返回一个没有初始化的Tensor,默认是FloatTensor类型。#torch.empty(d1,d2,d3)函数输入的是shapetorch.empty......
  • 6 05 | 深入浅出索引(下)
    在上一篇文章中,我和你介绍了InnoDB索引的数据结构模型,今天我们再继续聊聊跟MySQL索引有关的概念。在开始这篇文章之前,我们先来看一下这个问题:在下面这个表T中,如果我执行select*fromTwherekbetween3and5,需要执行几次树的搜索操作,会扫描多少行?下面是这个表的初始化语句......
  • linq的妙用 分组 交换索引
    //////Splitsacollectionofobjectsintonpageswithan(forexample,ifIhavealistof45shoesandsay'shoes.Split(5)'Iwillnowhave4pagesof10shoesand1pageof5shoes.//////Thetypeofobjectthecollectionshouldcontain.......
  • 搜索引擎基础语法
     搜索语法大全1.intitle搜索范围限定在网页标题上面网页标题通常是对网页内容提纲挈领式的归纳。把查询内容范围限定在网页标题中,有时能获得意想不到的结果语法结构:内容+空格intitle:你要查找的信息(此信息会被限定在网页标题内)例如:web学习intitle:安全注意:intitle:和后......
  • 15-Sass入门
    title:15-Sass入门publish:trueSass简介大家都知道,js中可以自定义变量,css仅仅是一个标记语言,不是编程语言,因此不可以自定义变量、不可以引用等等。面对这些问题,我们现在来引入Sass,简单的说,他是css的升级版,可以自定义变量,可以有if语句,还可以嵌套等等,很神奇吧!那下面......
  • JSP自定义标签开发入门
    评:简单深入赞一般情况下开发jsp自定义标签需要引用以下两个包 importjavax.servlet.jsp.*;importjavax.servlet.jsp.tagext.*; 首先我们需要大致了解开发自定义标签所涉及到的接口与类的层次结构(其中SimpleTag接口与SimpleTagSupport类是JSP2.0中新引入的)。 目标1:自......
  • Spring Aop的学习:Spring Aop的简单入门
    1.什么是AOPAOP(AspectOrientedProgramming):面向切面编程,是OOP(面向对象编程)的一个延续,其和OOP一样,也是一种编程思想。不过AOP是一种横向开发模式。 2.AOP的作用及应用场景作用AOP的主要作用就是减少代码量,提高代码的可重用性,有利于未来的可操作性与可维护性。主要操......
  • 小米AIoT SRE龚同学入职阅博笔记——SRE入门
    为了让团队同学对SRE有个统一的认识,有一些共同的套路和章法,尽量避免在工作中产生价值观和工作思路的矛盾,我一般会让新入职的同学读一下《入职必读》的几篇博客,1是提前对我们有个了解,2是告诉他们我们这的SRE要做什么和怎么做,3是便于入职后快速融入工作、团队,减少矛盾提高协作效率,最......