实验一下Lucene是怎么使用的。

参考:http://www.importnew.com/12715.html (例子比较简单)

http://www.yiibai.com/lucene/lucene_first_application.html (例子比较复杂)

这里也有一个例子:http://www.tuicool.com/articles/aqIZNnE

我用的版本比较高,是6.2.1版本,文档查阅:

http://lucene.apache.org/core/6_2_1/core/index.html

首先在Intellij里面创建一个Maven项目。名字为lucene-demo。(主要参考 http://www.importnew.com/12715.html )

其中pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.myapp</groupId>
<artifactId>lucene-demo</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>6.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>6.2.1</version>
</dependency>
</dependencies> </project>

讲了一个package:com.myapp.lucene,里面class LuceneDemo,内容如下:

package com.myapp.lucene;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
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.queryparser.classic.ParseException;
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.TopScoreDocCollector;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.Directory; import java.io.IOException; /**
* Created by baidu on 16/10/20.
*/
public class LuceneDemo {
// 0. Specify the analyzer for tokenizing text.
// The same analyzer should be used for indexing and searching
static StandardAnalyzer analyzer;
static Directory index; static void prepareDoc() throws IOException{
// 0. init analyzer
analyzer = new StandardAnalyzer(); // 1. create index
index = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter w = new IndexWriter(index, config); addDoc(w, "lucence tutorial", "123456");
addDoc(w, "hi hi hi", "222");
addDoc(w, "ok LUCENCE", "123");
w.close();
} static void addDoc(IndexWriter w, String text, String more) throws IOException{
Document doc = new Document();
doc.add(new TextField("text", text, Field.Store.YES));
doc.add(new StringField("more", more, Field.Store.YES));
w.addDocument(doc);
} static void search(String str) throws ParseException, IOException {
// 2. query
Query q = new QueryParser("text", analyzer).parse(str); // 3. search
int listNum = 10;
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(listNum);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs; // 4. display
System.out.printf("Found %d docs.\n", hits.length);
for (int i=0; i<hits.length; i++) {
int docId = hits[i].doc;
Document doc = searcher.doc(docId);
System.out.printf("Doc %d: text: %s, more: %s\n", i+1, doc.get("text"), doc.get("more"));
}
reader.close(); } public static void main(String[] args) {
try {
prepareDoc();
search("Lucence");
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} }
}

然后运行,能够成功:

Found 2 docs.
Doc 1: text: lucence tutorial, more: 123456
Doc 2: text: ok LUCENCE, more: 123 Process finished with exit code 0

因为用的是RAMDirectory,所以应该没有创建实际的目录和文件。

另外,代码和逻辑中有几点需要注意的地方:

注意,对于需要分词的内容我们使用TextField,对于像id这样不需要分词的内容我们使用StringField。
编码过程中,报过好几次错,关于Exception需要wrap或者throws的情况。
有些API的版本升级了,参数和以前不一样。在实际的代码中根据实际要求有所修改。一般都是简化了。

Lucene的学习及使用实验的更多相关文章

  1. 【Todo】Lucene系统学习

    之前已经写过一篇关于Lucene安装学习的文章:http://www.cnblogs.com/charlesblc/p/5980525.html 还有一篇关于Solr安装使用的文章:http://ww ...

  2. 第六周学习总结&java实验报告四

    第六周学习总结&java实验报告四 学习总结: 上一周因为接近国庆假期,所以老师没有讲太多的新知识点,只要是带我们一起做了一个动物模拟变声器的实验,进一步了解和学习到继承的 有关知识点和应用: ...

  3. Lucene/ElasticSearch 学习系列 (1) 为什么学,学什么,怎么学

    为什么学 <What I wish I knew When I was 20>这本书给了我很多启发.作者在书中提到,Stanford 大学培养人才的目标是 ”T形人才“:精通某个领域,但对 ...

  4. v3学院带你学习EEPROM读写实验

    一.实验背景在消费者电子电讯和工业电子中看上去不相关的设计里经常有很多相似的地方例如几乎每个系统都包括一些智能控制通常是一个单片的微控制器,通用电路例如LCD驱动器远程I/O,RAM,EEPROM或数 ...

  5. Lucene入门学习

    技术原理: 开发环境: lucene包:分词包,核心包,高亮显示(highlight和memory),查询包.(下载请到官网去查看,如若下载其他版本,请看我的上篇文档,在luke里面) 原文文档: 入 ...

  6. Lucene基础学习笔记

    在学校和老师一起做项目,在老师的推荐下深入学习了一些SqlServer的知识,看一些书下来哎也没记住多少,不过带来了新疑问. 不使用模糊查询,我应该用什么呢?如何能不影响数据库性能,还能做模糊查询呢? ...

  7. lucene&solr学习——创建和查询索引(代码篇)

    1. Lucene的下载 Lucene是开发全文检索功能的工具包,从官网下载Lucene4.10.3并解压. 官网:http://lucene.apache.org/ 版本:lucene7.7.0 ( ...

  8. lucene中文学习地址推荐

    Lucene原理与代码分析http://www.cnblogs.com/forfuture1978/category/300665.html Lucene5.5学习(1)-初尝Lucene全文检索引擎 ...

  9. TensorFlow - 深度学习破解验证码 实验

    TensorFlow - 深度学习破解验证码 简介:验证码主要用于防刷,传统的验证码识别算法一般需要把验证码分割为单个字符,然后逐个识别,如果字符之间相互重叠,传统的算法就然并卵了,本文采用cnn对验 ...

随机推荐

  1. bzoj1008 矩乘递推

    2013-11-17 10:38 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1008 比较水的题,直接矩阵乘法+递推就OK了 w[i,0 ...

  2. TCP三次握手四次分手

    TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...

  3. intValue()的用法

    今天看到了Integer的这个方法,有点疑惑,查了下,做下笔记; 1.intValue()是java.lang.Number类的方法,Number是一个抽象类.Java中所有的数值类都继承它.也就是说 ...

  4. delphi string,pchar,char的比较

    来自:http://blog.163.com/kat_/blog/static/189284269201152513331999/ ---------------------------------- ...

  5. QTP自动化测试框架的基础知识

    1. 什么是自动化测试框架? 假定你有一个活,需要构建许多自动化测试用例来测试这个应用程序.当你对这个应用程序完成自动化测试后,你对自己创建脚本应该有什么期望吗?你难道不想要- 脚本应该按照预期的来执 ...

  6. LightOJ 1012.Guilty Prince-DFS

    Guilty Prince  Time Limit: 2 second(s) Memory Limit: 32 MB Once there was a king named Akbar. He had ...

  7. Problem C: #104. 普通平衡树

    #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #inc ...

  8. linux根目录下文件夹概览详解

    / 根目录 /bin 存放必要的命令 /boot 存放内核以及启动所需的文件等 /dev 存放设备文件 /etc 存放系统的配置文件 /home 用户文件的主目录,用户数据存放在其主目录中 /lib ...

  9. 洛谷——P1134 阶乘问题

    P1134 阶乘问题 题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 ...

  10. tomcat安装规范

    创建用户 useradd -u 501 tomcat passwd tomcat tomcat安装 tar zxf apache-tomcat-8.5.5.tar.gz -C /usr/local/ ...