【问题标题】:lucene indexing of html fileshtml文件的lucene索引
【发布时间】:2012-09-16 13:36:55
【问题描述】:

尊敬的用户,我正在使用 apache lucene 进行索引和搜索。 我必须索引存储在计算机本地磁盘上的 html 文件。我必须对 html 文件的文件名和内容进行索引。我能够将文件名存储在 lucene 索引中,但不能存储 html 文件内容,它不仅应索引数据,还应索引包含图像链接和 url 的整个页面,以及如何访问这些索引文件中的内容 对于索引,我使用以下代码:

    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);
    }
}

提前致谢

【问题讨论】:

    标签: java search indexing lucene


    【解决方案1】:

    这行代码是你的内容没有被存储的原因:

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

    此方法不存储被索引的内容。

    如果您尝试索引 HTML 文件,请尝试使用 JTidy。这将使过程变得更加容易。

    示例代码:

    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();
        }
    }
    

    从 URL 获取 InputStream:

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

    从文件中获取 InputStream:

    InputStream stream = new FileInputStream(new File (htmlFile));
    

    【讨论】:

    • 先生,整洁的 Text 类在哪里无法使用它,我如何为文件位置提供输入流对象,谢谢和问候
    • 文本类是 org.w3c.dom.Text。它带有 Java。
    • 编辑了答案以显示如何从文件位置获取输入流
    • 是的,它成功了,非常感谢先生,非常感谢现在我如何访问索引文件中的内容并在挥杆控制中显示非常感谢先生
    • 我想知道为什么被接受的答案经常没有得到至少一次的投票:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 2013-06-24
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    相关资源
    最近更新 更多