我的Lucene搜索没有返回结果 [英] My Lucene search doesn't return results

查看:91
本文介绍了我的Lucene搜索没有返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Lucene,这是我的第一堂考试。我正在尝试实现内存搜索,并从示例中借用了一些代码。但搜索不能返回任何命中。你可以帮帮我吗?谢谢。

I'm studying Lucene and this is my first test class. I'm trying to implement a in memory search and borrowed some codes from examples. But the search cannot return any hits. Can you help me with that please? Thanks.

    package my.test;
    import java.io.IOException;

    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.analysis.util.CharArraySet;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.document.StringField;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.index.IndexWriterConfig.OpenMode;
    import org.apache.lucene.index.Term;
    import org.apache.lucene.search.BooleanClause;
    import org.apache.lucene.search.BooleanQuery;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.PrefixQuery;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.SearcherManager;
    import org.apache.lucene.store.RAMDirectory;
    import org.apache.lucene.util.Version;

    public class TestInMemorySearch {
      public static void main(String[] args) {
        // Construct a RAMDirectory to hold the in-memory representation of the index.
        RAMDirectory idx = new RAMDirectory();

    try {
      // Make an writer to create the index
      IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_42, new StandardAnalyzer(Version.LUCENE_42, CharArraySet.EMPTY_SET));
      iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
      IndexWriter writer = new IndexWriter(idx, iwc);

      // Add some Document objects containing quotes
      writer.addDocument(createDocument("Theodore Roosevelt man", "It behooves every man to remember that the work of the "
          + "critic, is of altogether secondary importance, and that, " + "in the end, progress is accomplished by the man who does " + "things."));
      writer.addDocument(createDocument("Friedrich Hayek", "The case for individual freedom rests largely on the "
          + "recognition of the inevitable and universal ignorance " + "of all of us concerning a great many of the factors on "
          + "which the achievements of our ends and welfare depend."));
      writer.addDocument(createDocument("Ayn Rand", "There is nothing to take a man's freedom away from "
          + "him, save other men. To be free, a man must be free " + "of his brothers."));
      writer.addDocument(createDocument("Mohandas Gandhi", "Freedom is not worth having if it does not connote " + "freedom to err."));

      // Optimize and close the writer to finish building the index
      writer.close();
      // Build an IndexSearcher using the in-memory index
      SearcherManager mgr = new SearcherManager(idx, null);

      try {
        Document[] hits = search(mgr, "man", 100);
        for (Document doc : hits) {
          String title    = doc.get("title");
          String content  = doc.get("content");
          System.out.println("Found match:[Title]" + title + ", [Content]" + content);
        }

      } catch (IOException e) {
        e.printStackTrace();
      }

    } catch (IOException ioe) {
      // In this example we aren't really doing an I/O, so this
      // exception should never actually be thrown.
      ioe.printStackTrace();
    }
  }

  /**
   * Make a Document object with an un-indexed title field and an indexed
   * content field.
   */
  private static Document createDocument(String title, String content) {
    Document doc = new Document();
    doc.add(new StringField("title", title, Field.Store.YES));
    doc.add(new StringField("content", content, Field.Store.YES));

    return doc;
  }

  private static Document[] search(SearcherManager searchManager, String searchString, int maxResults) throws IOException {
    IndexSearcher searcher = null;
    try {
      // Build the query.
      String[] tokens = searchString.split("\\s+");
      BooleanQuery query = new BooleanQuery();
      for (String token : tokens) {
        query.add(new PrefixQuery(new Term("title", token)), BooleanClause.Occur.MUST);
        query.add(new PrefixQuery(new Term("content", token)), BooleanClause.Occur.MUST);
      }

      searcher = searchManager.acquire();
      ScoreDoc[] scoreDocs = searcher.search(query, maxResults).scoreDocs;
      Document[] documents = new Document[scoreDocs.length];
      for (int i = 0; i < scoreDocs.length; i++) {
        documents[i] = searcher.doc(scoreDocs[i].doc);
      }
      return documents;
    } finally {
      if (searcher != null) {
        searchManager.release(searcher);
      }
    }
  }    
}


推荐答案

StringField 似乎是一个明显的选择,但它不是你想在这里使用的。您需要 TextField StringField 将字段表示为单个标记,实质上是关键字或标识符。 TextField 分析并标记字段以进行全文搜索。

StringField seems an obvious choice, but it isn't what you want to use here. You want TextField. StringField represents the field as a single token, essentially a keyword or identifier. TextField analyzes and tokenizes the field for full-text searching.

修复它就像更改一样简单搜索方法:

Fixing it is as simple as changing, in your search method:

doc.add(new StringField("title", title, Field.Store.YES));
doc.add(new StringField("content", content, Field.Store.YES));

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

这篇关于我的Lucene搜索没有返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆