1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| package com.crossoverJie.lucene;
import com.crossoverJie.pojo.User; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.*; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.*; import org.apache.lucene.search.highlight.*; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory;
import java.io.StringReader; import java.nio.file.Paths; import java.util.LinkedList; import java.util.List; import com.crossoverJie.util.*;
public class LuceneIndex {
private Directory dir=null;
private IndexWriter getWriter()throws Exception{
dir= FSDirectory.open(Paths.get("C://lucene")); SmartChineseAnalyzer analyzer=new SmartChineseAnalyzer(); IndexWriterConfig iwc=new IndexWriterConfig(analyzer); IndexWriter writer=new IndexWriter(dir, iwc); return writer; }
public void addIndex(User user)throws Exception{ IndexWriter writer=getWriter(); Document doc=new Document(); doc.add(new StringField("id",String.valueOf(user.getUserId()), Field.Store.YES));
doc.add(new TextField("username", user.getUsername(), Field.Store.YES)); doc.add(new TextField("description",user.getDescription(), Field.Store.YES)); writer.addDocument(doc); writer.close(); }
public void updateIndex(User user)throws Exception{ IndexWriter writer=getWriter(); Document doc=new Document(); doc.add(new StringField("id",String.valueOf(user.getUserId()), Field.Store.YES)); doc.add(new TextField("username", user.getUsername(), Field.Store.YES)); doc.add(new TextField("description",user.getDescription(), Field.Store.YES)); writer.updateDocument(new Term("id", String.valueOf(user.getUserId())), doc); writer.close(); }
public void deleteIndex(String userId)throws Exception{ IndexWriter writer=getWriter(); writer.deleteDocuments(new Term("id", userId)); writer.forceMergeDeletes(); writer.commit(); writer.close(); }
public List<User> searchBlog(String q)throws Exception{
dir= FSDirectory.open(Paths.get("C://lucene")); IndexReader reader = DirectoryReader.open(dir); IndexSearcher is=new IndexSearcher(reader); BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); SmartChineseAnalyzer analyzer=new SmartChineseAnalyzer();
QueryParser parser=new QueryParser("username",analyzer); Query query=parser.parse(q); QueryParser parser2=new QueryParser("description",analyzer); Query query2=parser2.parse(q); booleanQuery.add(query, BooleanClause.Occur.SHOULD); booleanQuery.add(query2, BooleanClause.Occur.SHOULD); TopDocs hits=is.search(booleanQuery.build(), 100); QueryScorer scorer=new QueryScorer(query); Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
SimpleHTMLFormatter simpleHTMLFormatter=new SimpleHTMLFormatter("<b><font color='red'>","</font></b>"); Highlighter highlighter=new Highlighter(simpleHTMLFormatter, scorer); highlighter.setTextFragmenter(fragmenter); List<User> userList=new LinkedList<User>(); for(ScoreDoc scoreDoc:hits.scoreDocs){ Document doc=is.doc(scoreDoc.doc); User user=new User(); user.setUserId(Integer.parseInt(doc.get(("id")))); user.setDescription(doc.get(("description"))); String username=doc.get("username"); String description=doc.get("description"); if(username!=null){ TokenStream tokenStream = analyzer.tokenStream("username", new StringReader(username)); String husername=highlighter.getBestFragment(tokenStream, username); if(StringUtil.isEmpty(husername)){ user.setUsername(username); }else{ user.setUsername(husername); } } if(description!=null){ TokenStream tokenStream = analyzer.tokenStream("description", new StringReader(description)); String hContent=highlighter.getBestFragment(tokenStream, description); if(StringUtil.isEmpty(hContent)){ if(description.length()<=200){ user.setDescription(description); }else{ user.setDescription(description.substring(0, 200)); } }else{ user.setDescription(hContent); } } userList.add(user); } return userList; } }
|