gpt4 book ai didi

java - 在 Lucene 中搜索提供 0 个结果

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:32 24 4
gpt4 key购买 nike

我正在使用 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 ... ");
}




}

}

最佳答案

如果我是你,第一件事可能就是用路加打开搜索数据库,看看你的内容是否真的被写入了搜索索引。

https://github.com/DmitryKey/luke

还要仔细检查正在填充的“productName”中是否有任何值。

一旦您确定索引的完整性良好,您就可以检查搜索词“山”是否确实是有效的搜索词。

关于java - 在 Lucene 中搜索提供 0 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42072847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com