package com.lucene.LuceneTest.memery; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.Field.Index; import org.apache.lucene.document.Field.Store; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause.Occur; 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.apache.lucene.util.Version; public class AppIndex { private Directory directory; public AppIndex() throws Exception { directory=FSDirectory.open(new File("E:/index/appIndex")); } public IndexWriter getWriter() throws Exception{ return new IndexWriter(directory,new StandardAnalyzer(Version.LUCENE_30),false,IndexWriter.MaxFieldLength.UNLIMITED); } //初始化索引 public void initIndex() throws Exception{ IndexWriter writer=new IndexWriter(directory,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED); File[] files=new File("D:/test_data/result1").listFiles(); for(int i=0;i<files.length;i++){ File f=files[i]; System.out.println(f.getName()); BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f))); String line=""; br.readLine(); while((line=br.readLine())!=null){ String[] arrStr=line.split("\",\""); if(arrStr.length>3){ Document doc=new Document(); doc.add(new Field("appid",arrStr[0].replaceAll("\"", "").trim(),Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("onlineDay",arrStr[1].trim(),Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("sellerUrl",arrStr[2].trim(),Field.Store.YES,Field.Index.NO)); doc.add(new Field("sellerName",arrStr[7].trim(),Field.Store.YES,Field.Index.ANALYZED)); //发布时间 String dateStr=arrStr[6].replaceAll("[TZ]", " ").trim(); doc.add(new Field("releaseDate",dateStr,Field.Store.YES,Field.Index.NOT_ANALYZED)); writer.addDocument(doc); } } } writer.close(); System.out.println("总共索引"+writer.numDocs()+"条!"); } //添加文档 public int addDocument(Document doc) throws Exception{ IndexWriter writer=getWriter(); writer.addDocument(doc); writer.close(); return writer.numDocs(); } //删除文档 public int removeDocument(String appid) throws Exception{ IndexWriter writer=getWriter(); System.out.println(writer.numDocs()); Term term=new Term("appid",appid); writer.deleteDocuments(term); writer.commit(); int delNum=writer.maxDoc()-writer.numDocs(); writer.optimize();//优化合并 writer.close(); return delNum; } public void updateDoc(String updateStr) throws Exception{ IndexWriter writer=getWriter(); System.out.println("numDocs"+writer.numDocs()); String[] cons=updateStr.split("\\|"); String s=updateStr.substring(updateStr.indexOf("appid=")+6); String appid=s.substring(0,s.indexOf("|")).replaceAll("\"",""); System.out.println("appid-->"+appid); for(Document doc:this.searchDoc("appid="+appid)){ for(int i=0;i<cons.length;i++){ String[] unitStr=cons[i].split("="); Store store=Field.Store.YES; Index index=Field.Index.ANALYZED; if("sellerName".equals(unitStr[0])){ store=Field.Store.YES; index=Field.Index.ANALYZED; // doc.removeField("sellerName"); }else if("sellerUrl".equals(unitStr[0])){ store=Field.Store.YES; index=Field.Index.NOT_ANALYZED; // doc.removeField("sellerUrl"); }else if("releaseDate".equals(unitStr[0])){ store=Field.Store.YES; index=Field.Index.NOT_ANALYZED; // doc.removeField("releaseDate"); }else if("onlineDay".equals(unitStr[0])){ store=Field.Store.YES; index=Field.Index.NOT_ANALYZED; // doc.removeField("onlineDay"); } if(!"appid".equals(unitStr[0])){ Field field=new Field(unitStr[0],unitStr[1].replaceAll("\"", "").trim(),store,index); doc.add(field); } } writer.updateDocument(new Term("appid",appid), doc); writer.commit(); } System.out.println("numDocs"+writer.numDocs()); writer.close(); } //搜索文档 public List<Document> searchDoc(String condition) throws Exception{ //搜索 IndexSearcher searcher=new IndexSearcher(this.directory); // QueryParser parser=new QueryParser(Version.LUCENE_30,"sellerName",new StandardAnalyzer(Version.LUCENE_30)); // Query query=parser.parse(condition); String[] cons=condition.split("\\|"); // String[] fields=new String[cons.length]; String[] queries=new String[cons.length]; Occur[] clauses=new Occur[cons.length]; for(int i=0;i<cons.length;i++){ String[] unitQ=cons[i].split("="); fields[i]=unitQ[0]; queries[i]=unitQ[1].replaceAll("\"", "").trim(); clauses[i]=BooleanClause.Occur.SHOULD; System.out.println(unitQ[0]+" | "+unitQ[1].replaceAll("\"", "").trim()); } Query query = MultiFieldQueryParser.parse(Version.LUCENE_30, queries, fields, clauses, new StandardAnalyzer(Version.LUCENE_30)); TopDocs tds= searcher.search(query,10); List<Document> list=new ArrayList<Document>(); ScoreDoc[] scoreDocs=tds.scoreDocs; for(int i=0;i<scoreDocs.length;i++){ list.add(searcher.doc(scoreDocs[i].doc)); } System.out.println(tds.totalHits); searcher.close(); return list; } public static void main(String[] args) throws Exception { AppIndex appIndex=new AppIndex(); // appIndex.initIndex(); Scanner input=new Scanner(System.in); while(true){ System.out.println("输入1添加索引 输入2搜索 输入3删除指定appid文档 输入4更新\n"); int flag=input.nextInt(); switch(flag){ case 1: System.out.println("输入以下字段用空格格开[appid,onlineDay,sellerUrl,sellerName,releaseDate]\n\n"); String appid=input.next(); String onlineDay=input.next(); String sellerUrl=input.next(); String sellerName=input.next(); String releaseDate=input.next(); Document doc=new Document(); doc.add(new Field("appid",appid,Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("onlineDay",onlineDay,Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("sellerUrl",sellerUrl,Field.Store.YES,Field.Index.NO)); doc.add(new Field("sellerName",sellerName,Field.Store.YES,Field.Index.ANALYZED)); //发布时间 doc.add(new Field("releaseDate",releaseDate,Field.Store.YES,Field.Index.NOT_ANALYZED)); int indexNum=appIndex.addDocument(doc); System.out.println("添加成功!共有索引["+indexNum+"]个"); break; case 2: System.out.println("请输入查询条件:appid=\"\"|onlineDay=\"\"|sellerUrl=\"\"|sellerName=\"\"|releaseDate=\"\""); String condition=input.next(); List<Document> list=appIndex.searchDoc(condition); System.out.println(list.size()); for(Document d:list){ for(Field f:d.getFields("onlineDay")){ System.out.println(f.stringValue()); } System.out.println("[appid="+d.get("appid")+",onlineDay="+d.get("onlineDay")+",sellerUrl="+d.get("sellerUrl")+",sellerName="+d.get("sellerName")+",releaseDate="+d.get("releaseDate")+"]"); } break; case 3: System.out.println("请输入appid\n"); appid=input.next(); int delNum=appIndex.removeDocument(appid); System.out.println("删除"+delNum+"条!"); break; case 4: System.out.println("请输入更新字段和值:appid=\"\"|onlineDay=\"\"|sellerUrl=\"\"|sellerName=\"\"|releaseDate=\"\""); String updateStr=input.next(); appIndex.updateDoc(updateStr); break; default: System.out.println("命令不对重新输入"); } } } }