Documents indexes

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Documents indexes

massimo
Hi, sorry I want to know a java method or a procedure to load some indexes of documents already created in a directory on the desktop. I saw a method named FSDirectoty but I don't know how it works! I proceeded in this way, but the compiler returns an NaN (Not a Number) Exception... what I did I do wrong? This is the sorce code I wrote, please look at it. Thank you very much and sorry for my english, I'm italian!


package it.unibas.ricerca.controllo;


import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

                        //RICERCA

public class AzioneCerca2 extends AbstractAction{

    private Controllo controllo;
    private Similarity similarity;
    private File file_for_index;
   
    public AzioneCerca2(Controllo controllo){
        this.controllo=controllo;
        this.putValue(NAME, "search file");
        this.putValue(SHORT_DESCRIPTION, "RICERCA DI DOCUMENTI");
        this.putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
        this.putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl A")); // o alt A
    }


    private static final int MAX_HITS = 10;
    private static final String FIELD = "frase";


    public void actionPerformed(ActionEvent e) {
   
    // Effettua le inizializzazioni...
        Analyzer analyzer = new MyAnalyzer();
        Directory directory = null;
        try {
           directory = FSDirectory.open(new File("../../indici/index_Scenari_usecases"));
        } catch (IOException ex) {
            Logger.getLogger(AzioneCerca2.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            effettuaRicerca("Inserimento anagrafica laboratorio", MAX_HITS, analyzer, directory);
        } catch (ParseException ex) {
            Logger.getLogger(AzioneCerca2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CorruptIndexException ex) {
            Logger.getLogger(AzioneCerca2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AzioneCerca2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   

    private static void effettuaRicerca(String queryString, int maxHits, Analyzer analyzer, Directory directory) throws ParseException, CorruptIndexException, IOException, org.apache.lucene.queryParser.ParseException {
        // Crea la QUERY...
        QueryParser parser = new QueryParser(Version.LUCENE_34, FIELD, analyzer);
        Query query = parser.parse(queryString);

        // ...la esegue...
        IndexSearcher indexSearcher = new IndexSearcher(directory);
        TopDocs results = indexSearcher.search(query, maxHits);
        System.out.println("\nSearching '" + queryString + "'.");
        System.out.println("Found #" + results.totalHits + " items (" + results.getMaxScore() + ")");

        // ...e, infine, stampa i risultati.
        ScoreDoc[] hits = results.scoreDocs;
        for (int i = 0; i < hits.length; i++) {
            Document hitDocIS = indexSearcher.doc(hits[i].doc);
            String foundSentence = hitDocIS.get(FIELD);

            System.out.println(hits[i].doc + "# (" + hits[i].score + ") - " + foundSentence);
        }
        indexSearcher.close();
    }

}