(1)创建索引
package com.lucene.index;
import java.io.File;
import java.io.IOException;
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.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import com.lucene.vo.ProductList;
public class CreateIndex {
public void createIndex(List<ProductList> pList,String path){
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
IndexWriterConfig indexWriteConfig = new IndexWriterConfig(Version.LUCENE_31, analyzer);
IndexWriter indexWriter = null ;
try {
Directory directory = new SimpleFSDirectory(new File(path));
indexWriter = new IndexWriter(directory, indexWriteConfig);
for (ProductList productList : pList) {
Document document = new Document();
Field id = new Field("id",productList.getId().toString(),Store.YES,Index.ANALYZED);
Field productName = new Field("productName", productList.getProductName(), Store.YES, Index.ANALYZED);
Field productPrice = new Field("productPrice", productList.getProductPrice().toString(), Store.YES, Index.NOT_ANALYZED);
Field catalogName = new Field("catalogName", productList.getCatalogName(), Store.YES, Index.ANALYZED);
document.add(id);
document.add(productName);
document.add(productPrice);
document.add(catalogName);
indexWriter.addDocument(document);
}
indexWriter.commit();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(2)读索引
package com.lucene.index;
import java.io.File;
import java.io.IOException;
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.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class SearchIndex {
public void search(String indexPath,String[] field,String quaryString){
FSDirectory directory = null ;
try {
directory = FSDirectory.open(new File(indexPath));
IndexReader reader = IndexReader.open(directory);
IndexSearcher search = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
MultiFieldQueryParser queryParse = new MultiFieldQueryParser(Version.LUCENE_31, field, analyzer);
Query query = queryParse.parse(quaryString);
ScoreDoc[] scoreDocs = search.search(query, 1000).scoreDocs;
System.out.println("找到的记录条数:"+scoreDocs.length);
for (ScoreDoc scoreDoc : scoreDocs) {
Document document = search.doc(scoreDoc.doc);
System.out.println(document.get("id")+":"+document.get("productName")+":"+document.get("productPrice")+":"+document.get("catalogName"));
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}