📄 contactinfosearcher.java
字号:
import contacts.ContactInfo;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.search.Hits;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import java.io.IOException;public class ContactInfoSearcher { String indexDir = "index"; // directory storing index files public static void main(String[] args) throws Exception { ContactInfo joe = new ContactInfo(); joe.setFirstName("Joe"); joe.setLastName("Walnes"); joe.setId(1); ContactInfo ara = new ContactInfo(); ara.setFirstName("Ara"); ara.setLastName("Abrahamian"); ara.setId(2); ContactInfoSearcher searcher = new ContactInfoSearcher(); searcher.createIndexDirectory(); searcher.index(joe); searcher.index(ara); searcher.unIndex(ara); searcher.reIndex(ara); // perform search Hits hits = searcher.search("firstName", "joe or fred"); if (hits.length() == 0) { // no results found System.out.println("No results found"); } else { // iterate over results for(int i = 0; i < hits.length(); i++) { Document document = hits.doc(i); System.out.println("--- Result " + i); System.out.println("First Name: " + document.get("firstName")); System.out.println("Last Name : " + document.get("lastName")); System.out.println("ID : " + document.get("id")); System.out.println("Score : " + hits.score(i)); } } } private void createIndexDirectory() { try { new IndexWriter(indexDir, null, true).close(); } catch (IOException e) { e.printStackTrace(); } } private IndexWriter openIndexWriter() throws IOException { Analyzer analyzer = new StandardAnalyzer(); return new IndexWriter(indexDir, analyzer, false); } private Document buildDocument(ContactInfo contact) { Document document= new Document(); document.add(Field.Keyword("id", String.valueOf(contact.getId()))); document.add(Field.Text("firstName", contact.getFirstName())); document.add(Field.Text("lastName", contact.getLastName())); return document; } private void index(ContactInfo contact) throws IOException { IndexWriter writer = openIndexWriter(); Document document = buildDocument(contact); writer.addDocument(document); writer.optimize(); writer.close(); } public void unIndex (ContactInfo contact) throws IOException { IndexReader reader = IndexReader.open(indexDir); try { Term term = new Term("id", String.valueOf(contact.getId())); reader.delete(term); } finally { reader.close(); } } public void reIndex(ContactInfo contact) throws IOException { unIndex(contact); index(contact); } public Hits search(String fieldname, String criteria) throws ParseException, IOException { // open IndexSearcher IndexSearcher searcher = new IndexSearcher(indexDir); try { Query query = buildQuery(fieldname, criteria); Hits hits = searcher.search(query); return hits; } finally {// searcher.close(); } } private Query buildQuery(String fieldName, String criteria) throws ParseException { Analyzer analyzer = new StandardAnalyzer(); QueryParser parser = new QueryParser(fieldName, analyzer); return parser.parse(criteria); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -