2017-08-25 22 views
0

我想索引文本,单词文件并搜索这些文件中的某些内容。当我搜索一个特定的字符串时可以,但是当我尝试使用正则表达式进行搜索时,它将不再起作用。在下面,我将列出一些关键的解释代码。不能使用正则表达式在lucene中搜索

的指数函数:

// FileBean is the class contains the file path, 
    // file content, file lastModified information 
    public void indexDoc(IndexWriter writer, FileBean t) throws Exception { 
    Document doc = new Document(); 
    System.out.println(t.getPath()); 
    doc.add(new StringField(LuceneConstants.PATH, t.getPath(), Field.Store.YES)); 
    doc.add(new LongPoint(LuceneConstants.MODIFIED, t.getModified())); 
    doc.add(new TextField(LuceneConstants.CONTENT, t.getContent(), Field.Store.NO)); 
    if (writer.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE){ 
     writer.addDocument(doc); 
    } else{ 
     writer.updateDocument(new Term(LuceneConstants.PATH, t.getPath()), doc); 
    } 
} 

我使用queryParse建立查询,查询将是一个RegexQuery就像 '\ d {16}' 了许多。

搜索功能

public static TopDocs getResults(IndexSearcher searcher, Query query) throws IOException { 
    TopDocs docs = searcher.search(query, 10); 
    return docs; 
} 

TopDocs的totalHit是0,这是不是我所期望。在我看来,没有文件被搜索。此内容应满足提供的给定正则表达式。

我试过Google搜索它,但仍然没有找到有效的解决方案。任何人都可以提供任何建议为什么totalHit返回0?谢谢。

回答

0

OMG,我终于找到原因了。虽然我不知道什么是深层原因。我发现如果我使用'[0-9]'而不是'\ d'。这将是好的! 如果有人能解释这一点,那将是美好的!

0

试着拿走'+',所以它会是'\ d {16}'。

+0

对不起,我贴错了。代码只是/ \ d {16} /。常规是好的。 – neal