最近想在android上弄个搜索应用,但入门就比较麻烦。首先如今都建议用 android studio这个工具,其次lucene更新实在太快,新版资料很难找。 java
1、android studio android
这个版本目前已经更新到2.0了,本身按照网上官方的介绍,下了个JDK8和2.0安装包,安装包竟然有1G多点,安装好后,又下载了些东西,总之,只是这个android studio安装就不是个简单的事。 apache
2、建立JAVA应用 ide
1. 工具
不能建立工程,而是建立module这个选项,而后选择java library这个选项。运行JAVA类时,直接在当前运行的类右键会弹出一个运行对话框便可。 测试
2.因为我的对ide并非很熟悉,关于添加lib这个弄了半天,真没弄来,直接没看见新建的工程下有libs这个文件夹,本身新建一个也不得行,最后google终于明白怎么用了。 google
工程上面有个下拉箭头,选择工程文件这个选项,就能看到每一个工程目录下的全部文件夹,而后将相关的jar包复制到libs目录下,复制好之后, code
好像只能一个一个的添加。。。 对象
经历过上面的步骤之后,环境基本搭建完毕。 索引
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; } }基本步骤有五步,指定一个分析词语的分析器、建立索引、建立查询对象、搜索、显示搜索结果。