Question

I am trying to search a query from Apache Lucene index. The search is returning a large amount of result, I have to fill the result in JTable using Swing. I am using the loop to extract the object row from Apache Lucene index.

For the above reason if there are thousands of the record then it takes time to fill the records in table , I am using the following code. Is there any other way to achieve the task without running the loop?

  try {         File indexDir= new File("path of the file")

               Directory directory = FSDirectory.open(indexDir);

                    IndexSearcher searcher = new IndexSearcher(directory, true);
                    int maxhits=1000000;
                    QueryParser parser1 = new QueryParser(Version.LUCENE_36, "field",

                      new StandardAnalyzer(Version.LUCENE_36));

              Query qu=parser1.parse("texttosearch");

                    TopDocs topDocs = searcher.search(, maxhits);
                    ScoreDoc[] hits = topDocs.scoreDocs;


              len = hits.length;


                     int docId = 0;
                    Document d;
                    Vector column_name=new Vector();   

    column_name.addElement("title"); 
    column_name.addElement(""); 
      // For All Rows Data

    Vector row=new Vector();
     String filename="";
               String titlee="";
    Vector newRow=new Vector();



    for ( i = 0; i<len; i++) {
     docId = hits[i].doc;
     d = searcher.doc(docId);
     filename= d.get(("fpath"));
     titlee=d.get("title");
     newRow= new Vector();
     newRow.addElement(titlee);
     newRow.addElement(filename);
     row.addElement(newRow);                     

    }

    DefaultTableModel model= new DefaultTableModel(row, column_name);
    table.setModel(model);    


 }

catch(Exception ex)

{

  ex.printStackTrace();
}
Was it helpful?

Solution

small mistakes sometimes creates big troubles

  • Vector row = new Vector(); should be Vector<Vector<Object>> row = new Vector<Vector<Object>>();

  • first line in loop for (i = 0; i < len; i++) { must be newRow = new Vector();

pseudo code

for (i = 0; i < len; i++) {
     // must be otherwise first Vector is added forever!!!
     newRow = new Vector();

     //loop, add elements to one dimensional Vector

     // add 1D Vector to 2D Vector used and implemented in JTables API 
     row.addElement(newRow); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top