ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

educoder Lucene - 全文检索入门

2022-01-29 11:02:34  阅读:170  来源: 互联网

标签:lucene educoder new Lucene 全文检索 org apache import document


第1关:使用lucene创建索引

package step1;

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

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;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class WriterIndex {
	
	
	
	//创建索引库
	public static void createIndex() throws IOException{
		/********** Begin **********/
		//创建索引库
        Directory dir = FSDirectory.open(new File("/temp/doc/1101/index"));
        //创建标准分析器
        Analyzer analyzer = new StandardAnalyzer();
        //创建indexwriterConfig对象
        //第一个参数:lucene的版本信息,可以选择对应的lucene版本也可以使用LATEST
        //第二个参数:分析器对象
        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
        //创建indexwriter对象
        IndexWriter index = new IndexWriter(dir,config);
        //原始文档的路径
        File resource = new File("source/searchsource");
        for (File f : resource.listFiles()) {
            //文件名
            String fileName = f.getName();
            //文件内容
            String fileContent = FileUtils.readFileToString(f);
            //文件路径
            String filePath = f.getPath();
            //文件大小
            long fileSize = FileUtils.sizeOf(f);
            //创建文件名域
            //第一个参数:域的名称
            //第二个参数:域的内容
            //第三个参数:是否存储
            Field fileNameField = new TextField("filename", fileName, Store.YES);
            //文件内容域
            Field fileContentField = new TextField("content", fileContent, Store.YES);
            //文件路径域(不分析、不索引、只存储)
            Field filePathField = new StoredField("path", filePath);
            //文件大小域
            Field fileSizeField = new LongField("size", fileSize, Store.YES);
            //创建document对象
            Document document = new Document();
            //添加field
            document.add(fileNameField);
            document.add(fileContentField);
            document.add(filePathField);
            document.add(fileSizeField);
            index.addDocument(document);
        }
        //关闭indexwriter
        index.close();












		/********** End **********/
	}
	
}





第2关:查询索引

package step2;

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

import javax.sql.rowset.serial.SerialArray;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class SearchIndex {

	
	public static void searchIndex() throws IOException{
		/********** Begin **********/
Directory directory = FSDirectory.open(new File("/temp/doc/1101/index"));
IndexReader reader = DirectoryReader.open(directory);
//创建indexsearcher对象
IndexSearcher searcher = new IndexSearcher(reader);
//创建查询
Query query = new TermQuery(new Term("content","mybatis"));
//执行查询
//第一个参数是查询对象,第二个参数是查询结果返回的最大值
TopDocs topDocs = searcher.search(query, 10);
//查询结束的总条数
System.out.println("查询结果的总条数:" + topDocs.totalHits);
//遍历查询结果
//topDocs.scoreDocs 存储了document对象的id
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
//sourceDoc.doc属性就是document对象的Id
//根据document的id找到document对象的id
//根据id获取document对象
Document document = searcher.doc(scoreDoc.doc);
//System.out.println(document.get("filename"));
System.out.println(document.get("path"));
System.out.println(document.get("content"));
}
reader.close();

		/********** End **********/
	}
	
}

第3关:分词器的使用

package step3;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;

public class AnalyzerTest {
	public static void main(String[] args) throws IOException {
		/********** Begin **********/
		    //创建一个标准分析器对象
    Analyzer analyzer = new CJKAnalyzer();
    //获得tokenStream对象
    //第一个参数:域名,可以随便给一个
    //第二个参数:要分析的文本内容
    TokenStream tokenStream = analyzer.tokenStream("test", "我喜欢在Educoder上学习");
    //添加一个引用,可以获得每个关键词
    CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
    //添加一个偏移量的引用,记录了关键词的开始位置以及结束位置
    OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
    //将指针调整到列表的头部
    tokenStream.reset();
    //遍历关键词列表,通过incrementToken方法判断列表是否结束
    while(tokenStream.incrementToken()) {
    //关键词的起始位置
    System.out.println("start->" + offsetAttribute.startOffset());
    //取关键词
    System.out.println(charTermAttribute);
    //结束位置
    System.out.println("end->" + offsetAttribute.endOffset());
    }
    tokenStream.close();
    
		
		
		
		
		
		/********** End **********/
	}
}

标签:lucene,educoder,new,Lucene,全文检索,org,apache,import,document
来源: https://blog.csdn.net/weixin_43833868/article/details/122740544

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有