android_studio Lucene入门

最近想在android上弄个搜索应用,但入门就比较麻烦。首先现在都建议用 android studio这个工具,其次lucene更新实在太快,新版资料很难找。

一、android studio

这个版本目前已经更新到2.0了,自己按照网上官方的介绍,下了个JDK8和2.0安装包,安装包居然有1G多点,安装好后,又下载了些东西,总之,只是这个android studio安装就不是个简单的事。

二、创建JAVA应用

1.

不能创建工程,而是创建module这个选项,然后选择java library这个选项。运行JAVA类时,直接在当前运行的类右键会弹出一个运行对话框即可。

2.由于个人对ide并不是很熟悉,关于添加lib这个弄了半天,真没弄来,直接没看见新建的工程下有libs这个文件夹,自己新建一个也不得行,最后google终于明白怎么用了。

工程上面有个下拉箭头,选择工程文件这个选项,就能看到每个工程目录下的所有文件夹,然后将相关的jar包复制到libs目录下,复制好以后,

好像只能一个一个的添加。。。

经历过上面的步骤以后,环境基本搭建完毕。


3.开始lucene6.0的测试

6.0的资料实在太少,个人对java虽然比较了解,但对lucene并不了解,找一个5分钟教材,复制过来,有些错误,把它改正了下,总算是能运行了。

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.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 java.io.IOException;
import java.text.ParseException;

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

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

        IndexWriterConfig config = new IndexWriterConfig(analyzer);

        IndexWriter w = new IndexWriter(index, config);
        w.addDocument(getDoc("Lucene in Action", "193398817"));
        w.addDocument(getDoc("Lucene for Dummies", "55320055Z"));
        w.addDocument(getDoc("Managing Gigabytes", "55063554A"));
        w.addDocument(getDoc("The Art of Computer Science", "9900333X"));
        w.addDocument(getDoc("The Art of lucene Science", "9900555"));
        w.close();

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

        // 3. search
        int hitsPerPage = 10;
        IndexReader reader = DirectoryReader.open(index);
        IndexSearcher searcher = new IndexSearcher(reader);
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);
        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 Document getDoc(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));
        return doc;
    }
}



基本步骤有五步,指定一个分析词语的分析器、创建索引、创建查询对象、搜索、显示搜索结果。

转载于:https://my.oschina.net/moluyingxing/blog/671203

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值