Would like to store data from database in lucene indexed file and retrieve table information and data

I am new to lucene. I want to store and retrieve data from a database. I would also like to store the table information, so that I may be able to tell which table the data belongs to and load the table for the user.

I am able to store data as text from database, but I am not able to figure out any approach which would let me to know the table from which it has come.

I have reached to this level, which lets me store data into an indexed file:

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

            Events event=Event.getEventById(5);


            Version matchVersion= Version.LUCENE_30;
            Analyzer analyzer=new StandardAnalyzer(matchVersion);
            boolean recreateIndexIfExists = true;
            File indexDir=new File(INDEX_DIRECTORY);
            Directory fsDir = FSDirectory.open(indexDir);
            IndexWriter indexWriter= new IndexWriter(fsDir,analyzer,MaxFieldLength.UNLIMITED);

            if(event !=null){
                Document doc=new Document();
                doc.add(new Field("field", event.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
                doc.add(new Field("field", event.getPlace(), Field.Store.YES, Field.Index.ANALYZED));
                doc.add(new Field("field", event.getDescription(), Field.Store.YES, Field.Index.ANALYZED));

                indexWriter.addDocument(doc);

            }
            indexWriter.optimize();
            indexWriter.close();
            searchIndex("first");
    }

This is to search from the index file:

File indexDir=new File(INDEX_DIRECTORY);
        Directory fsDir = FSDirectory.open(indexDir);
        if(indexDir.exists()){
            System.out.println("index file found");
        }else{
            System.out.println("not found");
        }
        IndexReader indexReader=IndexReader.open(fsDir);
        IndexSearcher searcher=new IndexSearcher(indexReader);

        Version matchVersion= Version.LUCENE_30;
        Analyzer analyzer=new StandardAnalyzer(matchVersion);

        QueryParser parser=new QueryParser(matchVersion, "field", analyzer);
        Query query;

        try {
            query = parser.parse(searchString);
            int n=10;
            TopDocs topDocs= searcher.search(query,n);

            ScoreDoc[] scoreDocs=topDocs.scoreDocs;
            System.out.println("hits= "+scoreDocs.length);
            for (int i = 0; i < scoreDocs.length; ++i) {
                ScoreDoc sd = scoreDocs[i];
                System.out.println("scoredocs: "+scoreDocs[i]);
                float score = sd.score;
                int docId = sd.doc;
                Document d = searcher.doc(docId);
                Field value = (Field) d.getField("place");
                System.out.println("placesdfgddd: "+value);

            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Upvotes: 0

Views: 337

Answers (1)

femtoRgon
femtoRgon

Reputation: 33351

Your document structure is a bit odd. I would usually expect Title, Place and Description to be stored as separate fields, rather than all in the same field named "field". That is, of course, up to you and your searching requirements, though.

To store the table name, just put it into a stored field. Since it sounds like you don't need to search on that, you can set it to Field.Index.NO:

Document doc=new Document();
//Note, changed these field names.
doc.add(new Field("title", event.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("place", event.getPlace(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("description", event.getDescription(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("table", yourTableName, Field.Store.YES, Field.Index.NO));

Which, once you have your search results, you can retrieve simply enough:

document.getField("table").stringValue();

Upvotes: 1

Related Questions