实验一下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. LeetCode 151 reverse word in a string

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  2. Python 基础总结

    1.执行python脚本的两种方式: 答:1../run.py.shell直接调用python脚本 2.python run.py 调用python 解释器来调用python脚本 5.python单行 ...

  3. 【bzoj4272】筐子放球

    看题解会的系列…… 详细解释先坑着,以后补…… #include<bits/stdc++.h> #define N 200005 using namespace std; ,tot=,cn ...

  4. .NET Core Runtime ARM32 builds now available

    原文地址:传送门 .NET Core Runtime ARM32 builds now available The .NET Core team is now producing ARM32 buil ...

  5. magento 报错及解决方法

    在后台安装主题包时安装出错,重新进入后台进不去,前台也进不去,提示“Service Temporarily Unavailable” 删除根目录下的maintenance.flag文件即可.

  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统

    http://www.tuicool.com/articles/NfyqQr 本节主要知识点是easyui 的手风琴加树结构做菜单导航 有园友抱怨原来菜单非常难看,但是基于原有树形无限级别的设计,没有 ...

  7. easyUI之datagrid

    对于easyUI中的datagrid组件,继承自pannel...,因此有其自己的组件,还有继承过来的属性. 1.width:指定其宽度 2.title:指定标题 3.iconCls:指定图标.见上图 ...

  8. Ubuntu下安装 Phantomjs

    1.安装phantomjs —-下载程序文件 wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x8 ...

  9. Manacher【p1210】回文检测

    题目描述--->P1210 回文检测 分析: 看到回文显然想到了manacher算法(线性求解回文串问题 如果不了解还是去敲一下板子,学习一下比较好.-->manacher 题目要求我们求 ...

  10. mdadm Raid5 /dev/md0 lost a disk and recovery from another machine

    centos -- how to add a new disk into a mdadm raid5 /dev/md0 which lost a /dev/sdc1 disk and  revoery ...