传说中强大的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.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.LockObtainFailedException;
public class TextFileIndexer {
private IndexWriter indexWriter;
public TextFileIndexer(String directory) {
try {
indexWriter = new IndexWriter(directory, new StandardAnalyzer(),
true);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
TextFileIndexer indexer = new TextFileIndexer("/home/com/tmp/index");
indexer.create("/home/sina", "axu你发三段论法的撒肥撒旦", "了脑门大量的萨考虑我的");
indexer.create("/home/blog", "9726", "java");
// indexer.delete("/home/blog");
indexer.commit();
}
public void create(String path, String title, String content) {
Document document = new Document();
Field fieldPath = new Field("path", path, Field.Store.YES,
Field.Index.NO);
Field fieldTitle = new Field("title", title, Field.Store.YES,
Field.Index.TOKENIZED);
Field fieldContent = new Field("content", content, Field.Store.NO,
Field.Index.TOKENIZED);
document.add(fieldPath);
document.add(fieldTitle);
document.add(fieldContent);
try {
indexWriter.addDocument(document);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void commit() {
try {
indexWriter.optimize();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void delete(String path) {
Term term = new Term("path", path);
try {
indexWriter.deleteDocuments(term);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void update(String path, String title, String content) {
this.delete(path);
this.create(path, title, content);
}
}
然后就可以进行搜索了,搜索创建好的索引:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
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 org.apache.lucene.store.LockObtainFailedException;
public class TextFileSearcher {
private IndexSearcher searcher;
public TextFileSearcher(String directory) {
try {
searcher = new IndexSearcher(directory);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
TextFileSearcher indexer = new TextFileSearcher("/home/com/tmp/index");
List<Document> list = indexer.search("9726");
for (Document document : list) {
System.out.println(document.get("path"));
}
}
public List<Document> search(String keyword) {
List<Document> resultset = new ArrayList<Document>();
Analyzer analyzer = new StandardAnalyzer();
try {
QueryParser parser = new QueryParser(null, analyzer);
Query query = parser.parse("title:" + keyword + " OR content:"
+ keyword);
Hits hits = searcher.search(query);
for (int i = 0; i < hits.length(); i++) {
resultset.add(hits.doc(i));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultset;
}
}