星空网 > 软件开发 > Java

Luence

Luence

Apache软件基金会的一个项目,是一个开发源码的全文检索引擎工具包,是一个全文检索引擎的一个架构。提供了完成的查询引擎和检索引擎,部分文本分析引擎。

全文检索程序库,虽然与搜索引擎相关,但是不能混淆。

 

官方网址:https://lucene.apache.org/

帮助文档:https://lucene.apache.org/core/4_9_1/index.html

 

官方解释:

Lucene is a Java full-text search engine. Lucene is not a complete application, but rather a code library and API that can easily be used to add search capabilities to applications.

 

倒排索引

了解Luence要知道倒排索引;

通俗解释,我们通常都是通过查找文件位置及文件名,再查找文件的内容。倒排索引可以理解为通过文件内容来查找文件位置及文件名的。

倒排索引是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射。它是文档检索系统中最常用的数据结构。通过倒排索引,可以根据单词快速获取包含这个单词的文档列表。

倒排索引也是lucence的索引核心。

 

文件内容可以表示一个field,文件名称可以表示一个field,将整个field进行分词,然后根据分词创建索引,建立一个个term;

如:文件的内容作为一个field,命名为"contents",将文件内容进行分词,假设文件内容为"A B",分词结果为"A","B",这样term的信息就为两条,field的内容为"contents",term对应的文本内容分别为"A"和"B"。

当查找指定分词的时候就可以获取这个分词所在的doc,并获取doc相关的信息。

 

demo编程

例子参考官方demo; package位置:org.apache.lucene.demo

 

自己写了一个demo

MyIndexFiles.java

 1 import org.apache.commons.io.FileUtils; 2 import org.apache.lucene.analysis.Analyzer; 3 import org.apache.lucene.analysis.standard.StandardAnalyzer; 4 import org.apache.lucene.document.*; 5 import org.apache.lucene.index.*; 6 import org.apache.lucene.queryparser.classic.ParseException; 7 import org.apache.lucene.queryparser.classic.QueryParser; 8 import org.apache.lucene.search.IndexSearcher; 9 import org.apache.lucene.search.Query; 10 import org.apache.lucene.search.ScoreDoc; 11 import org.apache.lucene.search.TopDocs; 12 import org.apache.lucene.store.Directory; 13 import org.apache.lucene.store.FSDirectory; 14 import org.apache.lucene.util.Version; 15 import org.junit.Test; 16  17 import java.io.File; 18 import java.io.IOException; 19  20 /** 21  * Created by Edward on 2016/7/25. 22 */ 23 public class MyIndexFiles { 24  25  26   public static void main(String[] args) throws IOException { 27  28     //文件方式存储索引文件 29     FSDirectory directory = FSDirectory.open(new File("D:\\documents\\Lucene\\MyDemo\\index")); 30  31     //文本解析器,分词器 32     Analyzer analyzer= new StandardAnalyzer(Version.LUCENE_4_9); 33  34     //索引写配置,要指定解析器及版本信息 35     IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_9, analyzer); 36  37     //创建写索引 38     IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig ); 39  40     //路径 41     File path = new File("D:\\documents\\Lucene\\MyDemo\\docs"); 42     //文件列表 43     File[] listFile = path.listFiles(); 44     for(File file: listFile){ 45       //创建doc 46       Document doc = new Document(); 47  48       //获取文件属性信息 49       String filename = file.getName(); 50       long lastModified = file.lastModified(); 51  52       //通过commons-io-2.4.jar包中的FileUtils方法,读文件内容转化为String 53       String readFile2Sting = FileUtils.readFileToString(file); 54  55       //将field添加到doc 56       //StringField不进行分词,当做一个分词 57       //Field的有索引和存储属性, 58         //Field.Store.NO代表数据不进行存储,仅能索引到,多用来处理文本内容,可获取文件名然后通过文件位置打开文件获取内容 59         //Field.Store.YES代表存储数据,通常用来直接获取文件路径 60       doc.add(new StringField("filename", filename, Field.Store.YES)); 61       doc.add(new LongField("modify", lastModified, Field.Store.YES)); 62       doc.add(new TextField("contents",readFile2Sting, Field.Store.NO)); 63  64       //新增的方式 65       //indexWriter.addDocument(doc); 66  67       //更新的方式, 更新与term匹配的docs 68       indexWriter.updateDocument(new Term("filename", file.getName()), doc); 69     } 70     indexWriter.close(); 71   } 72  73  74   @Test 75   public void serach() throws IOException, ParseException { 76  77     //本地索引文件 78     Directory directory = FSDirectory.open(new File("D:\\documents\\Lucene\\MyDemo\\index")); 79  80     //读索引目录 81     IndexReader indexReader = DirectoryReader.open(directory); 82  83     //创建索引搜索对象 84     IndexSearcher indexSearcher = new IndexSearcher(indexReader); 85  86     Analyzer analyzer= new StandardAnalyzer(Version.LUCENE_4_9); 87  88     //查询解析 指定查询的item,解析器,版本 89     QueryParser queryParse = new QueryParser(Version.LUCENE_4_9, "contents", analyzer); 90  91     //查询内容 92     Query query = queryParse.parse("111"); 93  94     //查询指定条数 95     int num = 6; 96     TopDocs topDocs= indexSearcher.search(query, num); 97  98     //采集数 99     ScoreDoc[] docs = topDocs.scoreDocs;100 101     for(ScoreDoc doc:docs){102 103       //获取doc编号104       int i = doc.doc;105 106       //通过文档编号获取文档信息107       Document d = indexSearcher.doc(i);108 109       //打印文档信息110       System.out.println(d.get("filename"));111       System.out.println(d.get("modify"));112       System.out.println(d.get("contents"));113     }114     indexReader.close();115   }116117 }

 




原标题:Luence

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Shopee无货源商家店铺数据分析需要掌握的几点数据:https://www.ikjzd.com/articles/124178
2020 年Prime Day会员日或不再举行?卖家何去何从?:https://www.ikjzd.com/articles/124198
lazada的入驻流程,入驻条件及热卖品类目分享:https://www.ikjzd.com/articles/124201
新手卖家如何快速上道亚马逊?:https://www.ikjzd.com/articles/124204
亚马逊卖家的销量都来自哪些地方?:https://www.ikjzd.com/articles/124205
卖家抄袭售假,亚马逊与品牌方联合起诉,打击假货!:https://www.ikjzd.com/articles/124206
深圳玉龙公园簕杜鹃花海花期时间+门票+交通:https://www.vstour.cn/a/365174.html
出境旅游预订须知:https://www.vstour.cn/a/365175.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流