【问题标题】:lucene search with wildcard is significantly slow带通配符的 lucene 搜索速度非常慢
【发布时间】:2012-09-19 01:07:54
【问题描述】:

我有一个包含 1000 个文件的列表(并且每年增长两倍),其中只有文本,每个文件的大小约为 8Mb,我正在尝试查找给定(通配符)表达式的文件名。

例如,所有文件都包含此类数据

COD1004129641208240002709991455671866 4IT / HUF 4400QQQUF 3300QQQUF

我的搜索可能是:“*9991455671866”,它与上面的行匹配。

问题是(也许我的期望太高了)返回结果需要一分钟多一点。

我的文档索引如下所示:

private Document getDocument(File file) throws IOException
{
    FileReader reader = new FileReader(file);
    Document doc = new Document();
    doc.add(new Field(IndexProperties.FIELD_FILENAME, file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED)); 
    doc.add(new Field(IndexProperties.FIELD_CONTENT, reader));

    return doc;
}

分析仪

        Directory fsDir = FSDirectory.open(new File(indexFolder));
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);

        // build the writer
        IndexWriterConfig indexWriter = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        IndexWriter writer = new IndexWriter(fsDir, indexWriter);

通配符搜索为:

public List<String> findFilenameByContent(String wildCardContent, String INDEX_FOLDER, String TICKETS_FOLDER) throws Exception
{   
    long start = System.currentTimeMillis();
    Term term = new Term(IndexProperties.FIELD_CONTENT, wildCardContent); //eg *9991455671866
    Query query = new WildcardQuery(term);

    //loop through docs
    Directory fsDir = FSDirectory.open(new File(INDEX_FOLDER));
    IndexSearcher searcher = new IndexSearcher(IndexReader.open(fsDir));
    ScoreDoc[] queryResults = searcher.search(query, 10).scoreDocs;  
    List<String> strs = new ArrayList<String>();

    for (ScoreDoc scoreDoc : queryResults) 
    {  
        Document doc = searcher.doc(scoreDoc.doc);  
        strs.add(doc.get(IndexProperties.FIELD_FILENAME));
    }

    searcher.close();
    long end = System.currentTimeMillis();
    System.out.println("TOTAL SEARCH TIME: "+(end-start)/1000.0+ "secs");
    return strs;
}

【问题讨论】:

    标签: java performance optimization lucene


    【解决方案1】:

    我认为您的代码没有任何问题。 如果您只需要搜索,请尝试:

    IndexReader.open(fsDir,true);
    

    这可能会缩短您的搜索时间。

    This suggestions may help.

    【讨论】:

      猜你喜欢
      • 2016-03-12
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      相关资源
      最近更新 更多