*lucene索引_创建_域选项
【索引建立步骤】
【创建Directory】
【创建writer】
【创建文档并添加索引】
文档和域的概念很重要
文档相当于表中的每一条记录,域相当于表中的每一个字段。
【查询索引的基本信息】
使用IndexReader进行查询。
【实践】
附:
IndexUtil.java:
package cn.hk.index; import java.io.File;
import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version; public class IndexUtil {
private String[] ids = {"1","2","3","4","5","6"};
private String[] emails = {"aa@hk.arg","bb@hk.org","cc@hk.arg",
"dd@hk.org","ee@hk.org","ff@hk.org"};
private String[] content = {
"welcome to visited the space","hello boy","my name is aa","i like football",
"I like football and I like Basketball too","I like movie and swim"
};
private int[] attachs = {2,3,1,4,5,5};
private String[] names = {"zhangsan","lisi","john","mike","jetty","jake"}; private Directory directory = null; public IndexUtil(){
try {
directory = FSDirectory.open(new File("d://lucene/index02"));
} catch (IOException e) {
e.printStackTrace();
}
} public void query(){
try {
IndexReader reader = IndexReader.open(directory);
//通过reader可以获取文档的数量
System.out.println("numDocs:" + reader.numDocs());
System.out.println("maxDocs" + reader.maxDoc());
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
} public void index(){
IndexWriter writer = null;
try {
writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
Document doc = null;
for(int i=0;i<ids.length;i++){
doc = new Document();
doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("content",content[i],Field.Store.NO,Field.Index.ANALYZED));
doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(writer != null)
try {
writer.close();
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} }
} }
TestIndex.java:
package cn.hk.test; import org.junit.Test; import cn.hk.index.IndexUtil; public class TestIndex { @Test
public void testIndex(){
IndexUtil iu = new IndexUtil();
iu.index();
} @Test
public void testQuery(){
IndexUtil iu = new IndexUtil();
iu.query();
}
}
index()运行结果:
query()运行结果
*lucene索引_创建_域选项的更多相关文章
- Lucene——Field.Store(存储域选项)及Field.Index(索引选项)
Field.Store.YES或者NO(存储域选项) 设置为YES表示或把这个域中的内容完全存储到文件中,方便进行文本的还原 设置为NO表示把这个域的内容不存储到文件中,但是可以被索引,此时内容无法完 ...
- Lucene——索引的创建、删除、修改
package cn.tz.lucene; import java.io.File; import java.util.ArrayList; import java.util.List; import ...
- lucene索引的创建与搜索
package com.cs.multi; import java.io.File;import java.io.IOException; import org.apache.lucene.analy ...
- Lock锁_线程_线程域
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- lucene 索引创建步骤
一.步骤: 1.存储位置:1)文件: Directory dir= FSDirectory.open(new File("D:\\LuceneIndex")); 2)内存: new ...
- C++框架_之Qt的开始部分_概述_安装_创建项目_快捷键等一系列注意细节
C++框架_之Qt的开始部分_概述_安装_创建项目_快捷键等一系列注意细节 1.Qt概述 1.1 什么是Qt Qt是一个跨平台的C++图形用户界面应用程序框架.它为应用程序开发者提供建立艺术级图形界面 ...
- JAVAEE——Lucene基础:什么是全文检索、Lucene实现全文检索的流程、配置开发环境、索引库创建与管理
1. 学习计划 第一天:Lucene的基础知识 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3.配置开发环境 4.创建索引库 5 ...
- Lucene中的域选项
Field类是lucene在索引过程中的一个很重要的类,该类控制着域值被索引的方式 域选项主要包括以下几种选项:域索引选项.域存储选项.域的项向量选项.域的排序选项等 域索引选项:通过倒排索引来控制域 ...
- lucene&solr学习——创建和查询索引(理论)
1.Lucene基础 (1) 简介 Lucene是apache下的一个开放源代码的全文检索引擎工具包.提供完整的查询引擎和索引引擎:部分文本分析引擎. Lucene的目的是为软件开发人员提供一个简单易 ...
随机推荐
- python_os.path模块用法
python中os.path模块用法: dirname() 用于去掉文件名,返回目录所在的路径 >>> import os >>> os.path.dirname ...
- Android UI 设计规范
1. 基础常识 1.1 主流屏幕尺寸 标识 屏幕尺寸 hdpi 480 * 800 xhdpi 720 * 1280 xxhdpi 1080 * 1920 1.2 图标尺寸 标识 启动图标尺寸 菜单图 ...
- 51nod 1154 回文串划分
1154 回文串划分 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有 ...
- ACM_Encoding
Encoding Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定一个只包含'A' - 'Z'的字符串,我们可以使用以下方法对 ...
- jmeter(七)函数
JMeter函数是一些能够转化在测试树中取样器或者其他配置元件的域的特殊值.一个函数的调用就像这样:${_functionName(var1,var2,var3)},-functionName匹配函数 ...
- poj1781In Danger(约瑟夫) 问题
链接 之前队内赛中的一道题目 当时怎么想也没想到,就一直放到了今天,刚才看另一题的讲解突然看到时拿这个题作为引子来讲的,就仔细看了下. 参考<<具体数学>> p7. Josep ...
- 在reset css后两个input之间还是出现默认间隔的问题。
<div class="search_box fl"> <input type="text" class="search_text& ...
- JavaScript实现JQuery的功能
- hihocoder offer收割编程练习赛12 C 矩形分割
思路: 模拟,深搜. 实现: #include <iostream> #include <cstdio> #include <string> using names ...
- 机器学习-随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )
梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...