1
私はLucene 6.4.0を使用しています。次のコードを使用してください。Luceneを検索すると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);
}
と検索I'amのための使用:
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 ... ");
}
}
}
すべてが問題なく表示されます。私が気づいたことは、あなたの例がインデックスビルディングの 'file'を定義していないことだけです。愚かな質問、あなたは間違いなくあなたのインデックスを 'c:\\ lucene'に書いていますか?インデックスファイルが存在するかどうか確認しましたか? – TedTrippin
愚かな質問ではありません。その場所でインデックスファイルが作成されました@TedTrippin –
luceneコードを修正して、うまくいきました(手動でproductName = mountainを追加しました。すべてが正常に動作していることを確認してください。 – TedTrippin