如下所示是我索引数据的方法:
public void getAvailableItems(String sql) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
IndexWriter writer=null;
File file = null;
try{
file = new File(LUCENE_INDEX_DIRECTORY);
analyzer = new WhitespaceAnalyzer(Version.LUCENE_CURRENT);
writer = new IndexWriter(
FSDirectory.open(file),
analyzer,
true,
IndexWriter.MaxFieldLength.LIMITED
);
Class.forName("com.mysql.jdbc.Driver").newInstance();
//get connection object
con = DriverManager.getConnection(
"jdbc:mysql://"+DB_HOST_NAME+"/evergoldbuilders", DB_USER_NAME, DB_PASSWORD);
//create statement object
stmt = con.createStatement();
//execute query
rs = stmt.executeQuery(sql);
//iterate through result set
while(rs.next()){
String name = rs.getString("category_name").trim() + " " + rs.getString("sub_category_name").trim() + " " + rs.getString("classification_name").trim() + " " + rs.getString("item_name").trim();
Document document = new Document();
Field nameField = new Field("item_complete_name", name, Field.Store.YES, Field.Index.ANALYZED);
document.add(nameField);
writer.addDocument(document);
}
writer.optimize();
}catch(Exception e){
e.printStackTrace();
}
}
以及我搜索关键字的方法:
public void searchItem(String column, String search) throws Exception{
ScoreDoc[] hits = null;
QueryParser parser = null;
Query q = null;
int hitsPerPage = 50;
analyzer = new WhitespaceAnalyzer(Version.LUCENE_CURRENT);
File files = new File(LUCENE_INDEX_DIRECTORY);
IndexReader reader = IndexReader.open(FSDirectory.open(files),true);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
parser = new QueryParser(Version.LUCENE_CURRENT, "item_complete_name", analyzer);
q = parser.parse(search + "*");
searcher.search(q, collector);
hits = collector.topDocs().scoreDocs;
System.out.println("Found " + hits.length + " hits.");
count = 0;
for(int i=0;i<hits.length;++i) {
isFound = true;
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println(d.getField("item_complete_name").stringValue());
count++;
}
searcher.close();
}
最后,我的样本数据已编入索引:
ALUMINUM 4'/O U.S. ALUMINUM
ALUMINUM 4" CHINA ALUMINUM
ALUMINUM 3'/O U.S. ALUMINUM
ALUMINUM 3"A CHINA ALUMINUM
PAINTS DAVIES 4 LITERS DV 472 HI-HEAT RESISTING ALUMINUM (1200°F)
PAINTS DAVIES 4 LITERS DV 470 SILVER FINISH ALUMINUM
我的问题是,无论何时我搜索“alum *”,都没有找到搜索结果,但“铝”确实如此。未找到“铝和瓷*”的搜索结果。我可以使用lucene通配符(即*和?)来使用空白分析器搜索索引数据吗?空白分析器是否标记非字母?我想要一个分析器来标记我的空间数据。是空白分析仪正确的使用?非常感谢!
答案 0 :(得分:0)
使用org.apache.lucene.analysis.standard。 StandardAnalyzer 代替WhitespaceAnalyzer
可以解决问题。