• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Lucene HelloWorld

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Lucene教程 - Lucene HelloWorld


我们可以使用Lucene为您的应用程序添加全文搜索功能。

索引

我们将从一些字符串创建一个内存索引。

StandardAnalyzer analyzer = new StandardAnalyzer(Version.LATEST);
Directory index = new RAMDirectory();

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

IndexWriter w = new IndexWriter(index, config);
addDoc(w, "Lucene in Action", "1");
addDoc(w, "Lucene for Dummies", "2");
addDoc(w, "Java", "3");
addDoc(w, "Oracle", "4");
w.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));
  doc.add(new StringField("isbn", isbn, Field.Store.YES));
  w.addDocument(doc);
}
 

TextField对内容进行标记化,而StringField不对其内容进行标记化。


查询

以下代码显示了如何从Java字符串构建查询。

String querystr = "lucene";

Query q = new QueryParser(Version.LATEST, "title", analyzer).parse(querystr);

当进行搜索时,我们首先打开索引,这是一个内存索引。TopScoreDocCollector用于收集前10个评分点击。

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

显示

以下是显示结果的逻辑。

    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(d.get("isbn") );
      System.out.println(d.get("title") );
    }

完整代码

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 Main {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException, ParseException {
  
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LATEST);
    Directory index = new RAMDirectory();

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

    IndexWriter w = new IndexWriter(index, config);
    addDoc(w, "Lucene in Action", "1");
    addDoc(w, "Lucene for Dummies", "2");
    addDoc(w, "Java", "3");
    addDoc(w, "Oracle", "4");
    w.close();

    String querystr = "lucene";

    Query q = new QueryParser(Version.LATEST, "title", analyzer).parse(querystr);

    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;
    
    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(d.get("isbn") );
      System.out.println(d.get("title") );
    }
    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));
    doc.add(new StringField("isbn", isbn, Field.Store.YES));
    w.addDocument(doc);
  }
}

下载

下载源代码和库。

下载 Lucene_HelloWorld.zip

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Log4j 安装发布时间:2022-01-22
下一篇:
JPA 实体管理器发布时间:2022-01-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap