mr_wangb
2014-04-24 17:52
浏览 316

学习lucene建立检索和搜索过程中遇到的错误

使用lucene4.6.0版本建立的索引和搜索,在创建索引后会产生如下索引文件
[img]
[img]http://dl2.iteye.com/upload/attachment/0096/4453/79ed3575-bec1-3dbf-bb93-067fe57531b5.png[/img]
[/img]

但是在搜索时[code="java"]
ScoreDoc[] sds = tds.scoreDocs;
[/code]中的sds总为空。[b]求大神帮忙解决!!!!!!![/b]

代码如下:

[code="java"]
package nhky.bs.lucene.test.hellolucene;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class HelloLucene {

/**
 * 建立索引
 */
public void index() {

    IndexWriter writer = null;
    try {
        Directory directory = FSDirectory.open(new File("F:/lucene/index01"));
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,new StandardAnalyzer(Version.LUCENE_46));
        writer = new IndexWriter(directory, iwc);

        // 3,创建Document
        Document document = null;
        // 4,为Docuemnt添加Field
        File files = new File("F:/lucene/files");
        // 5,通过IndexWriter添加文档到索引中
        for (File file : files.listFiles()) {
            document = new Document();
             document.add(new Field("content", new FileReader(file),new FieldType()));
             document.add(new StoredField("fileName", file.getName()));
             document.add(new StoredField("path", file.getAbsolutePath()));  
             writer.addDocument(document);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

/**
 * 建立搜索
 */
public void searcher() {
    DirectoryReader reader = null;
    try {
        // 创建Directory (去哪里搜索,此目录跟建立索引的目录一致)
        Directory directory = FSDirectory.open(new File("F:/lucene/index01"));
        reader = DirectoryReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(reader);     
        QueryParser parser = new QueryParser(Version.LUCENE_46, "fileName",new StandardAnalyzer(Version.LUCENE_46));
        Query query = parser.parse("document");

        //根据searcher搜索并且返回TopDocs
        TopDocs tds = searcher.search(query, 10);

        // 根据TopDocs获取ScoreDoc对象
        // 得到查询结果数组
        ScoreDoc[] sds = tds.scoreDocs; 
        for (ScoreDoc sd : sds) {
            // 根据searcher和scoreDoc对象获取具体的Document对象
            Document d = searcher.doc(sd.doc);
            // 根据Document获取需要的值
            System.out.println("搜索结果:");
            System.out.println(d.get("fileName") + "[" + d.get("path")
                    + "]");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } finally {
        // 关闭reader
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

}

[/code]

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • liujinxiaccp 2014-04-25 15:05
    已采纳

    Query query = parser.parse("document.txt"); 用全名称来搜,或者Query query = parser.parse("document*"); 因为StoreField是不进行分词的。

    点赞 评论
  • gy090028 2014-04-24 19:17

    请问您建立索引的文档中有文件名为document的吗?
    注意document为一个整体。
    第77行

    Query query = parser.parse("document");

    中document为您搜索的关键字。

    点赞 评论

相关推荐 更多相似问题