一、lucene是什么?
最受欢迎的java开源全文搜索引擎开发工具包。 提供了完整的查询引擎和索引引擎, 部分文本分词引擎。 lucene的目的是为软件开发人员提供一个简单易用的工具包, 以方便在目标系统中实现全文检索功能, 或者是以此为基础建立起完整的全文检索引擎。
二、lucene代码示例:
package com.javaxiaobang.es.lucene;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.IOUtils;
import com.stant.EsConstants;
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.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
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 java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Set;/*** 功 能:lucene* 作 者:java潇邦* 时 间:2020/5/4*/
public class LuceneMain {/*** 源码地址:*/public static void main(String[] args) {//1、创建索引createIndex(EsConstants.INDEX_DATA_DIR);//2、添加索引文档addIndexDoc(EsConstants.INDEX_DATA_DIR, EsConstants.JSON_CONTENT);//3、查询内容query(EsConstants.INDEX_DATA_DIR, "歌手","杰伦");}/*** 创建索引** @param indexDir 索引存放位置*/public static void createIndex(String indexDir) {IndexWriter writer = null;try {//获取目录Directory directory = FSDirectory.(indexDir));//设置分词器Analyzer analyzer = new StandardAnalyzer();//准备configIndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);//创建lucene实例writer = new IndexWriter(directory, indexWriterConfig);System.out.println("索引创建成功");} catch (Exception e) {e.printStackTrace();} finally {IOUtils.close(writer);}}/*** 添加索引文档** @param indexDir 索引存放位置* @param jsonContent json内容*/public static void addIndexDoc(String indexDir, String jsonContent) {IndexWriter writer = null;try {//获取目录Directory directory = FSDirectory.(indexDir));//设置分词器Analyzer analyzer = new StandardAnalyzer();//准备configIndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);//创建lucene实例writer = new IndexWriter(directory, indexWriterConfig);Document document = jsonToDoc(jsonContent);writer.addDocument(document);System.out.println("索引文档添加成功");} catch (Exception e) {e.printStackTrace();} finally {IOUtils.close(writer);}}/*** json内容转document文档** @param jsonContent json内容* @return*/public static Document jsonToDoc(String jsonContent) {Document document = new Document();JSONObject jsonObj = JSONObject.parseObject(jsonContent);Set<Map.Entry<String, Object>> entrySet = Set();for (Map.Entry<String, Object> entry : entrySet) {document.add(new Key(), Value() == null ? "" : Value().toString(), Field.Store.YES));}return document;}/*** 查询文档* @param indexDir 索引存放位置* @param queryParam 查询条件* @param queryContent 查询单词内容* @return*/public static String query(String indexDir, String queryParam, String queryContent) {StringBuilder result = new StringBuilder();IndexReader reader = null;try {//获取目录Directory directory = FSDirectory.((indexDir)));//获取readerreader = DirectoryReader.open(directory);//获取索引实例IndexSearcher searcher = new IndexSearcher(reader);//设置分词器Analyzer analyzer = new StandardAnalyzer();//创建解析器QueryParser queryParser = new QueryParser(queryParam, analyzer);Query query = queryParser.parse(queryContent);TopDocs topDocs = searcher.search(query, 10);System.out.println("topDocs内容:" + JSONString(topDocs));for (ScoreDoc scoreDoc : topDocs.scoreDocs) {//拿到文档实例Document document = searcher.doc(scoreDoc.doc);//获取所有文档字段List<IndexableField> fieldList = Fields();//处理文档字段for (IndexableField field:fieldList){result.append(field.name());result.append(":");result.append(field.stringValue());result.append(",rn");}}System.out.println("查询结果:"+result);} catch (Exception e) {e.printStackTrace();}finally {IOUtils.close(reader);}String();}}
三、源码地址:
本文发布于:2024-01-31 06:25:32,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170665353526205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |