博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lucene简单入门
阅读量:6934 次
发布时间:2019-06-27

本文共 4285 字,大约阅读时间需要 14 分钟。

说lucene是Java界的检索之王,当之无愧。近年来elasticsearch的火爆登场,包括之前的solr及solr cloud,其底层都是lucene。简单了解lucene,对使用elasticsearch还是有点帮助的。本文就简单过一下其简单的api使用。

添加依赖

org.apache.lucene
lucene-core
4.6.1
org.apache.lucene
lucene-analyzers-common
4.6.1
org.apache.lucene
lucene-queryparser
4.6.1
org.apache.lucene
lucene-codecs
4.6.1

索引与检索

创建索引

File indexDir = new File(this.getClass().getClassLoader().getResource("").getFile());    @Test    public void createIndex() throws IOException {//        Directory index = new RAMDirectory();        Directory index = FSDirectory.open(indexDir);        // 0. Specify the analyzer for tokenizing text.        //    The same analyzer should be used for indexing and searching        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);        // 1. create the index        IndexWriter w = new IndexWriter(index, config);        addDoc(w, "Lucene in Action", "193398817");        addDoc(w, "Lucene for Dummies", "55320055Z");        addDoc(w, "Managing Gigabytes", "55063554A");        addDoc(w, "The Art of Computer Science", "9900333X");        w.close();    }    private 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);    }

检索

@Test    public void search() throws IOException {        // 2. query        String querystr = "lucene";        // the "title" arg specifies the default field to use        // when no field is explicitly specified in the query.        Query q = null;        try {            StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);            q = new QueryParser(Version.LUCENE_46,"title", analyzer).parse(querystr);        } catch (Exception e) {            e.printStackTrace();        }        // 3. search        int hitsPerPage = 10;        Directory index = FSDirectory.open(indexDir);        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();    }

分词

对于搜索来说,分词出现在两个地方,一个是对用户输入的关键词进行分词,另一个是在索引文档时对文档内容的分词。两个分词最好一样,这样才可以更好地匹配出来。

@Test    public void cutWords() throws IOException {//        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);//        CJKAnalyzer analyzer = new CJKAnalyzer(Version.LUCENE_46);        SimpleAnalyzer analyzer = new SimpleAnalyzer();        String text = "Spark是当前最流行的开源大数据内存计算框架,采用Scala语言实现,由UC伯克利大学AMPLab实验室开发并于2010年开源。";        TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);        try {            tokenStream.reset();            while (tokenStream.incrementToken()) {                System.out.println(charTermAttribute.toString());            }            tokenStream.end();        } finally {            tokenStream.close();            analyzer.close();        }    }

输出

spark是当前最流行的开源大数据内存计算框架采用scala语言实现由uc伯克利大学amplab实验室开发并于2010年开源

本工程

参考

转载地址:http://wxwnl.baihongyu.com/

你可能感兴趣的文章
C++ 知识点汇总
查看>>
javascript中元素的scrollLeft和scrollTop属性说明
查看>>
jquery获取对象
查看>>
2018年全国多校算法寒假训练营练习比赛(第二场)B - TaoTao要吃鸡
查看>>
别在迷恋正则表达式解析html了,好吗?
查看>>
【ZJOI 2008】树的统计 Count
查看>>
Window7系统 中常见的进程命令分析?
查看>>
二、观察者模式
查看>>
multimap删除
查看>>
笔记2011.7.12
查看>>
好插件让你事半功倍!【资源篇】
查看>>
(转)SplitContainer 控件(Windows 窗体)
查看>>
【转】PHP foreach 小结
查看>>
【转】如何用Redis做LRU-Cache
查看>>
项目—视频直播系统
查看>>
java集合框架容器 java框架层级 继承图结构 集合框架的抽象类 集合框架主要实现类...
查看>>
HttpClient使用详解
查看>>
java中对JVM的深度解析、调优工具、垃圾回收
查看>>
浅谈Java语言中ArrayList和HashSet的区别
查看>>
React文档(十四)深入JSX
查看>>