- 浏览: 46046 次
- 性别:
- 来自: 南京
文章分类
最新评论
完全个人理解,如有更好的方法,欢迎一起讨论
LuceneUtils.java
package com.zbiti.lucene;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
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.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.CachingWrapperFilter;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryWrapperFilter;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import com.zbiti.util.sys.JsonUtils;
/**
* Lucene 辅助类
* @author 张明
*
*/
public class LuceneUtils {
//读取数据放入的地址
private static String IndexPath = "C:\\update\\index";
static Directory dir = null;
//初始化静态代码块
static{
try {
dir = FSDirectory.open(new File(IndexPath));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建本地文件
* @param list 传递过来的数据是一个list
* @throws Exception
*/
public static boolean createData(List<Information> list){
try {
Directory dir =FSDirectory.open(new File(IndexPath));
IndexWriterConfig writerConfig=new IndexWriterConfig(Version.LUCENE_36, new IKAnalyzer());
//设置每次创建都重新创建
writerConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
IndexWriter writer = new IndexWriter(dir, writerConfig) ;
//循环处理将数据放入Lucene中
for(int i=0; i< list.size(); i++){
Information information = list.get(i);
Document doc = new Document();
//设置字段
setField(information, doc);
writer.addDocument(doc);
}
//提交
writer.optimize();
writer.commit();
writer.close();
return true;
} catch (CorruptIndexException e) {
e.printStackTrace();
return false;
} catch (LockObtainFailedException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 创建一条数据
* @param information 一条数据,传送过来的数据必须是完整的数据
* @return
*/
public static boolean createData(Information information){
try {
Directory dir =FSDirectory.open(new File(IndexPath));
IndexWriterConfig writerConfig=new IndexWriterConfig(Version.LUCENE_36, new IKAnalyzer());
IndexWriter writer = new IndexWriter(dir, writerConfig);
Document doc = new Document();
//设置字段
setField(information, doc);
writer.addDocument(doc);
writer.optimize();
writer.commit();
writer.close();
return true;
} catch (CorruptIndexException e) {
e.printStackTrace();
return false;
} catch (LockObtainFailedException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 更新一条信息
* @param information 根据bjStorageId的唯一性来更新数据
* 传送过来的数据必须是完整的数据,Lucene的更新不同于sql更新,更新是一天信息的完整更新
* @return
*/
public static boolean updateData(Information information){
try {
Directory dir =FSDirectory.open(new File(IndexPath));
//第三个参数代表更新的时候不重新创建原本的数据
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_CURRENT), false, MaxFieldLength.UNLIMITED);
Document doc = new Document();
//设置字段
setField(information, doc);
Term term = new Term("bjStorageId",information.getBjStorageId().toString());
writer.updateDocument(term,doc);
writer.optimize();
writer.commit();
writer.close();
return true;
} catch (CorruptIndexException e) {
e.printStackTrace();
return false;
} catch (LockObtainFailedException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 删除一条数据
* @return 根据bjStorageId的唯一性来删除数据
*/
public static boolean deleteData(String bjStorageId){
try {
Directory dir =FSDirectory.open(new File(IndexPath));
//第三个参数代表更新的时候不重新创建原本的数据
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_CURRENT), false, MaxFieldLength.UNLIMITED);
Term term = new Term("bjStorageId", bjStorageId);
writer.deleteDocuments(term);
writer.optimize();
writer.commit();
writer.close();
return true;
} catch (CorruptIndexException e) {
e.printStackTrace();
return false;
} catch (LockObtainFailedException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 查询数据
* @param searchStr 即将要查询的字符串
* @param fieldStr 需要查询的字段数组
* @param flag 是否统计标志
* @param groupStr 分组字段数组
* @param flagAcc 是否根据条件精确查询
* @param mapFilter查询条件
* @return
*/
public static Map<String,Object> getDataList(String searchStr, String[] fieldArray, boolean flag, String[] groupArray, boolean flagAcc, Map<String,Object> mapFilter){
try {
IndexSearcher search = new IndexSearcher(dir);
System.out.println("lucene中的数据总数为:------->>>" + search.maxDoc());
QueryParser qp=new MultiFieldQueryParser(Version.LUCENE_36, fieldArray, new IKAnalyzer());
Query query=qp.parse(searchStr);
//一次查询多少个结果
//TopDocs tDocs=search.search(query,1000000);
TopDocs tDocs = null;
//是否二次查询,根据多条件精确查找
if(flagAcc){
LuceneFilter luceneFilter = new LuceneFilter();
Set<String> keySet = mapFilter.keySet();
for (String key : keySet)
luceneFilter.addFilter(key, mapFilter.get(key).toString());
query=luceneFilter.getFilterQuery(query);//结果过滤
tDocs = search.search(query,1000000);
}else{
tDocs=search.search(query,1000000);
}
int numTotalHits = tDocs.totalHits;
System.out.println("lucene中的搜索出来的数据总数为:------->>>" + numTotalHits);
Map<String,Object> map = new HashMap<String,Object>();
map.put("information", tDocs);
map.put("search", search);
//是否同时查询分组统计数据
if(flag){
for(int j=0; j < groupArray.length; j++){
JSONArray jsonArr = new JSONArray();
JSONObject jsonObj = new JSONObject();
Map<String,Object> groupMap = getDataByGroupBy(search, query, groupArray[j]);
Set<String> keySet = groupMap.keySet();
for (String key : keySet) {
jsonObj.put("id",key);
jsonObj.put("count", groupMap.get(key));
jsonArr.add(jsonObj);
}
map.put(groupArray[j],jsonArr);
}
}
//解析数据
// Document doc = null;
// for(int i=0; i<numTotalHits; i++){
// int k = tDocs.scoreDocs[i].doc ; //文档内部编号
// doc = search.doc(k);
// System.out.println(doc.get("name") +"--" + doc.get("id") + "-----" + doc.get("type") + "---" + doc.get("testField"));
// }
// search.close();
return map;
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据field指定的字段来进行分组统计总数
* @param search
* @param query
* @param field
*/
public static Map<String,Object> getDataByGroupBy(IndexSearcher search, Query query, String field){
try {
GroupCollector myCollector = new GroupCollector();
myCollector.setF(field);
search.search(query, myCollector);
GroupField gf = myCollector.getGroupField();
List<String> values = gf.getValues();
//读取数据
Map<String,Object> map = new HashMap<String,Object>();
for (String value : values) {
map.put(value, gf.getCountMap().get(value));
}
return map;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 设置字段信息
* @param information
* NOT_ANALYZED不支持模糊查找, ANALYZED支持模糊查找
*/
public static void setField(Information information, Document doc){
doc.add(new Field("actualStorageId", information.getActualStorageId()!=null?information.getActualStorageId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("deviceId", information.getDeviceId()!=null?information.getDeviceId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("brandId", information.getBrandId()!=null?information.getBrandId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("netId", information.getNetId()!=null?information.getNetId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("bjSpecialtyId", information.getBjSpecialtyId()!=null?information.getBjSpecialtyId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("deviceType", information.getDeviceType()!=null?information.getDeviceType().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("bjStorageId", information.getBjStorageId()!=null?information.getBjStorageId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("bjType", information.getBjType()!=null?information.getBjType().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("state1Cd", information.getState1Cd()!=null?information.getState1Cd().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("state2Cd", information.getState2Cd()!=null?information.getState2Cd().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("isStocktake", information.getIsStocktake()!=null?information.getIsStocktake().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("shelfId", information.getShelfId()!=null?information.getShelfId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
/*==================================================================================================================*/
doc.add(new Field("actualStorageRoom", information.getActualStorageRoom()!=null?information.getActualStorageRoom().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("originalSerialNum", information.getOriginalSerialNum()!=null?information.getOriginalSerialNum().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("deviceModel", information.getDeviceModel()!=null?information.getDeviceModel().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("frontPic", information.getFrontPic()!=null?information.getFrontPic().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("brandName", information.getBrandName()!=null?information.getBrandName().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("netType", information.getNetType()!=null?information.getNetType().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("netName", information.getNetName()!=null?information.getNetName().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("specialtyName", information.getSpecialtyName()!=null?information.getSpecialtyName().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("shelfCode", information.getShelfName()!=null?information.getShelfName().toString():"", Field.Store.YES, Field.Index.ANALYZED));
}
public static void main(String[] args) {
}
}
//辅助类
LuceneFilter.java
package com.zbiti.lucene;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.FilteredQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryWrapperFilter;
import org.apache.lucene.search.TermQuery;
/**
* 条件辅助类
* @author 张明
*
*/
public class LuceneFilter {
private List<Filter> filterList;
public LuceneFilter(){
filterList = new ArrayList<Filter>();
}
public void addFilter(String Field,String Value){
Term term=new Term(Field,Value);//添加term
QueryWrapperFilter filter=new QueryWrapperFilter(new TermQuery(term));//添加过滤器
filterList.add(filter);//加入List,可以增加多個过滤
}
public Query getFilterQuery(Query query){
for(int i=0;i<filterList.size();i++){
//取出多個过滤器,在结果中再次定位结果
query = new FilteredQuery(query, filterList.get(i));
}
return query;
}
}
Information.java
package com.zbiti.lucene;
/**
* Bean
*
* @author 张明
*
*/
public class Information {
/**
* 实物库房ID(分组字段)
*/
private Integer actualStorageId;
/**
* 设备型号ID(分组字段)
*/
private Integer deviceId;
/**
* 品牌ID(分组字段)
*/
private Integer brandId;
/**
* 网元ID(分组字段)
*/
private Integer netId;
/**
* 专业ID(分组字段)
*/
private Integer bjSpecialtyId;
/**
* 设备类型(分组字段)
*/
private String deviceType;
/**
* 备件编号(分组字段)
*/
private Integer bjStorageId;
/** *******************区分字段******************* */
/**
* 备件类型(区分备件-BJ,送修件SXJ)(分组字段)
*/
private String bjType;
/**
* 资产库房备件状态(分组字段)
*/
private String state1Cd;
/**
* 实物库房备件状态(分组字段)
*/
private String state2Cd;
/**
* 是否已盘点(分组字段)
*/
private String isStocktake;
/**
* 网元类型(分组字段)
*/
private String netType;
/**
* 货架ID(分组字段)
*/
private Integer shelfId;
/*=======================================================*/
/**
* 实物库房
*/
private String actualStorageRoom;
/**
* 序列号(送修件原序列号)
*/
private String originalSerialNum;
/**
* 设备型号
*/
private String deviceModel;
/**
* 设备前图片
*/
private String frontPic;
/**
* 品牌名称
*/
private String brandName;
/**
* 网元名称
*/
private String netName;
/**
* 专业类别
*/
private String specialtyName;
/**
* 货架名称
*/
private String shelfName;
public Integer getBjStorageId() {
return bjStorageId;
}
public void setBjStorageId(Integer bjStorageId) {
this.bjStorageId = bjStorageId;
}
public Integer getActualStorageId() {
return actualStorageId;
}
public void setActualStorageId(Integer actualStorageId) {
this.actualStorageId = actualStorageId;
}
public String getActualStorageRoom() {
return actualStorageRoom;
}
public void setActualStorageRoom(String actualStorageRoom) {
this.actualStorageRoom = actualStorageRoom;
}
public String getOriginalSerialNum() {
return originalSerialNum;
}
public void setOriginalSerialNum(String originalSerialNum) {
this.originalSerialNum = originalSerialNum;
}
public String getState1Cd() {
return state1Cd;
}
public void setState1Cd(String state1Cd) {
this.state1Cd = state1Cd;
}
public String getState2Cd() {
return state2Cd;
}
public void setState2Cd(String state2Cd) {
this.state2Cd = state2Cd;
}
public String getIsStocktake() {
return isStocktake;
}
public void setIsStocktake(String isStocktake) {
this.isStocktake = isStocktake;
}
public Integer getDeviceId() {
return deviceId;
}
public void setDeviceId(Integer deviceId) {
this.deviceId = deviceId;
}
public String getDeviceModel() {
return deviceModel;
}
public void setDeviceModel(String deviceModel) {
this.deviceModel = deviceModel;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getFrontPic() {
return frontPic;
}
public void setFrontPic(String frontPic) {
this.frontPic = frontPic;
}
public Integer getBrandId() {
return brandId;
}
public void setBrandId(Integer brandId) {
this.brandId = brandId;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public Integer getNetId() {
return netId;
}
public void setNetId(Integer netId) {
this.netId = netId;
}
public String getNetType() {
return netType;
}
public void setNetType(String netType) {
this.netType = netType;
}
public String getNetName() {
return netName;
}
public void setNetName(String netName) {
this.netName = netName;
}
public Integer getBjSpecialtyId() {
return bjSpecialtyId;
}
public void setBjSpecialtyId(Integer bjSpecialtyId) {
this.bjSpecialtyId = bjSpecialtyId;
}
public String getSpecialtyName() {
return specialtyName;
}
public void setSpecialtyName(String specialtyName) {
this.specialtyName = specialtyName;
}
public Integer getShelfId() {
return shelfId;
}
public void setShelfId(Integer shelfId) {
this.shelfId = shelfId;
}
public String getShelfName() {
return shelfName;
}
public void setShelfName(String shelfName) {
this.shelfName = shelfName;
}
public String getBjType() {
return bjType;
}
public void setBjType(String bjType) {
this.bjType = bjType;
}
}
GroupField.java
package com.zbiti.lucene;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author 张明
*
*/
public class GroupField {
// 所有可能的分组字段值,排序按每个字段值的文档个数大小排序
private List<String> values = new ArrayList<String>();
// 保存字段值和文档个数的对应关系
private Map<String, Integer> countMap = new HashMap<String, Integer>();
public Map<String, Integer> getCountMap() {
return countMap;
}
public void setCountMap(Map<String, Integer> countMap) {
this.countMap = countMap;
}
public List<String> getValues() {
Collections.sort(values, new ValueComparator());
return values;
}
public void setValues(List<String> values) {
this.values = values;
}
public void addValue(String value) {
if (value == null || "".equals(value))
return;
if (countMap.get(value) == null) {
countMap.put(value, 1);
values.add(value);
}else{
countMap.put(value, countMap.get(value) + 1);
}
}
class ValueComparator implements Comparator<String> {
public int compare(String value0, String value1) {
if (countMap.get(value0) > countMap.get(value1)) {
return -1;
} else if (countMap.get(value0) < countMap.get(value1)) {
return 1;
}
return 0;
}
}
}
GroupCollector.java
package com.zbiti.lucene;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.FieldCache;
import org.apache.lucene.search.Scorer;
/**
*
* @author 张明
*
*/
public class GroupCollector extends Collector{
// 保存分组统计结果
private GroupField groupField = new GroupField();
//fieldCache
private String[] fieldCache;
//统计字段
private String field;
String spliter;
int length;
public void setFc(String[] fieldCache) {
this.fieldCache = fieldCache;
}
@Override
public void setScorer(Scorer scorer) throws IOException {
}
@Override
public void setNextReader(IndexReader reader, int docBase)
throws IOException {
fieldCache = FieldCache.DEFAULT.getStrings(reader, field);
}
@Override
public void collect(int doc) throws IOException {
// 添加的GroupField中,由GroupField负责统计每个不同值的数目
groupField.addValue(fieldCache[doc]);
}
@Override
public boolean acceptsDocsOutOfOrder() {
return true;
}
public GroupField getGroupField() {
return groupField;
}
public void setSpliter(String spliter) {
this.spliter = spliter;
}
public void setLength(int length) {
this.length = length;
}
public void setF(String field) {
this.field = field;
}
}
//测试类
package com.zbiti.lucene;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TopDocs;
public class TestLucene {
public static void main(String[] args) throws CorruptIndexException, IOException {
//创建
List<Information> list = new ArrayList<Information>();
for(int i=0; i< 10000; i++){
Information information = new Information();
information.setActualStorageId(i);
information.setDeviceId(i);
if(i<300)
information.setBrandId(8888);
else
information.setBrandId(7777);
if(i < 500)
information.setNetId(100);
else
information.setNetId(200);
information.setBjSpecialtyId(i);
if(i <= 100)
information.setDeviceType("AMD");
else
information.setDeviceType("INTEL");
information.setBjStorageId(i);
if(i <= 20)
information.setActualStorageRoom("实际库房名称");
else
information.setActualStorageRoom("库房名称");
information.setOriginalSerialNum("序列号");
information.setState1Cd("001");
information.setState2Cd("002");
information.setIsStocktake("true");
information.setDeviceModel("设备型号");
information.setFrontPic("/pm/tp.png");
if(i <= 600 && i > 500)
information.setBrandName("实际银行名称");
else
information.setBrandName("银行名称");
information.setNetType("网元类型");
information.setNetName("网元名称");
information.setSpecialtyName("专业类别");
information.setShelfId(i);
information.setShelfName("货架编码名称");
information.setBjType("BJ");
list.add(information);
}
LuceneUtils.createData(list);
//====================================================================================================
//查询
// String[] fields = {"netId"};
// String str = "100";
String[] fields = {"actualStorageRoom","brandName"};
// //不分组统计
Map<String,Object> mapFilter = new HashMap<String,Object>();
Map<String,Object> map = LuceneUtils.getDataList("实际", fields, false, null, false, mapFilter);
TopDocs topDocs = (TopDocs)map.get("information");
IndexSearcher search = (IndexSearcher)map.get("search");
// Document doc = null;
// //取数据字段内容
// for(int i=0; i<topDocs.totalHits; i++){
// int k = topDocs.scoreDocs[i].doc ; //文档内部编号
// doc = search.doc(k);
// System.out.println(doc.get("actualStorageRoom") +"--" + doc.get("brandName") + "----" + doc.get("netId")) ;
// }
// search.close();
//======================================================================================================
// //分组统计
String str5 = "100";
String[] fields5 = {"netId"};
String[] groupFields = {"deviceType"};
Map<String,Object> mapGroup = LuceneUtils.getDataList(str5, fields5, true, groupFields, false, null);
System.out.println("统计信息 ------->>> " + mapGroup.get("deviceType"));
search.close();
//=======================================================================================================
//新增
Information information = new Information();
information.setActualStorageId(80001);
information.setDeviceId(80001);
information.setBrandId(80001);
information.setNetId(80001);
information.setBjSpecialtyId(80001);
information.setDeviceType("INTEL");
information.setBjStorageId(80001);
information.setActualStorageRoom("实际库房名称");
information.setOriginalSerialNum("序列号");
information.setState1Cd("001");
information.setState2Cd("002");
information.setIsStocktake("true");
information.setDeviceModel("设备型号");
information.setFrontPic("/pm/tp.png");
information.setBrandName("银行名称");
information.setNetType("网元类型");
information.setNetName("网元名称");
information.setSpecialtyName("专业类别");
information.setShelfId(80001);
information.setShelfName("货架编码名称");
information.setBjType("BJ");
LuceneUtils.createData(information);
String str6 = "实际";
String[] fields1 = {"actualStorageRoom","brandName"};
LuceneUtils.getDataList(str6, fields1, false, null, false, null);
//更新
Information information2 = new Information();
information2.setActualStorageId(400);
information2.setDeviceId(80001);
information2.setBrandId(80001);
information2.setNetId(80001);
information2.setBjSpecialtyId(400);
information2.setDeviceType("INTEL");
information2.setBjStorageId(2);
information2.setActualStorageRoom("更新库房名称");
information2.setOriginalSerialNum("序列号");
information2.setState1Cd("001");
information2.setState2Cd("002");
information2.setIsStocktake("true");
information2.setDeviceModel("设备型号");
information2.setFrontPic("/pm/tp.png");
information2.setBrandName("银行名称");
information2.setNetType("网元类型");
information2.setNetName("网元名称");
information2.setSpecialtyName("专业类别");
information2.setShelfId(80001);
information2.setShelfName("货架编码名称");
information2.setBjType("BJ");
LuceneUtils.updateData(information2);
String str2 = "实际";
String[] fields2 = {"actualStorageRoom","brandName"};
LuceneUtils.getDataList(str2, fields2, false, null, false, null);
//删除
LuceneUtils.deleteData("600");
String str3 = "实际";
String[] fields3 = {"actualStorageRoom","brandName"};
LuceneUtils.getDataList(str3, fields3, false, null, false, null);
}
}
LuceneUtils.java
package com.zbiti.lucene;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
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.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.CachingWrapperFilter;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryWrapperFilter;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import com.zbiti.util.sys.JsonUtils;
/**
* Lucene 辅助类
* @author 张明
*
*/
public class LuceneUtils {
//读取数据放入的地址
private static String IndexPath = "C:\\update\\index";
static Directory dir = null;
//初始化静态代码块
static{
try {
dir = FSDirectory.open(new File(IndexPath));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建本地文件
* @param list 传递过来的数据是一个list
* @throws Exception
*/
public static boolean createData(List<Information> list){
try {
Directory dir =FSDirectory.open(new File(IndexPath));
IndexWriterConfig writerConfig=new IndexWriterConfig(Version.LUCENE_36, new IKAnalyzer());
//设置每次创建都重新创建
writerConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
IndexWriter writer = new IndexWriter(dir, writerConfig) ;
//循环处理将数据放入Lucene中
for(int i=0; i< list.size(); i++){
Information information = list.get(i);
Document doc = new Document();
//设置字段
setField(information, doc);
writer.addDocument(doc);
}
//提交
writer.optimize();
writer.commit();
writer.close();
return true;
} catch (CorruptIndexException e) {
e.printStackTrace();
return false;
} catch (LockObtainFailedException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 创建一条数据
* @param information 一条数据,传送过来的数据必须是完整的数据
* @return
*/
public static boolean createData(Information information){
try {
Directory dir =FSDirectory.open(new File(IndexPath));
IndexWriterConfig writerConfig=new IndexWriterConfig(Version.LUCENE_36, new IKAnalyzer());
IndexWriter writer = new IndexWriter(dir, writerConfig);
Document doc = new Document();
//设置字段
setField(information, doc);
writer.addDocument(doc);
writer.optimize();
writer.commit();
writer.close();
return true;
} catch (CorruptIndexException e) {
e.printStackTrace();
return false;
} catch (LockObtainFailedException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 更新一条信息
* @param information 根据bjStorageId的唯一性来更新数据
* 传送过来的数据必须是完整的数据,Lucene的更新不同于sql更新,更新是一天信息的完整更新
* @return
*/
public static boolean updateData(Information information){
try {
Directory dir =FSDirectory.open(new File(IndexPath));
//第三个参数代表更新的时候不重新创建原本的数据
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_CURRENT), false, MaxFieldLength.UNLIMITED);
Document doc = new Document();
//设置字段
setField(information, doc);
Term term = new Term("bjStorageId",information.getBjStorageId().toString());
writer.updateDocument(term,doc);
writer.optimize();
writer.commit();
writer.close();
return true;
} catch (CorruptIndexException e) {
e.printStackTrace();
return false;
} catch (LockObtainFailedException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 删除一条数据
* @return 根据bjStorageId的唯一性来删除数据
*/
public static boolean deleteData(String bjStorageId){
try {
Directory dir =FSDirectory.open(new File(IndexPath));
//第三个参数代表更新的时候不重新创建原本的数据
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_CURRENT), false, MaxFieldLength.UNLIMITED);
Term term = new Term("bjStorageId", bjStorageId);
writer.deleteDocuments(term);
writer.optimize();
writer.commit();
writer.close();
return true;
} catch (CorruptIndexException e) {
e.printStackTrace();
return false;
} catch (LockObtainFailedException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 查询数据
* @param searchStr 即将要查询的字符串
* @param fieldStr 需要查询的字段数组
* @param flag 是否统计标志
* @param groupStr 分组字段数组
* @param flagAcc 是否根据条件精确查询
* @param mapFilter查询条件
* @return
*/
public static Map<String,Object> getDataList(String searchStr, String[] fieldArray, boolean flag, String[] groupArray, boolean flagAcc, Map<String,Object> mapFilter){
try {
IndexSearcher search = new IndexSearcher(dir);
System.out.println("lucene中的数据总数为:------->>>" + search.maxDoc());
QueryParser qp=new MultiFieldQueryParser(Version.LUCENE_36, fieldArray, new IKAnalyzer());
Query query=qp.parse(searchStr);
//一次查询多少个结果
//TopDocs tDocs=search.search(query,1000000);
TopDocs tDocs = null;
//是否二次查询,根据多条件精确查找
if(flagAcc){
LuceneFilter luceneFilter = new LuceneFilter();
Set<String> keySet = mapFilter.keySet();
for (String key : keySet)
luceneFilter.addFilter(key, mapFilter.get(key).toString());
query=luceneFilter.getFilterQuery(query);//结果过滤
tDocs = search.search(query,1000000);
}else{
tDocs=search.search(query,1000000);
}
int numTotalHits = tDocs.totalHits;
System.out.println("lucene中的搜索出来的数据总数为:------->>>" + numTotalHits);
Map<String,Object> map = new HashMap<String,Object>();
map.put("information", tDocs);
map.put("search", search);
//是否同时查询分组统计数据
if(flag){
for(int j=0; j < groupArray.length; j++){
JSONArray jsonArr = new JSONArray();
JSONObject jsonObj = new JSONObject();
Map<String,Object> groupMap = getDataByGroupBy(search, query, groupArray[j]);
Set<String> keySet = groupMap.keySet();
for (String key : keySet) {
jsonObj.put("id",key);
jsonObj.put("count", groupMap.get(key));
jsonArr.add(jsonObj);
}
map.put(groupArray[j],jsonArr);
}
}
//解析数据
// Document doc = null;
// for(int i=0; i<numTotalHits; i++){
// int k = tDocs.scoreDocs[i].doc ; //文档内部编号
// doc = search.doc(k);
// System.out.println(doc.get("name") +"--" + doc.get("id") + "-----" + doc.get("type") + "---" + doc.get("testField"));
// }
// search.close();
return map;
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据field指定的字段来进行分组统计总数
* @param search
* @param query
* @param field
*/
public static Map<String,Object> getDataByGroupBy(IndexSearcher search, Query query, String field){
try {
GroupCollector myCollector = new GroupCollector();
myCollector.setF(field);
search.search(query, myCollector);
GroupField gf = myCollector.getGroupField();
List<String> values = gf.getValues();
//读取数据
Map<String,Object> map = new HashMap<String,Object>();
for (String value : values) {
map.put(value, gf.getCountMap().get(value));
}
return map;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 设置字段信息
* @param information
* NOT_ANALYZED不支持模糊查找, ANALYZED支持模糊查找
*/
public static void setField(Information information, Document doc){
doc.add(new Field("actualStorageId", information.getActualStorageId()!=null?information.getActualStorageId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("deviceId", information.getDeviceId()!=null?information.getDeviceId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("brandId", information.getBrandId()!=null?information.getBrandId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("netId", information.getNetId()!=null?information.getNetId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("bjSpecialtyId", information.getBjSpecialtyId()!=null?information.getBjSpecialtyId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("deviceType", information.getDeviceType()!=null?information.getDeviceType().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("bjStorageId", information.getBjStorageId()!=null?information.getBjStorageId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("bjType", information.getBjType()!=null?information.getBjType().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("state1Cd", information.getState1Cd()!=null?information.getState1Cd().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("state2Cd", information.getState2Cd()!=null?information.getState2Cd().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("isStocktake", information.getIsStocktake()!=null?information.getIsStocktake().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("shelfId", information.getShelfId()!=null?information.getShelfId().toString():"", Field.Store.YES, Field.Index.NOT_ANALYZED));
/*==================================================================================================================*/
doc.add(new Field("actualStorageRoom", information.getActualStorageRoom()!=null?information.getActualStorageRoom().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("originalSerialNum", information.getOriginalSerialNum()!=null?information.getOriginalSerialNum().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("deviceModel", information.getDeviceModel()!=null?information.getDeviceModel().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("frontPic", information.getFrontPic()!=null?information.getFrontPic().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("brandName", information.getBrandName()!=null?information.getBrandName().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("netType", information.getNetType()!=null?information.getNetType().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("netName", information.getNetName()!=null?information.getNetName().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("specialtyName", information.getSpecialtyName()!=null?information.getSpecialtyName().toString():"", Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("shelfCode", information.getShelfName()!=null?information.getShelfName().toString():"", Field.Store.YES, Field.Index.ANALYZED));
}
public static void main(String[] args) {
}
}
//辅助类
LuceneFilter.java
package com.zbiti.lucene;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.FilteredQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryWrapperFilter;
import org.apache.lucene.search.TermQuery;
/**
* 条件辅助类
* @author 张明
*
*/
public class LuceneFilter {
private List<Filter> filterList;
public LuceneFilter(){
filterList = new ArrayList<Filter>();
}
public void addFilter(String Field,String Value){
Term term=new Term(Field,Value);//添加term
QueryWrapperFilter filter=new QueryWrapperFilter(new TermQuery(term));//添加过滤器
filterList.add(filter);//加入List,可以增加多個过滤
}
public Query getFilterQuery(Query query){
for(int i=0;i<filterList.size();i++){
//取出多個过滤器,在结果中再次定位结果
query = new FilteredQuery(query, filterList.get(i));
}
return query;
}
}
Information.java
package com.zbiti.lucene;
/**
* Bean
*
* @author 张明
*
*/
public class Information {
/**
* 实物库房ID(分组字段)
*/
private Integer actualStorageId;
/**
* 设备型号ID(分组字段)
*/
private Integer deviceId;
/**
* 品牌ID(分组字段)
*/
private Integer brandId;
/**
* 网元ID(分组字段)
*/
private Integer netId;
/**
* 专业ID(分组字段)
*/
private Integer bjSpecialtyId;
/**
* 设备类型(分组字段)
*/
private String deviceType;
/**
* 备件编号(分组字段)
*/
private Integer bjStorageId;
/** *******************区分字段******************* */
/**
* 备件类型(区分备件-BJ,送修件SXJ)(分组字段)
*/
private String bjType;
/**
* 资产库房备件状态(分组字段)
*/
private String state1Cd;
/**
* 实物库房备件状态(分组字段)
*/
private String state2Cd;
/**
* 是否已盘点(分组字段)
*/
private String isStocktake;
/**
* 网元类型(分组字段)
*/
private String netType;
/**
* 货架ID(分组字段)
*/
private Integer shelfId;
/*=======================================================*/
/**
* 实物库房
*/
private String actualStorageRoom;
/**
* 序列号(送修件原序列号)
*/
private String originalSerialNum;
/**
* 设备型号
*/
private String deviceModel;
/**
* 设备前图片
*/
private String frontPic;
/**
* 品牌名称
*/
private String brandName;
/**
* 网元名称
*/
private String netName;
/**
* 专业类别
*/
private String specialtyName;
/**
* 货架名称
*/
private String shelfName;
public Integer getBjStorageId() {
return bjStorageId;
}
public void setBjStorageId(Integer bjStorageId) {
this.bjStorageId = bjStorageId;
}
public Integer getActualStorageId() {
return actualStorageId;
}
public void setActualStorageId(Integer actualStorageId) {
this.actualStorageId = actualStorageId;
}
public String getActualStorageRoom() {
return actualStorageRoom;
}
public void setActualStorageRoom(String actualStorageRoom) {
this.actualStorageRoom = actualStorageRoom;
}
public String getOriginalSerialNum() {
return originalSerialNum;
}
public void setOriginalSerialNum(String originalSerialNum) {
this.originalSerialNum = originalSerialNum;
}
public String getState1Cd() {
return state1Cd;
}
public void setState1Cd(String state1Cd) {
this.state1Cd = state1Cd;
}
public String getState2Cd() {
return state2Cd;
}
public void setState2Cd(String state2Cd) {
this.state2Cd = state2Cd;
}
public String getIsStocktake() {
return isStocktake;
}
public void setIsStocktake(String isStocktake) {
this.isStocktake = isStocktake;
}
public Integer getDeviceId() {
return deviceId;
}
public void setDeviceId(Integer deviceId) {
this.deviceId = deviceId;
}
public String getDeviceModel() {
return deviceModel;
}
public void setDeviceModel(String deviceModel) {
this.deviceModel = deviceModel;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getFrontPic() {
return frontPic;
}
public void setFrontPic(String frontPic) {
this.frontPic = frontPic;
}
public Integer getBrandId() {
return brandId;
}
public void setBrandId(Integer brandId) {
this.brandId = brandId;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public Integer getNetId() {
return netId;
}
public void setNetId(Integer netId) {
this.netId = netId;
}
public String getNetType() {
return netType;
}
public void setNetType(String netType) {
this.netType = netType;
}
public String getNetName() {
return netName;
}
public void setNetName(String netName) {
this.netName = netName;
}
public Integer getBjSpecialtyId() {
return bjSpecialtyId;
}
public void setBjSpecialtyId(Integer bjSpecialtyId) {
this.bjSpecialtyId = bjSpecialtyId;
}
public String getSpecialtyName() {
return specialtyName;
}
public void setSpecialtyName(String specialtyName) {
this.specialtyName = specialtyName;
}
public Integer getShelfId() {
return shelfId;
}
public void setShelfId(Integer shelfId) {
this.shelfId = shelfId;
}
public String getShelfName() {
return shelfName;
}
public void setShelfName(String shelfName) {
this.shelfName = shelfName;
}
public String getBjType() {
return bjType;
}
public void setBjType(String bjType) {
this.bjType = bjType;
}
}
GroupField.java
package com.zbiti.lucene;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author 张明
*
*/
public class GroupField {
// 所有可能的分组字段值,排序按每个字段值的文档个数大小排序
private List<String> values = new ArrayList<String>();
// 保存字段值和文档个数的对应关系
private Map<String, Integer> countMap = new HashMap<String, Integer>();
public Map<String, Integer> getCountMap() {
return countMap;
}
public void setCountMap(Map<String, Integer> countMap) {
this.countMap = countMap;
}
public List<String> getValues() {
Collections.sort(values, new ValueComparator());
return values;
}
public void setValues(List<String> values) {
this.values = values;
}
public void addValue(String value) {
if (value == null || "".equals(value))
return;
if (countMap.get(value) == null) {
countMap.put(value, 1);
values.add(value);
}else{
countMap.put(value, countMap.get(value) + 1);
}
}
class ValueComparator implements Comparator<String> {
public int compare(String value0, String value1) {
if (countMap.get(value0) > countMap.get(value1)) {
return -1;
} else if (countMap.get(value0) < countMap.get(value1)) {
return 1;
}
return 0;
}
}
}
GroupCollector.java
package com.zbiti.lucene;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.FieldCache;
import org.apache.lucene.search.Scorer;
/**
*
* @author 张明
*
*/
public class GroupCollector extends Collector{
// 保存分组统计结果
private GroupField groupField = new GroupField();
//fieldCache
private String[] fieldCache;
//统计字段
private String field;
String spliter;
int length;
public void setFc(String[] fieldCache) {
this.fieldCache = fieldCache;
}
@Override
public void setScorer(Scorer scorer) throws IOException {
}
@Override
public void setNextReader(IndexReader reader, int docBase)
throws IOException {
fieldCache = FieldCache.DEFAULT.getStrings(reader, field);
}
@Override
public void collect(int doc) throws IOException {
// 添加的GroupField中,由GroupField负责统计每个不同值的数目
groupField.addValue(fieldCache[doc]);
}
@Override
public boolean acceptsDocsOutOfOrder() {
return true;
}
public GroupField getGroupField() {
return groupField;
}
public void setSpliter(String spliter) {
this.spliter = spliter;
}
public void setLength(int length) {
this.length = length;
}
public void setF(String field) {
this.field = field;
}
}
//测试类
package com.zbiti.lucene;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TopDocs;
public class TestLucene {
public static void main(String[] args) throws CorruptIndexException, IOException {
//创建
List<Information> list = new ArrayList<Information>();
for(int i=0; i< 10000; i++){
Information information = new Information();
information.setActualStorageId(i);
information.setDeviceId(i);
if(i<300)
information.setBrandId(8888);
else
information.setBrandId(7777);
if(i < 500)
information.setNetId(100);
else
information.setNetId(200);
information.setBjSpecialtyId(i);
if(i <= 100)
information.setDeviceType("AMD");
else
information.setDeviceType("INTEL");
information.setBjStorageId(i);
if(i <= 20)
information.setActualStorageRoom("实际库房名称");
else
information.setActualStorageRoom("库房名称");
information.setOriginalSerialNum("序列号");
information.setState1Cd("001");
information.setState2Cd("002");
information.setIsStocktake("true");
information.setDeviceModel("设备型号");
information.setFrontPic("/pm/tp.png");
if(i <= 600 && i > 500)
information.setBrandName("实际银行名称");
else
information.setBrandName("银行名称");
information.setNetType("网元类型");
information.setNetName("网元名称");
information.setSpecialtyName("专业类别");
information.setShelfId(i);
information.setShelfName("货架编码名称");
information.setBjType("BJ");
list.add(information);
}
LuceneUtils.createData(list);
//====================================================================================================
//查询
// String[] fields = {"netId"};
// String str = "100";
String[] fields = {"actualStorageRoom","brandName"};
// //不分组统计
Map<String,Object> mapFilter = new HashMap<String,Object>();
Map<String,Object> map = LuceneUtils.getDataList("实际", fields, false, null, false, mapFilter);
TopDocs topDocs = (TopDocs)map.get("information");
IndexSearcher search = (IndexSearcher)map.get("search");
// Document doc = null;
// //取数据字段内容
// for(int i=0; i<topDocs.totalHits; i++){
// int k = topDocs.scoreDocs[i].doc ; //文档内部编号
// doc = search.doc(k);
// System.out.println(doc.get("actualStorageRoom") +"--" + doc.get("brandName") + "----" + doc.get("netId")) ;
// }
// search.close();
//======================================================================================================
// //分组统计
String str5 = "100";
String[] fields5 = {"netId"};
String[] groupFields = {"deviceType"};
Map<String,Object> mapGroup = LuceneUtils.getDataList(str5, fields5, true, groupFields, false, null);
System.out.println("统计信息 ------->>> " + mapGroup.get("deviceType"));
search.close();
//=======================================================================================================
//新增
Information information = new Information();
information.setActualStorageId(80001);
information.setDeviceId(80001);
information.setBrandId(80001);
information.setNetId(80001);
information.setBjSpecialtyId(80001);
information.setDeviceType("INTEL");
information.setBjStorageId(80001);
information.setActualStorageRoom("实际库房名称");
information.setOriginalSerialNum("序列号");
information.setState1Cd("001");
information.setState2Cd("002");
information.setIsStocktake("true");
information.setDeviceModel("设备型号");
information.setFrontPic("/pm/tp.png");
information.setBrandName("银行名称");
information.setNetType("网元类型");
information.setNetName("网元名称");
information.setSpecialtyName("专业类别");
information.setShelfId(80001);
information.setShelfName("货架编码名称");
information.setBjType("BJ");
LuceneUtils.createData(information);
String str6 = "实际";
String[] fields1 = {"actualStorageRoom","brandName"};
LuceneUtils.getDataList(str6, fields1, false, null, false, null);
//更新
Information information2 = new Information();
information2.setActualStorageId(400);
information2.setDeviceId(80001);
information2.setBrandId(80001);
information2.setNetId(80001);
information2.setBjSpecialtyId(400);
information2.setDeviceType("INTEL");
information2.setBjStorageId(2);
information2.setActualStorageRoom("更新库房名称");
information2.setOriginalSerialNum("序列号");
information2.setState1Cd("001");
information2.setState2Cd("002");
information2.setIsStocktake("true");
information2.setDeviceModel("设备型号");
information2.setFrontPic("/pm/tp.png");
information2.setBrandName("银行名称");
information2.setNetType("网元类型");
information2.setNetName("网元名称");
information2.setSpecialtyName("专业类别");
information2.setShelfId(80001);
information2.setShelfName("货架编码名称");
information2.setBjType("BJ");
LuceneUtils.updateData(information2);
String str2 = "实际";
String[] fields2 = {"actualStorageRoom","brandName"};
LuceneUtils.getDataList(str2, fields2, false, null, false, null);
//删除
LuceneUtils.deleteData("600");
String str3 = "实际";
String[] fields3 = {"actualStorageRoom","brandName"};
LuceneUtils.getDataList(str3, fields3, false, null, false, null);
}
}
发表评论
-
JAVA解决多个域名配置问题
2016-06-02 15:55 1091CROSS_DOMAIN=http:// ... -
java final浅析(转载)
2015-01-20 13:43 678谈到final关键字,想必很多人都不陌生,在使用匿名内部类的 ... -
Redis
2014-11-25 17:44 0package com.agileeagle.gf.frie ... -
API文档集合地址
2014-08-23 22:03 762http://tool.oschina.net/apidocs ... -
封装属于自己的JS库
2013-03-22 13:34 5172(function(){ if(!window.projec ... -
WebLogic下一个ear包部署两个war共享session问题
2012-11-06 11:06 1135网上找了好多的资料,对于我这种刚刚接触webLogic的人来说 ...
相关推荐
Lucene是一个高性能、全文本搜索库,而IKAnalyzer是一个专门针对中文分词的开源分析器,它们的结合能够有效地帮助我们处理中文文本的搜索需求。 首先,我们需要了解Lucene的基本概念。Lucene提供了完整的搜索索引...
- 通过 Lucene,开发者可以实现快速、精确的全文搜索,包括布尔查询、短语查询和模糊查询等。 2. **IKAnalyzer** - IKAnalyzer 是一个针对中文的分词器,专为 Lucene 和 Solr 设计。 - 它支持动态词典更新,可以...
例如,若文档由多个字段组成,如标题(Title)和内容(Content),则可以通过“Title:Lucene AND Content:Java”这样的查询,精确查找标题中包含“Lucene”,同时内容中包含“Java”的文档。 #### 代码示例 以下是...
7. **搜索(Search)**:Lucene支持多种搜索类型,如布尔查询、短语查询、范围查询等,可以灵活地组合条件进行精确或模糊匹配。 8. **评分(Scoring)**:Lucene会根据多个因素(如TF-IDF,词项频率等)为每个匹配...
在测试程序中,开发者可能会编写代码来模拟不同的查询场景,比如精确匹配、模糊匹配或者短语查询,以验证标准分词器的效果。此外,还可能涉及到评分策略、排序和高亮显示等高级特性,这些都是提升用户体验的重要部分...
- **搜索功能**:提供灵活的搜索API,可以进行精确匹配、模糊搜索、范围搜索等多种类型的查询。 - **排序与评分**:支持对搜索结果进行排序,通常基于相关性评分,该评分反映了查询词与文档的匹配程度。 - **高亮...
Lucene是一款强大的全文搜索引擎库,广泛应用于各种数据检索场景。在Lucene中,表达式查询是一种高级查询方式,允许用户通过特定的语法构造复杂的查询条件。本篇文章将深入探讨Lucene表达式处理查询的原理、语法及...
Lucene 4.10是Apache Lucene项目的一个重要版本,它是一个高性能、全文本搜索引擎库,广泛应用于各种信息检索系统中。这个版本在索引效率、查询性能以及搜索精度上都有显著提升,为开发者提供了强大的文本处理能力。...
Lucene是一个开源的全文搜索引擎库,由Apache软件基金会开发并维护。它提供了高效的文本检索、分析和存储功能,被广泛应用于各种需要全文搜索的应用中。本篇将通过一个简单的入门例子,带你了解如何使用Lucene进行...
Lucene是一个强大的全文检索库,由Apache软件基金会开发并维护,它为构建高效、可扩展的搜索功能提供了核心工具。在这个“lucene简单搜索引擎”项目中,我们能够看到如何利用Lucene来实现一个简易版的搜索引擎,类似...
《深入理解Windows Lucene 8.11.0:全文搜索引擎的卓越之旅》 Lucene是Apache软件基金会的一个开源项目,它是一个强大的、高性能的全文搜索引擎库,被广泛应用于各种搜索应用开发中。Windows Lucene 8.11.0是Lucene...
1. **全文搜索**:支持对文档内容的精确匹配、模糊匹配和短语搜索。 2. **排序和评分**:可以根据相关性对搜索结果进行排序,其中 TF-IDF(词频-逆文档频率)是常用的评分机制。 3. **多语言支持**:Lucene 提供了...
1. 全文检索:支持模糊匹配和精确匹配,提供多种查询语法。 2. 倒排索引:通过索引项查找文档,提高检索效率。 3. 排序:根据指定字段对搜索结果进行排序。 4. 分页:实现搜索结果的分页展示,优化用户体验。 5. ...
**正文** Lucene是一个强大的全文检索库,由Apache软件基金会开发并维护,它为Java开发者提供了...同时,Lucene也支持高级特性,如短语搜索、模糊搜索、评分系统、近实时搜索等,使得它成为企业级搜索解决方案的首选。
Lucene是一个开源的全文搜索引擎库,它提供了强大的索引和搜索功能,被广泛应用于各类文档的海量搜索系统中。在"Lucene4.X实战类baidu搜索的大型文档海量搜索系统"课程中,涵盖了一系列关于Lucene的核心知识点,包括...
Lucene提供了丰富的搜索功能,包括基本的全文搜索、布尔搜索、模糊搜索、范围查询等。此外,还支持排序、高亮显示等功能,使得搜索结果更加精确和易于理解。 #### 性能优化 为了提高搜索效率,Lucene采用了一系列的...
- **模糊搜索**: 允许用户输入不精确的查询,如使用通配符或近似搜索。 - **范围查询**: 可以根据数值或日期范围筛选结果。 - **短语搜索**: 查找连续出现的多个词组。 - **高亮显示**: 在搜索结果中突出显示...