最近想在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; } }基本步驟有五步,指定一個分析詞語的分析器、建立索引、建立查詢對象、搜索、顯示搜索結果。