Lucene-全文索引
近期接触了lucene,我想也有非常多人以前听过,于是带着好奇心,我開始对lucene进行了解,给我影响最深的是它非常多的应用了索引表,这个工具之所以快是就是由于大量引用到了索引表。今天仅仅说下我刚開始做的校历样例,创建索引。
以下对lucene从概念上做个介绍,Lucene是一个信息检索的函数库(Library),利用它你能够为你的应用加上索引和搜索的功能.Lucene的使用者不须要深入了解有关全文检索的知识,只学会使用库中的一个类,你就为你的应用实现全文检索的功能.不过千万别以为Lucene是一个象google那样的搜索引擎,Lucene甚至不是一个应用程序,它不过一个工具,一个Library.你也能够把它理解为一个将索引,搜索功能封装的非常好的一套简单易用的API.利用这套API你能够做非常多有关搜索的事情,并且非常方便.
那么lucene能够做什么呢?Lucene能够对不论什么的数据做索引和搜索. Lucene无论数据源是什么格式,仅仅要它能被转化为文字的形式,就能够被Lucene所分析利用.也就是说无论是MS word,
Html ,pdf还是其它什么形式的文件仅仅要你能够从中抽取出文字形式的内容就能够被Lucene所用.你就能够用Lucene对它们进行索引以及搜索. 以下是我做的一个小样例,就是一个查询生成索引的样例:
<span style="font-size:14px;">package com.jikexueyuan.study;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class IndexCreate { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);//StandardAnalyzer是将英文依照空格、标点符号等进行分词。将中文依照单个字进行分词。一个汉字算一个词
IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_46,analyzer);//把写入的文件用指定的分词器将文章分词(这样检索的时候才干查的快),然后将词放入索引文件。
indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
Directory directory=null;
IndexWriter indexWriter=null;
try {
directory=FSDirectory.open(new File("E://index/test"));// //索引库存放在这个目录里 ,Directory表示索引文件保存的地方,是抽象类,两个子类FSDirectory表示文件里,RAMDirectory 表示存储在内存中
if(indexWriter.isLocked(directory)){
indexWriter.unlock(directory);
}
indexWriter=new IndexWriter(directory,indexWriterConfig);
} catch (Exception e) {
e.printStackTrace();
} //Document document=new Document();
Document doc = new Document();
doc.add(new StringField("id","abcde", Store.YES));
doc.add(new org.apache.lucene.document.TextField("content","极客学院",Store.YES));
doc.add(new IntField("num",1,Store.YES)); try {
indexWriter.addDocument(doc);//向索引中加入文档(Insert)
} catch (Exception e) { e.printStackTrace(); } Document doc1 = new Document();
doc1.add(new StringField("id","sdfsd", Store.YES));
doc1.add(new org.apache.lucene.document.TextField("content","Lucene案例",Store.YES));
doc1.add(new IntField("num",1,Store.YES)); try {
indexWriter.addDocument(doc1);
} catch (Exception e) { e.printStackTrace(); }
try {
indexWriter.commit();
indexWriter.close();
directory.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
</span>
结果会生成一系列的有关索引的文件。例如以下图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ2RhbjE5OTExMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
从上面的样例我们能够看出创建索引须要的三个要素各自是:
1、indexWriter
2、Directory
3、Anayzer
4、Document
5、Field
对于lucene的分享还要继续。希望有越来越多的人能够共同努力!
Lucene-全文索引的更多相关文章
- 5分钟了解lucene全文索引
一.Lucene介绍及应用 Apache Lucene是当下最为流行的开源全文检索工具包,基于JAVA语言编写. 目前基于此工具包开源的搜索引擎,成熟且广为人知的有Solr和Elasticsearch ...
- 全文索引-lucene,solr,nutch,hadoop之nutch与hadoop
全文索引-lucene.solr.nutch,hadoop之lucene 全文索引-lucene.solr,nutch,hadoop之solr 我在去年的时候,就想把lucene,solr.nutch ...
- 深度解析 Lucene 轻量级全文索引实现原理
一.Lucene简介 1.1 Lucene是什么? Lucene是Apache基金会jakarta项目组的一个子项目: Lucene是一个开放源码的全文检索引擎工具包,提供了完整的查询引擎和索引引擎, ...
- lucene全文检索---打酱油的日子
检索内容,一般的程序员第一时间想到的是sql的like来做模糊查询,其实这样的搜索是比较耗时的.已经有lucene帮我们 封装好了,lucene采用的是分词检索等策略. 1.lucene中的类描述 I ...
- 全文索引之nutch与hadoop(转)
原文:http://blog.csdn.net/chaofanwei/article/details/39476535 全文索引-lucene,solr,nutch,hadoop之lucene 全文索 ...
- Lucene:基于Java的全文检索引擎简介
Lucene:基于Java的全文检索引擎简介 Lucene是一个基于Java的全文索引工具包. 基于Java的全文索引/检索引擎--Lucene Lucene不是一个完整的全文索引应用,而是是一个用J ...
- Lucene:基于Java的全文检索引擎简介 (zhuan)
http://www.chedong.com/tech/lucene.html ********************************************** Lucene是一个基于Ja ...
- 整合hibernate的lucene大数据模糊查询
大数据模糊查询lucene 对工作单使用 like模糊查询时,实际上 数据库内部索引无法使用 ,需要逐条比较查询内容,效率比较低在数据量很多情况下, 提供模糊查询性能,我们可以使用lucene全文 ...
- (转)ElasticSearch学习
(二期)21.全文搜索引擎Elasticsearch [课程21]elasticsearch.xmind82.1KB [课程21]lucene.xmind0.8MB [课程21]基本用法....api ...
- (转)mblog解读(二)
(二期)12.开源博客项目mblog解读(二) [课程12]freema...模板.xmind77.9KB [课程12]hibernat...arch.xmind0.1MB freemarker模板技 ...
随机推荐
- codeforces#281 A
脑子有点秀逗 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...
- 安卓4.3以上版本已经完美支持BLE(英文版)
Android 4.3 (API Level 18) introduces built-in platform support for Bluetooth Low Energy in the cent ...
- 一些 <link> 标记分享
<link rel="alternate" media="handheld" href="#" /> <link rel= ...
- python3.x 学习笔记1(基础知识)
1.python模块: 标准库和第三方库,第三方库需要下载安装 2.模块sys: 命令 功能 sys.stdin 标准输入流sys.stdout 标准输出流sys.stderr ...
- CUDA中的归约
CUDA编程实战书中的乘方和解决办法: 对一个数组执行某种计算,然后产生一个更小的结果数组. 由一个线程在共享内存上进行迭代并计算出总和值.而如果用并行,所花时间就与数组长度的对数成正比. 代码的思想 ...
- appium使用教程(三)-------------用例编写
1. 驱动 import os, time, unittest from appium import webdriver PATH = lambda p:os.path.abspath(os.path ...
- [转载][来自csdn]RTS和CTS是什么意思?
原文链接: http://blog.csdn.net/zmq5411/article/details/6280332 这篇文章看着挺好,明白易懂,顺手转过来 34RTS和CTS是什么意思? 解释一:R ...
- 今日SGU 5.28
SGU 121 题意:给你一张图,问你每个顶点必须有黑白两条边(如果它的边数>=2),问你怎么染色,不行就输出no 收获:你会发现不行的情况只有一个单纯的奇数环的时候,反之我们交替染色即可 #i ...
- 最长回文字串 (The longest palindrome substring)
这两天去学了一下,觉得下面那篇文章写的很好,有例子,比较容易懂,所以转一下. 以下内容来自:hihoCoder: 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...
- IDEA使用技巧汇总
使用IDEA也有一段时间了,今天又看到了一个不错的IDEA视频,觉得对IDEA熟悉得更多了,在这里做下笔记,如下 视频链接:https://www.imooc.com/video/16219 1.下载 ...