package com.jiaoyiping.lucene;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
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.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test; import java.io.IOException;
import java.nio.file.Paths; /**
* Created with IntelliJ IDEA.
* User: 焦一平
* Date: 2015/3/21
* Time: 15:40
* To change this template use File | Settings | File Templates.
*/
public class TestLuceneIndex {
private static String indexPath = "d:\\lucene";
@Test
public void testIndex() throws IOException {
Directory directory = FSDirectory.open(Paths.get(indexPath));
IndexWriter writer = new IndexWriter(directory,new IndexWriterConfig(new StandardAnalyzer()));
Document document = new Document();
//Field有多种实现类可选,不同的实现类有不同的索引策略
document.add(new TextField("name","张三", Field.Store.YES));
document.add(new IntField("age",23,Field.Store.YES));
writer.addDocument(document);
writer.commit();
writer.close();
}
@Test
public void testSearch() throws IOException, ParseException {
Directory directory = FSDirectory.open(Paths.get(indexPath));
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser queryParser = new QueryParser("name",new StandardAnalyzer());
Query query = queryParser.parse("张三");
TopDocs tds = searcher.search(query, 10);
ScoreDoc[] sds = tds.scoreDocs;
for(ScoreDoc sd: sds){
Document document = searcher.doc(sd.doc);
System.out.println(document.get("name"));
System.out.println(document.get("age"));
}
}
}

以上是Lucene最基本的索引和搜索的使用(基于Lucene5.0版本)

执行构建索引操作之后会在索引存放目录下生成索引文件。执行搜索的时候由IndexReader来读取这些索引文件

Lucene中最简单的索引和搜索示例的更多相关文章

  1. 使用Lucene.NET实现简单的站内搜索

    使用Lucene.NET实现简单的站内搜索 导入Lucene.NET 开发包 Lucene 是apache软件基金会一个开放源代码的全文检索引擎工具包,是一个全文检索引擎的架构,提供了完整的查询引擎和 ...

  2. 用Lucene.net对数据库建立索引及搜索<转>

    用Lucene.net对数据库建立索引及搜索 最近我一直在研究 Lucene.net ,发现Lucene.net对数据库方面建索引的文章在网上很少见,其实它是可以对数据库进行索引的,我闲着没事,写了个 ...

  3. 在Hadoop分布式文件系统的索引和搜索

    FROM:http://www.drdobbs.com/parallel/indexing-and-searching-on-a-hadoop-distr/226300241?pgno=3 在今天的信 ...

  4. (二)Luence——代码实现索引及搜索

    完成需求:使用Lucene完成对数据库中图书信息的索引和搜索功能. 1. 环境准备及工程搭建 1.1 环境准备 mysql5.5+java8+lucene4.10.3(目前最新7.0.1,这里够用就好 ...

  5. 理解Lucene索引与搜索过程中的核心类

    理解索引过程中的核心类 执行简单索引的时候需要用的类有: IndexWriter.ƒDirectory.ƒAnalyzer.ƒDocument.ƒField 1.IndexWriter IndexWr ...

  6. Lucene.net 从创建索引到搜索的代码范例

    关于Lucene.Net的介绍网上已经很多了在这里就不多介绍Lucene.Net主要分为建立索引,维护索引和搜索索引Field.Store的作用是通过全文检查就能返回对应的内容,而不必再通过id去DB ...

  7. lucene简介 创建索引和搜索初步

    lucene简介 创建索引和搜索初步 一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引 ...

  8. 用Lucene对文档进行索引搜索

    问题 现在给出很多份文档,现在对某个搜索词感兴趣,想找到相关的文档. 简单搜索 一种简单粗暴的做法是: 1.读取每个文档:2.找到其中含有搜索词的文档:3.对找到的文档中搜索词出现的次数统计:4.根据 ...

  9. Lucene第二讲——索引与搜索

    一.Feild域 1.Field域的属性 是否分词:Tokenized 是:对该field存储的内容进行分词,分词的目的,就是为了索引. 否:不需要对field存储的内容进行分词,不分词,不代表不索引 ...

随机推荐

  1. [JavaScript]JS调用PHP和PHP调用JS的方法举例

    http://blog.csdn.net/pleasecallmewhy/article/details/8592571 body { background: #C7EDCC !important; ...

  2. dvi 中的内容居中

    text-align:right;  文本居中 line-height:35px;*垂直居中*

  3. Python 字符串语法,for

    #-*- coding:utf-8 -*- #字符串的拼接方式一 a = "hello" b = "world" c = a + " " + ...

  4. VMware ESXi 不支持NTFS格式的USB外接硬盘

    本来想搞直通USB外接大容量硬盘(希捷Seagate Backup+ Hub WH 8T),实现在同一部ESXi下,直接将NAS的数据转移到外接硬盘.结果发现虚拟机下的win server系统识别不了 ...

  5. 获取platformVersion、deviceName、appPackage

    命令获取如下: 手机与电脑连接 devicename:    adb devices platformversion :   adb shell getprop ro.build.version.re ...

  6. 【F12】修改 DevTools的主题

    1.点击setting

  7. ubuntu 系统网络突然"网络已禁用"

    sudo service network-manager stop  sudo rm /var/lib/NetworkManager/NetworkManager.state  sudo servic ...

  8. linux -- ubuntu搭建nodejs环境

    需求:在web端做一个实时性功能比较强的模块, 客户端:用websocket 服务端:node.js node.js介绍:node.js天生就是一个高效的服务端语言,可以直接使用 javascript ...

  9. js学习之--Bootstrap Modals(模态框)

    http://www.runoob.com/bootstrap/bootstrap-v2-modal-plugin.html http://outofmemory.cn/bootstrap/tutor ...

  10. par函数的adj 参数- 控制文字的对齐方式

    adj 用来控制文字的对齐方式,取值范围为0到1,控制图片中x轴和y轴标签,标题,以及通过text 添加的文字的对齐方式 0表示左对齐,代码示例: par(adj = 0)plot(1:5, 1:5, ...