java - 为什么 Lucene 找不到任何包含此代码的文档?

标签 java search full-text-search lucene

我正在编写这段代码,它将单个文档添加到 lucene (4.7) 索引,然后尝试通过查询文档中确实存在的术语来找到它。但indexSearcher 不返回任何文档。我的代码有什么问题吗?感谢您的评论和反馈。

String indexDir = "/home/richard/luc_index_03";
    try {
        Directory directory = new SimpleFSDirectory(new File(
                indexDir));
        Analyzer analyzer = new SimpleAnalyzer(
                Version.LUCENE_47);
        IndexWriterConfig conf = new IndexWriterConfig(
                Version.LUCENE_47, analyzer);
        conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
        conf.setRAMBufferSizeMB(256.0);
        IndexWriter indexWriter = new IndexWriter(
                directory, conf);

        Document doc = new Document();
        String title="New York is an awesome city to live!";
        doc.add(new StringField("title", title, StringField.Store.YES));
        indexWriter.addDocument(doc);
        indexWriter.commit();
        indexWriter.close();
        directory.close();
        IndexReader reader = DirectoryReader
                .open(FSDirectory.open(new File(
                        indexDir)));
        IndexSearcher indexSearcher = new IndexSearcher(
                reader);


        String field="title";
        SimpleQueryParser qParser = new SimpleQueryParser(analyzer, field);
        String queryText="New York" ; 
        Query query = qParser.parse(queryText);
        int hitsPerPage = 100;
        TopDocs results = indexSearcher.search(query, 5 * hitsPerPage);
        System.out.println("number of results: "+results.totalHits);
        ScoreDoc[] hits = results.scoreDocs;
        int numTotalHits = results.totalHits;

        for (ScoreDoc scoreDoc:hits){
            Document docC = indexSearcher.doc(scoreDoc.doc);
            String path = docC.get("path");
            String titleC = docC.get("title");
            String ne = docC.get("ne");
            System.out.println(path+"\n"+titleC+"\n"+ne);
            System.out.println("---*****----");

        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

运行后我就得到了

number of results: 0

最佳答案

这是因为您使用了StringField。来自javadoc:

A field that is indexed but not tokenized: the entire String value is indexed as a single token.

只需使用 TextField 即可,应该没问题。

关于java - 为什么 Lucene 找不到任何包含此代码的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22693197/

相关文章:

php - 从 Sphinx(用 PHP)检索信息的最佳方法是什么?

java - 2D 元胞自动机故障

javascript - List.js 搜索列和返回值大于

Java Swing---没有错误或异常,但程序似乎无法运行

php - 日期范围搜索

用于搜索 displayName 包含特定术语的组的 Azure 图形 api

php - 在 codeigniter 多词搜索中遇到问题

mySQL MATCH 跨多个表

java - 即使没有屏幕旋转,也要让 ViewModel 保持 Activity 状态

java - 将现有 Swing 项目导入 NetBeans