我正在使用 Lucene 6.4.0 并使用以下代码。
但是对于每个查询,它返回 0 结果。代码似乎是正确的,索引也在建立,但我无法知道我哪里出错了。
public void buildIndex(){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
IndexWriter writer=null;
StandardAnalyzer analyzer = null;
analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
try{
System.out.println("Start indexing");
//get a reference to index directory filejdbc:mysql://localhost/MTDATABASE
writer = new IndexWriter(FSDirectory.open(file.toPath()), config);
//initialize the driver class
Class.forName("com.mysql.jdbc.Driver").newInstance();
//get connection object
con = DriverManager.getConnection(
"jdbc:mysql://"+DB_HOST_NAME+"/MTDATABASE",
DB_USER_NAME, DB_PASSWORD);
//create statement object
stmt = con.createStatement();
//execute query
rs = stmt.executeQuery("SELECT * FROM products");
//iterate through result set
while(rs.next()){
productId = rs.getInt("productId");
productImageName = rs.getString("productImageName");
productCategory = rs.getString("productCategory");
productBrandName = rs.getString("productBrandName");
productType = rs.getString("productType");
productName = rs.getString("productName");
prop1 = rs.getString("prop1");
prop2 = rs.getString("prop2");
prop3 = rs.getString("prop3");
prop4 = rs.getString("prop4");
prop5 = rs.getString("prop5");
description = rs.getString("description");
price = rs.getInt("price");
discount = rs.getFloat("discount");
cashback = rs.getFloat("cashback");
availability = rs.getString("availability");
//create a full text field which contains name,
//age and designation
String fulltext = productId + " " + productImageName +
" " + productCategory+" "+productBrandName+" "+productType+
" " + productName + " "+prop1+" "+prop2+" "+prop3+" "+prop4+" "+prop5+
" "+description+" "+price+" "+discount+" "+cashback+" "+availability;
/*doc.add(new StringField("id",
Integer.toString(id), StringField.Store.YES));
doc.add(new TextField("title", title,
TextField.Store.YES));
w.addDocument(doc);*/
//create document object
addDoc(writer,productId,productImageName,productCategory,productBrandName,productType,
productName,prop1,prop2,prop3,prop4,prop5,description,price,discount,cashback,availability);
writer.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(writer!=null)
writer.close();
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
System.out.println("Finished indexing");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
IndexBuilder builder = new IndexBuilder();
builder.buildIndex();
}
private void addDoc(IndexWriter w, int productId, String productImageName,String productCategory,String productBrandName,String productType,
String productName,String prop1,String prop2,String prop3,String prop4,String prop5,
String description,int price,float discount,float cashback,String availability) throws IOException {
Document doc = new Document();
doc.add(new StringField("produciId",Integer.toString(productId), StringField.Store.YES));
doc.add(new StringField("productImageName",productImageName,StringField.Store.YES));
doc.add(new TextField("productCategory",productCategory,TextField.Store.YES));
doc.add(new TextField("productBrandName",productBrandName,TextField.Store.YES));
doc.add(new TextField("productType",productType,TextField.Store.YES));
doc.add(new TextField("productName",productName,TextField.Store.YES));
doc.add(new TextField("prop1",prop1,TextField.Store.YES));
doc.add(new TextField("prop2",prop2,TextField.Store.YES));
doc.add(new TextField("prop3",prop3,TextField.Store.YES));
doc.add(new TextField("prop4",prop4,TextField.Store.YES));
doc.add(new TextField("prop5",prop5,TextField.Store.YES));
doc.add(new StringField("description",description,StringField.Store.YES));
doc.add(new StringField("price",Integer.toString(price),StringField.Store.YES));
doc.add(new StringField("discount",Float.toString(discount),StringField.Store.YES));
doc.add(new StringField("cashback",Float.toString(cashback),StringField.Store.YES));
doc.add(new StringField("availability",availability,StringField.Store.YES));
w.addDocument(doc);
}
对于我正在使用的搜索:
public class Testing {
public static void main(String[] args) {
try{
String LUCENE_INDEX_DIRECTORY = "C:\\lucene";
File file = new File(LUCENE_INDEX_DIRECTORY);
Directory index = FSDirectory.open(file.toPath());
StandardAnalyzer analyzer = new StandardAnalyzer();
String query = "mountain";
QueryParser parser = new QueryParser("productName",analyzer);
Query q = null;
q=parser.parse(query);
int hitsPerPage = 10;
IndexReader reader=null;
TopScoreDocCollector collector = null;
IndexSearcher searcher = null;
reader=DirectoryReader.open(index);
searcher=new IndexSearcher(reader);
collector = TopScoreDocCollector.create(10);
searcher.search(q,collector);
ScoreDoc[] hits=collector.topDocs().scoreDocs;
System.out.println(hits.length);
if(hits.length>0){
for(int i=0; i<hits.length; i++){
int docId = hits[i].doc;
Document document = searcher.doc(docId);
System.out.println("BrandName is: "+document.get("productBrandName")+"and ProductName is: "+document.get("productName")+
"productCategory is: "+document.get("productCategory")+"and prop is:"+document.get("prop1"));
}
}else{
System.out.println("Not Done");
}
}catch(Exception e){
System.out.println("Not done ... ");
}
}
}
答案 0 :(得分:0)
如果我是你,那么第一件事就是利用Luke打开搜索数据库,看看你的内容是否真的被写入搜索索引。
https://github.com/DmitryKey/luke
并仔细检查“productName”中是否有任何值被填充。
一旦你可以确定索引的完整性是好的,那么你可以检查搜索词“mountain”是否真的是一个有效的搜索词。