【问题标题】:Lucene 4.2 StringFieldLucene 4.2 字符串字段
【发布时间】:2013-06-02 17:01:12
【问题描述】:

我是 Lucene 的新手。我有两个文档,我想对名为“关键字”的文档字段进行完全匹配(该字段可能在一个文档中出现多次)。

第一个文档包含关键字“注释很酷”。第二个文档包含关键字“注释也很酷”。当我搜索“注释很酷”时,如何构建查询以便只找到第一个文档?

我读过一些关于“StringField”的东西,它没有被标记化。如果我在“addDoc”方法中将“关键字”字段从“TextField”更改为“StringField”,则将找不到任何内容。

这是我的代码:

private IndexWriter writer;

public void lucene() throws IOException, ParseException {
    // Build the index
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_42);
    Directory index = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_42,
            analyzer);
    this.writer = new IndexWriter(index, config);

    // Add documents to the index
    addDoc("Spring", new String[] { "Java", "JSP",
            "Annotation is cool" });
    addDoc("Java", new String[] { "Oracle", "Annotation is cool too" });

    writer.close();

    // Search the index
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);

    BooleanQuery qry = new BooleanQuery();

    qry.add(new TermQuery(new Term("keyword", "\"Annotation is cool\"")), BooleanClause.Occur.MUST);

    System.out.println(qry.toString());

    Query q = new QueryParser(Version.LUCENE_42, "title", analyzer).parse(qry.toString());

    int hitsPerPage = 10;
    TopScoreDocCollector collector = TopScoreDocCollector.create(
            hitsPerPage, true);

    searcher.search(q, collector);

    ScoreDoc[] hits = collector.topDocs().scoreDocs;

    for (int i = 0; i < hits.length; ++i) {
        int docId = hits[i].doc;
        Document doc = searcher.doc(docId);
        System.out.println((i + 1) + ". \t" + doc.get("title"));
    }

    reader.close();
}

private void addDoc(String title, String[] keywords) throws IOException {
    // Create new document
    Document doc = new Document();

    // Add title
    doc.add(new TextField("title", title, Field.Store.YES));

    // Add keywords
    for (int i = 0; i < keywords.length; i++) {
        doc.add(new TextField("keyword", keywords[i], Field.Store.YES));
    }

    // Add document to index
    this.writer.addDocument(doc);
}

【问题讨论】:

    标签: lucene


    【解决方案1】:

    您的问题不在于如何索引该字段。字符串字段是将整个输入索引为单个标记的正确方法。问题是你如何搜索。我真的不知道你打算用这个逻辑来完成什么,真的。

    BooleanQuery qry = new BooleanQuery();
    qry.add(new TermQuery(new Term("keyword", "\"Annotation is cool\"")), BooleanClause.Occur.MUST);
    //Great! You have a termQuery added to the parent BooleanQuery which should find your keyword just fine!
    
    Query q = new QueryParser(Version.LUCENE_42, "title", analyzer).parse(qry.toString());
    //Now all bets are off.
    

    Query.toString() 是一种方便的调试方法,但假设通过 QueryParser 运行输出文本查询将重新生成相同的查询是不安全的。标准查询解析器确实没有太多能力将多个单词表示为一个术语。我相信,您看到的 String 版本看起来像:

    keyword:"Annotation is cool"
    

    这将被解释为 PhraseQuery。一个 PhraseQuery 将查找三个连续的词条,Annotationiscool,但是按照您对此进行索引的方式,您只有一个词条“注释很酷”

    解决方案是永远不要使用像

    这样的逻辑
     Query nuttyQuery = queryParser.parse(perfectlyGoodQuery.toString());
     searcher.search(nuttyQuery);
    

    相反,只需使用您已经创建的 BooleanQuery 进行搜索。

     searcher.search(perfectlyGoodQuery);
    

    【讨论】:

    • 非常感谢您的帮助。如果我取消注释带有“Query q ...”的行并将“searcher.search(...)”中的参数“q”更改为“qry”,则找不到任何内容。我也改成StringField,没有成功。
    • 看起来你只在构造查询时用引号括起来,而不是在索引时,所以从查询术语中删除引号。
    猜你喜欢
    • 2013-04-25
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多