Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8573589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:17:12+00:00 2026-06-11T19:17:12+00:00

Dear Users I am working on apache lucene for indexing and searching . I

  • 0

Dear Users I am working on apache lucene for indexing and searching .
I have to index html files stored on the local disc of computer . I have to make indexing on filename and contents of the html files . I am able to store the file names in the lucene index but not the html file contents which should index not only the data but the entire page consisting images link and url and how can i access the contents from those indexed files
for indexing i am using the following code:

    File indexDir = new File(indexpath);
    File dataDir = new File(datapath);
    String suffix = ".htm";
    IndexWriter indexWriter = new IndexWriter(
            FSDirectory.open(indexDir),
            new SimpleAnalyzer(),
            true,
            IndexWriter.MaxFieldLength.LIMITED);
    indexWriter.setUseCompoundFile(false);
    indexDirectory(indexWriter, dataDir, suffix);

    numIndexed = indexWriter.maxDoc();
    indexWriter.optimize();
    indexWriter.close();


private void indexDirectory(IndexWriter indexWriter, File dataDir, String suffix) throws IOException {
    try {
        for (File f : dataDir.listFiles()) {
            if (f.isDirectory()) {
                indexDirectory(indexWriter, f, suffix);
            } else {
                indexFileWithIndexWriter(indexWriter, f, suffix);
            }
        }
    } catch (Exception ex) {
        System.out.println("exception 2 is" + ex);
    }
}

private void indexFileWithIndexWriter(IndexWriter indexWriter, File f,
    String suffix) throws IOException {
    try {
        if (f.isHidden() || f.isDirectory() || !f.canRead() || !f.exists()) {
            return;
        }
        if (suffix != null && !f.getName().endsWith(suffix)) {
            return;
        }
        Document doc = new Document();
        doc.add(new Field("contents", new FileReader(f)));
        doc.add(new Field("filename", f.getFileName(),
                Field.Store.YES, Field.Index.ANALYZED));
        indexWriter.addDocument(doc);
    } catch (Exception ex) {
        System.out.println("exception 4 is" + ex);
    }
}

thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:17:13+00:00Added an answer on June 11, 2026 at 7:17 pm

    This line of code is the reason why your contents is not being stored:

    doc.add(new Field("contents", new FileReader(f)));
    

    This method DOES NOT STORE the contents being indexed.

    If you are trying to index HTML files, try using JTidy. It will make the process much easier.

    Sample Codes:

    public class JTidyHTMLHandler {
    
        public org.apache.lucene.document.Document getDocument(InputStream is) throws DocumentHandlerException {
            Tidy tidy = new Tidy();
            tidy.setQuiet(true);
            tidy.setShowWarnings(false);
            org.w3c.dom.Document root = tidy.parseDOM(is, null);
            Element rawDoc = root.getDocumentElement();
    
            org.apache.lucene.document.Document doc =
                    new org.apache.lucene.document.Document();
    
            String body = getBody(rawDoc);
    
            if ((body != null) && (!body.equals(""))) {
                doc.add(new Field("contents", body, Field.Store.NO, Field.Index.ANALYZED));
            }
    
            return doc;
        }
    
        protected String getTitle(Element rawDoc) {
            if (rawDoc == null) {
                return null;
            }
    
            String title = "";
    
            NodeList children = rawDoc.getElementsByTagName("title");
            if (children.getLength() > 0) {
                Element titleElement = ((Element) children.item(0));
                Text text = (Text) titleElement.getFirstChild();
                if (text != null) {
                    title = text.getData();
                }
            }
            return title;
        }
    
        protected String getBody(Element rawDoc) {
            if (rawDoc == null) {
                return null;
            }
    
            String body = "";
            NodeList children = rawDoc.getElementsByTagName("body");
            if (children.getLength() > 0) {
                body = getText(children.item(0));
            }
            return body;
        }
    
        protected String getText(Node node) {
            NodeList children = node.getChildNodes();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < children.getLength(); i++) {
                Node child = children.item(i);
                switch (child.getNodeType()) {
                    case Node.ELEMENT_NODE:
                        sb.append(getText(child));
                        sb.append(" ");
                        break;
                    case Node.TEXT_NODE:
                        sb.append(((Text) child).getData());
                        break;
                }
            }
            return sb.toString();
        }
    }
    

    To get an InputStream from a URL:

    URL url = new URL(htmlURLlocation);
    URLConnection connection = url.openConnection();
    InputStream stream = connection.getInputStream();
    

    To get an InputStream from a File:

    InputStream stream = new FileInputStream(new File (htmlFile));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hallo Dear Stackoverflow users. Working with sql on sql server I have searched the
Dear experts, I am currently working on an code that allows users to drag
Good day dear users, I have to edit a Report in Infor PM Application
Dear Unix users I have for example 6 folders and in each folder I
Dear stackoverflow users, I'm currently working on a schoolproject which involves generating a PHP
Dear all, I have a question about Facebook Page: ( NOT user profile page,
Dear all,Now i have this question in my java program,I think it should be
Dear All, please help me since I'm newbie in SQL Server. I have a
Dear all, I have a select query that currently produces the following results: DoctorName
I have requirement where I need to allow users to upload a Word document

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.