【问题标题】:Problems with luceneLucene 的问题
【发布时间】:2013-08-20 15:11:43
【问题描述】:
package avajava;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
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.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import java.io.IOException;

public class HelloLucene {
   public static void main(String[] args) throws IOException, ParseException {
      // 0. Specify the analyzer for tokenizing text.
      //    The same analyzer should be used for indexing and searching
      StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);

      // 1. create the index
      Directory index = new RAMDirectory();

      IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);

      IndexWriter w = new IndexWriter(index, config);
      addDoc(w, "Lucene in Action", "193398817");
      addDoc(w, "Lucene for office use", "55320055Z");
      addDoc(w, "Managing Gigabytes", "55063554A");
      addDoc(w, "The Art of Computer Science", "9900333X");
      w.close();

      // 2. query
      String querystr = "title:of";

      // the "title" arg specifies the default field to use
      // when no field is explicitly specified in the query.
      Query q = new QueryParser(Version.LUCENE_44, "title", analyzer).parse(querystr);

      // 3. search
      int hitsPerPage = 10;
      IndexReader reader = DirectoryReader.open(index);
      IndexSearcher searcher = new IndexSearcher(reader);
      TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
      searcher.search(q, collector);
      ScoreDoc[] hits = collector.topDocs().scoreDocs;

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

      // reader can only be closed when there
      // is no need to access the documents any more.
      reader.close();
  }

  private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
     Document doc = new Document();
     doc.add(new TextField("title", title, Field.Store.YES));

     // use a string field for isbn because we don't want it tokenized
     doc.add(new StringField("isbn", isbn, Field.Store.YES));
     w.addDocument(doc);
  }

}

我从一些例子中得到了上面的代码。但是根据查询字符串(“title:of”),我没有得到预期的结果。

预期输出:

找到 2 个匹配项。 1. 55320055Z Lucene 办公用 2. 9900333X 计算机科学的艺术

请帮忙。

【问题讨论】:

  • 现在我得到了 Found O 命中的输出。问题原因是搜索关键字“of”。我认为代码在搜索关键字“lucene”下运行良好。
  • 我有一段时间没有使用 Lucene,也没有使用当前版本,但会假设停用词已被过滤(在索引期间,或在创建查询期间,或两者兼而有之)。查看 API 文档应该可以澄清这一点。

标签: java search lucene


【解决方案1】:

您需要使用一组空的停用词来构造 StandardAnalyzer。 'of' 是一个步进词。

Lucene StandardAnalyzer

【讨论】:

    猜你喜欢
    • 2012-06-22
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    相关资源
    最近更新 更多