Lucene add、updateDocument添加、更新与search查询(转)
package com.lucene;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.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.search.Hits;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;public class UpdateDocument { private static String path = "d:/index"; public static void main(String[] args){// addIndex(); updateIndex(); search("李四"); search("王五"); } public static void addIndex(){ try { IndexWriter write = new IndexWriter(path,new StandardAnalyzer(),true); Document doc = new Document(); doc.add(new Field("id","123456",Field.Store.YES,Field.Index.UN_TOKENIZED)); doc.add(new Field("userName","张三",Field.Store.YES,Field.Index.TOKENIZED)); doc.add(new Field("comefrom","北京",Field.Store.YES,Field.Index.TOKENIZED)); write.addDocument(doc); write.close(); } catch (IOException e) { e.printStackTrace(); } } public static void updateIndex(){ try { IndexWriter write = new IndexWriter(path,new StandardAnalyzer(),false); Document docNew = new Document(); docNew.add(new Field("id","123456",Field.Store.YES,Field.Index.UN_TOKENIZED)); docNew.add(new Field("userName","王五",Field.Store.YES,Field.Index.TOKENIZED)); Term term = new Term("id","123456"); /** 调用updateDocument的方法,传给它一个新的doc来更新数据, Term term = new Term("id","1234567"); 先去索引文件里查找id为1234567的Doc,如果有就更新它(如果有多条,最后更新后只有一条)。如果没有就新增. 数据库更新的时候,我们可以只针对某个列来更新,而lucene只能针对一行数据更新。 */ write.updateDocument(term, docNew); write.close(); } catch (IOException e) { e.printStackTrace(); } } public static Query queryParser(String str){ QueryParser queryParser = new QueryParser("userName", new StandardAnalyzer()); try { Query query = queryParser.parse(str); return query; } catch (Exception e) { e.printStackTrace(); } return null; } public static void search(String str){ try { IndexSearcher search = new IndexSearcher(path); Query query = queryParser(str); Hits hits = search.search(query); if(hits==null){ return; } if(hits.length() == 0){ System.out.println(" 没有搜索到'" + str+"'"); return; } for (int i = 0; i < hits.length(); i++) { Document doc = hits.doc(i); System.out.println("id = "+hits.id(i)); System.out.println("own id = " + doc.get("id")); System.out.println("userName = "+doc.get("userName")); System.out.println("come from = "+doc.get("comefrom")); System.out.println(""); } } catch (Exception e) { e.printStackTrace(); } }}Lucene add、updateDocument添加、更新与search查询(转)的更多相关文章
- ASP.NET MVC搭建项目后台UI框架—6、客户管理(添加、修改、查询、分页)
目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...
- 给当前的URL添加/更新新的参数
/** * 给当前的URL添加/更新新的参数 * @param a 参数 * @param b 值 * @returns {string} 新的参数 * @constructor */ functio ...
- day 68crm(5) 分页器的进一步优化,以及在stark上使用分页器,,以及,整理代码,以及stark组件search查询
前情提要: 本节内容 自定制分页器 保存及查询记录 代码整理, stark组件search 查询 一:自定制分页器 page 1:创建类 Pagination # 自定制分页器 _ _init ...
- Dom4j 添加 / 更新 / 删除 XML
1.获得文档 /** *1.获得解析流 *2.解析XML */ 2.添加 /** *1.获取父元素 *2.创建元素 *3.创建属性并添加到元素中 *4.元素添加到根节点 */ 3.更新 /** *1. ...
- MVC客户管理(添加、修改、查询、分页)
ASP.NET MVC搭建项目后台UI框架—6.客户管理(添加.修改.查询.分页) 目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2 ...
- POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 140120 ...
- 2015 UESTC 数据结构专题A题 秋实大哥与小朋友 线段树 区间更新,单点查询,离散化
秋实大哥与小朋友 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Desc ...
- 2017 Wuhan University Programming Contest (Online Round) D. Events,线段树区间更新+最值查询!
D. Events 线段树区间更新查询区间历史最小值,看似很简单的题意写了两天才写出来. 题意:n个数,Q次操作,每次操作对一个区间[l,r]的数同时加上C,然后输出这段区间的历史最小值. 思路:在线 ...
- git add 命令添加所有改动内容
git add xx命令可以将xx文件添加到暂存区,如果有很多改动可以通过 git add -A .来一次添加所有改变的文件. 注意 -A 选项后面还有一个句点. git add -A表示添加所有内容 ...
随机推荐
- 安装mcrouter
下载准备: mcrouter 下载地址 : https://github.com/facebook/mcrouter gflags 下载地址:http://download.csdn.net/deta ...
- 华中农业大学新生赛C题
http://acm.hzau.edu.cn/problem.php?id=1099 题意: 输入两个整数 l 和 n,代表半径和output的保留小数点位数. 输出圆的面积,保留n位小数. 一开始觉 ...
- IAR和Keil文件包含路径设置
在模块化编程时,为一个模块单独设置头文件是必不可少的. 在两款主流编译器中,在引用模块函数时候,包含头文件路径是必须的,那么设置文件路径的准确性就显得尤为重要. 否则,编译器会报错,无法打开某某头文件 ...
- spring配置文件详解【总结】
知其然,知其所以然 <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns=&qu ...
- dom入门
当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). //显示,改变html内容 document.getElementById("p1" ...
- PAT (Top Level) Practise 1005 Programming Pattern (35)
后缀数组.排序之后得到height数组,然后从上到下将height>=len的都分为一组,然后找到第一组个数最多的输出即可. #pragma comment(linker, "/STA ...
- scanf函数与scanf_s函数
ANSI C中没有scanf_s(),只有scanf(),scanf()在读取时不检查边界,所以可能会造成内存泄露.所以vc++2005/2008中提供了scanf_s(),在最新的VS2013中也提 ...
- akka 入门
http://blog.csdn.net/thkhxm/article/details/40182835 1.首先安装akka的相关包-- http://akka.io/downloads/2.导入依 ...
- hdu_3182_Hamburger Magi(状压DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3182 题意:有n个汉堡,做每个汉堡需要消耗一定的能量,每个汉堡对应一定的价值,且只能做一次,并且做当前 ...
- shell脚本 整数比较
1.整数比较 -eq 等于,如:if [ "$a" -eq "$b" ] -ne 不等于,如:if [ "$a" -ne " ...