【问题标题】:PhraseQuery+Lucene 4.6 is not working for PDF Word searchPhraseQuery+Lucene 4.6 不适用于 PDF Word 搜索
【发布时间】:2014-01-23 10:40:53
【问题描述】:

Iam 使用带有 Phrase Query 的 lucene 4.6 版本从 PDF 中搜索单词。下面是我的代码。在这里,我能够从 PDF 中获取输出文本,并将查询作为内容获取:“以下是”。但是点击数显示为0。有什么建议吗?提前致谢。

            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);

            // Store the index in memory:               
            Directory directory = new RAMDirectory();
            // To store an index on disk, use this instead:
            //Directory directory = FSDirectory.open("/tmp/testindex");
            IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);
            IndexWriter iwriter = new IndexWriter(directory, config);
            iwriter.deleteAll();
            iwriter.commit();
            Document doc = new Document();
            PDDocument document = null;
                try {
                    document = PDDocument.load(strFilepath);
                } 
                catch (IOException ex) {
                    System.out.println("Exception Occured while Loading the document: " + ex);
                }
              String output=new PDFTextStripper().getText(document);
              System.out.println(output);
            //String text = "This is the text to be indexed";
            doc.add(new Field("contents", output, TextField.TYPE_STORED));
            iwriter.addDocument(doc);
            iwriter.close();

            // Now search the index
            DirectoryReader ireader = DirectoryReader.open(directory);
            IndexSearcher isearcher = new IndexSearcher(ireader);
            String sentence = "Following are the";
            //IndexSearcher searcher = new IndexSearcher(directory);
            if(output.contains(sentence)){
                System.out.println("");
            }

           PhraseQuery query = new PhraseQuery();
            String[] words = sentence.split(" ");
            for (String word : words) {
               query.add(new Term("contents", word));
            }

            ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
            // Iterate through the results:
            if(hits.length>0){
                System.out.println("Searched text existed in the PDF.");
            }
            ireader.close();
            directory.close();
         }
         catch(Exception e){
             System.out.println("Exception: "+e.getMessage());
         }

【问题讨论】:

  • 您确定您粘贴了正确的代码吗?你的sentence"2.3",你.split(" ") 然后用作短语查询参数。这没什么意义。
  • 抱歉给您造成了混淆......这里的句子是“Following are the”......

标签: pdf lucene


【解决方案1】:

PhraseQuery 无法正常工作的原因有两个

  1. StandardAnalyzer 使用 ENGLISH_STOP_WORDS_SET,其中包含 a, an, and, are, as, at, be, but, by, for, if, in, into, is, it, no, not, of, on, or, such, that, the, their, then, there, these, they, this, to, was, will, with 这些词将在索引时从TokenStream 中删除。这意味着当您在索引中搜索“Following are the”时,将找不到 arethe。所以你永远不会得到像 arethe 这样的PhraseQuery 的任何结果,而 the 将永远不会在那里进行搜索。 解决方案是使用这个构造函数 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46, CharArraySet.EMPTY_SET); 编制索引时,这将确保StopFilter 在编制索引时不会从TokenStream 中删除任何单词。

  2. StandardAnalyzer 也使用LowerCaseFilter,这意味着所有标记都将被标准化为小写。所以 Following 将被索引为 following,这意味着搜索“Following”不会给你结果。因为这个.toLowerCase() 会来救你,只需在你的sentence上使用它,你就会得到搜索结果。

还可以查看 link,它指定了 Unicode 标准附件 #29,其后是 StandardTokenizer。粗略看一下,在索引时,似乎撇号、引号、句号、小逗号和许多其他字符在某些条件下会被忽略。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    相关资源
    最近更新 更多