建立Maven工程,導入索引,html
<dependencies> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>4.10.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>4.10.4</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId> <version>4.10.4</version> </dependency> </dependencies>
建立book表,向其中插入5條數據,java
數據庫sql,以下:python
create table `book` ( `id` int (11), `name` varchar (192), `price` float , `pic` varchar (96), `description` text ); insert into `book` (`id`, `name`, `price`, `pic`, `description`) values('1','java 編程思想','71.5','23488292934.jpg','做者簡介 Bruce Eckel,是MindView公司的總裁,該公司向客戶提供軟件諮詢和培訓。他是C++標準委員會擁有表決權的成員之一,擁有應用物理學學士和計算機工程碩士學位。除本書外,他仍是《C++編程思想》的做者,並與人合著了《C++編程思想第2卷》。\r\n\r\n《計算機科學叢書:Java編程思想(第4版)》贏得了全球程序員的普遍讚譽,即便是最晦澀的概念,在BruceEckel的文字親和力和小而直接的編程示例面前也會化解於無形。從Java的基礎語法到最高級特性(深刻的面向對象概念、多線程、自動項目構建、單元測試和調試等),本書都能逐步指導你輕鬆掌握。\r\n 從《計算機科學叢書:Java編程思想(第4版)》得到的各項大獎以及來自世界各地的讀者評論中,不難看出這是一本經典之做。本書的做者擁有多年教學經驗,對C、C++以及Java語言都有獨到、深刻的看法,以通俗易懂及小而直接的示例解釋了一個個晦澀抽象的概念。本書共22章,包括操做符、控制執行流程、訪問權限控制、複用類、多態、接口、經過異常處理錯誤、字符串、泛型、數組、容器深刻研究、JavaI/O系統、枚舉類型、併發以及圖形化用戶界面等內容。這些豐富的內容,包含了Java語言基礎語法以及高級特性,適合各個層次的Java程序員閱讀,同時也是高等院校講授面向對象程序設計語言以及Java語言的絕佳教材和參考書。\r\n 《計算機科學叢書:Java編程思想(第4版)》特色:\r\n 適合初學者與專業人員的經典的面向對象敘述方式,爲更新的JavaSE5/6增長了新的示例和章節。\r\n 測驗框架顯示程序輸出。\r\n 設計模式貫穿於衆多示例中:適配器、橋接器、職責鏈、命令、裝飾器、外觀、工廠方法、享元、點名、數據傳輸對象、空對象、代理、單例、狀態、策略、模板方法以及訪問者。\r\n 爲數據傳輸引入了XML,爲用戶界面引入了SWT和Flash。\r\n 從新撰寫了有關併發的章節,有助於讀者掌握線程的相關知識。\r\n 專門爲第4版以及JavaSE5/6重寫了700多個編譯文件中的500多個程序。\r\n 支持網站包含了全部源代碼、帶註解的解決方案指南、網絡日誌以及多媒體學習資料。\r\n 覆蓋了全部基礎知識,同時論述了高級特性。\r\n 詳細地闡述了面向對象原理。\r\n 在線可得到Java講座CD,其中包含BruceEckel的所有多媒體講座。\r\n 在網站上能夠觀看現場講座、諮詢和評論。\r\n 專門爲第4版以及JavaSE5/6重寫了700多個編譯文件中的500多個程序。\r\n 支持網站包含了全部源代碼、帶註解的解決方案指南、網絡日誌以及多媒體學習資料。\r\n 覆蓋了全部基礎知識,同時論述了高級特性。\r\n 詳細地闡述了面向對象原理。\r\n\r\n\r\n'); insert into `book` (`id`, `name`, `price`, `pic`, `description`) values('2','apache lucene','66.0','77373773737.jpg','lucene是apache的開源項目,是一個全文檢索的工具包。\r\n# Apache Lucene README file\r\n\r\n## Introduction\r\n\r\nLucene is a Java full-text search engine. Lucene is not a complete\r\napplication, but rather a code library and API that can easily be used\r\nto add search capabilities to applications.\r\n\r\n * The Lucene web site is at: http://lucene.apache.org/\r\n * Please join the Lucene-User mailing list by sending a message to:\r\n java-user-subscribe@lucene.apache.org\r\n\r\n## Files in a binary distribution\r\n\r\nFiles are organized by module, for example in core/:\r\n\r\n* `core/lucene-core-XX.jar`:\r\n The compiled core Lucene library.\r\n\r\nTo review the documentation, read the main documentation page, located at:\r\n`docs/index.html`\r\n\r\nTo build Lucene or its documentation for a source distribution, see BUILD.txt'); insert into `book` (`id`, `name`, `price`, `pic`, `description`) values('3','mybatis','55.0','88272828282.jpg','MyBatis介紹\r\n\r\nMyBatis 本是apache的一個開源項目iBatis, 2010年這個項目由apache software foundation 遷移到了google code,而且更名爲MyBatis。 \r\nMyBatis是一個優秀的持久層框架,它對jdbc的操做數據庫的過程進行封裝,使開發者只須要關注 SQL 自己,而不須要花費精力去處理例如註冊驅動、建立connection、建立statement、手動設置參數、結果集檢索等jdbc繁雜的過程代碼。\r\nMybatis經過xml或註解的方式將要執行的statement配置起來,並經過java對象和statement中的sql進行映射生成最終執行的sql語句,最後由mybatis框架執行sql並將結果映射成java對象並返回。\r\n'); insert into `book` (`id`, `name`, `price`, `pic`, `description`) values('4','spring','56.0','83938383222.jpg','## Spring Framework\r\nspringmvc.txt\r\nThe Spring Framework provides a comprehensive programming and configuration model for modern\r\nJava-based enterprise applications - on any kind of deployment platform. A key element of Spring is\r\ninfrastructural support at the application level: Spring focuses on the \"plumbing\" of enterprise\r\napplications so that teams can focus on application-level business logic, without unnecessary ties\r\nto specific deployment environments.\r\n\r\nThe framework also serves as the foundation for\r\n[Spring Integration](https://github.com/SpringSource/spring-integration),\r\n[Spring Batch](https://github.com/SpringSource/spring-batch) and the rest of the Spring\r\n[family of projects](http://springsource.org/projects). Browse the repositories under the\r\n[SpringSource organization](https://github.com/SpringSource) on GitHub for a full list.\r\n\r\n[.NET](https://github.com/SpringSource/spring-net) and\r\n[Python](https://github.com/SpringSource/spring-python) variants are available as well.\r\n\r\n## Downloading artifacts\r\nInstructions on\r\n[downloading Spring artifacts](https://github.com/SpringSource/spring-framework/wiki/Downloading-Spring-artifacts)\r\nvia Maven and other build systems are available via the project wiki.\r\n\r\n## Documentation\r\nSee the current [Javadoc](http://static.springsource.org/spring-framework/docs/current/api)\r\nand [Reference docs](http://static.springsource.org/spring-framework/docs/current/reference).\r\n\r\n## Getting support\r\nCheck out the [Spring forums](http://forum.springsource.org) and the\r\n[Spring tag](http://stackoverflow.com/questions/tagged/spring) on StackOverflow.\r\n[Commercial support](http://springsource.com/support/springsupport) is available too.\r\n\r\n## Issue Tracking\r\nSpring\'s JIRA issue tracker can be found [here](http://jira.springsource.org/browse/SPR). Think\r\nyou\'ve found a bug? Please consider submitting a reproduction project via the\r\n[spring-framework-issues](https://github.com/springsource/spring-framework-issues) repository. The\r\n[readme](https://github.com/springsource/spring-framework-issues#readme) provides simple\r\nstep-by-step instructions.\r\n\r\n## Building from source\r\nInstructions on\r\n[building Spring from source](https://github.com/SpringSource/spring-framework/wiki/Building-from-source)\r\nare available via the project wiki.\r\n\r\n## Contributing\r\n[Pull requests](http://help.github.com/send-pull-requests) are welcome; you\'ll be asked to sign our\r\ncontributor license agreement ([CLA](https://support.springsource.com/spring_committer_signup)).\r\nTrivial changes like typo fixes are especially appreciated (just\r\n[fork and edit!](https://github.com/blog/844-forking-with-the-edit-button)). For larger changes,\r\nplease search through JIRA for similiar issues, creating a new one if necessary, and discuss your\r\nideas with the Spring team.\r\n\r\n## Staying in touch\r\nFollow [@springframework](http://twitter.com/springframework) and its\r\n[team members](http://twitter.com/springframework/team/members) on Twitter. In-depth articles can be\r\nfound at the SpringSource [team blog](http://blog.springsource.org), and releases are announced via\r\nour [news feed](http://www.springsource.org/news-events).\r\n\r\n## License\r\nThe Spring Framework is released under version 2.0 of the\r\n[Apache License](http://www.apache.org/licenses/LICENSE-2.0).\r\n'); insert into `book` (`id`, `name`, `price`, `pic`, `description`) values('5','solr','78.0','99999229292.jpg','solr是一個全文檢索服務\r\n# Licensed to the Apache Software Foundation (ASF) under one or more\r\n# contributor license agreements. See the NOTICE file distributed with\r\n# this work for additional information regarding copyright ownership.\r\n# The ASF licenses this file to You under the Apache License, Version 2.0\r\n# (the \"License\"); you may not use this file except in compliance with\r\n# the License. You may obtain a copy of the License at\r\n#\r\n# http://www.apache.org/licenses/LICENSE-2.0\r\n#\r\n# Unless required by applicable law or agreed to in writing, software\r\n# distributed under the License is distributed on an \"AS IS\" BASIS,\r\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n# See the License for the specific language governing permissions and\r\n# limitations under the License.\r\n\r\n\r\nWelcome to the Apache Solr project!\r\n-----------------------------------\r\n\r\nSolr is the popular, blazing fast open source enterprise search platform\r\nfrom the Apache Lucene project.\r\n\r\nFor a complete description of the Solr project, team composition, source\r\ncode repositories, and other details, please see the Solr web site at\r\nhttp://lucene.apache.org/solr\r\n\r\n\r\nGetting Started\r\n---------------\r\n\r\nSee the \"example\" directory for an example Solr setup. A tutorial\r\nusing the example setup can be found at\r\n http://lucene.apache.org/solr/tutorial.html\r\nor linked from \"docs/index.html\" in a binary distribution.\r\nAlso, there are Solr clients for many programming languages, see \r\n http://wiki.apache.org/solr/IntegratingSolr\r\n\r\n\r\nFiles included in an Apache Solr binary distribution\r\n----------------------------------------------------\r\n\r\nexample/\r\n A self-contained example Solr instance, complete with a sample\r\n configuration, documents to index, and the Jetty Servlet container.\r\n Please see example/README.txt for information about running this\r\n example.\r\n\r\ndist/solr-XX.war\r\n The Apache Solr Application. Deploy this WAR file to any servlet\r\n container to run Apache Solr.\r\n\r\ndist/solr-<component>-XX.jar\r\n The Apache Solr libraries. To compile Apache Solr Plugins,\r\n one or more of these will be required. The core library is\r\n required at a minimum. (see http://wiki.apache.org/solr/SolrPlugins\r\n for more information).\r\n\r\ndocs/index.html\r\n The Apache Solr Javadoc API documentation and Tutorial\r\n\r\n\r\nInstructions for Building Apache Solr from Source\r\n-------------------------------------------------\r\n\r\n1. Download the Java SE 7 JDK (Java Development Kit) or later from http://java.sun.com/\r\n You will need the JDK installed, and the $JAVA_HOME/bin (Windows: %JAVA_HOME%\\bin) \r\n folder included on your command path. To test this, issue a \"java -version\" command \r\n from your shell (command prompt) and verify that the Java version is 1.7 or later.\r\n\r\n2. Download the Apache Ant binary distribution (1.8.2+) from \r\n http://ant.apache.org/ You will need Ant installed and the $ANT_HOME/bin (Windows: \r\n %ANT_HOME%\\bin) folder included on your command path. To test this, issue a \r\n \"ant -version\" command from your shell (command prompt) and verify that Ant is \r\n available. \r\n\r\n You will also need to install Apache Ivy binary distribution (2.2.0) from \r\n http://ant.apache.org/ivy/ and place ivy-2.2.0.jar file in ~/.ant/lib -- if you skip \r\n this step, the Solr build system will offer to do it for you.\r\n\r\n3. Download the Apache Solr distribution, linked from the above web site. \r\n Unzip the distribution to a folder of your choice, e.g. C:\\solr or ~/solr\r\n Alternately, you can obtain a copy of the latest Apache Solr source code\r\n directly from the Subversion repository:\r\n\r\n http://lucene.apache.org/solr/versioncontrol.html\r\n\r\n4. Navigate to the \"solr\" folder and issue an \"ant\" command to see the available options\r\n for building, testing, and packaging Solr.\r\n \r\n NOTE: \r\n To see Solr in action, you may want to use the \"ant example\" command to build\r\n and package Solr into the example/webapps directory. See also example/README.txt.\r\n\r\n\r\nExport control\r\n-------------------------------------------------\r\nThis distribution includes cryptographic software. The country in\r\nwhich you currently reside may have restrictions on the import,\r\npossession, use, and/or re-export to another country, of\r\nencryption software. BEFORE using any encryption software, please\r\ncheck your country\'s laws, regulations and policies concerning the\r\nimport, possession, or use, and re-export of encryption software, to\r\nsee if this is permitted. See <http://www.wassenaar.org/> for more\r\ninformation.\r\n\r\nThe U.S. Government Department of Commerce, Bureau of Industry and\r\nSecurity (BIS), has classified this software as Export Commodity\r\nControl Number (ECCN) 5D002.C.1, which includes information security\r\nsoftware using or performing cryptographic functions with asymmetric\r\nalgorithms. The form and manner of this Apache Software Foundation\r\ndistribution makes it eligible for export under the License Exception\r\nENC Technology Software Unrestricted (TSU) exception (see the BIS\r\nExport Administration Regulations, Section 740.13) for both object\r\ncode and source code.\r\n\r\nThe following provides more details on the included cryptographic\r\nsoftware:\r\n Apache Solr uses the Apache Tika which uses the Bouncy Castle generic encryption libraries for\r\n extracting text content and metadata from encrypted PDF files.\r\n See http://www.bouncycastle.org/ for more details on Bouncy Castle.\r\n');
定義一個POJO(Book)類;類的屬性名與book表中的字段名一致.mysql
public class Book { private Integer id; private String name; private String pic; private Float price; private String description; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
查詢全部數據庫數據,查詢結果封裝到List <Book>集合中git
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class BookDaoImpl { public List<Book> getBooks() throws Exception { List<Book> books = new ArrayList<Book>(); Connection conn = null; PreparedStatement statement=null; ResultSet resultSet = null; Class aClass = Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/file","root","123"); statement = conn.prepareStatement("SELECT * FROM book"); resultSet = statement.executeQuery(); while (resultSet.next()){ Book book = new Book(); book.setId(resultSet.getInt("id")); book.setName(resultSet.getString("name")); book.setPic(resultSet.getString("pic")); book.setPrice(resultSet.getFloat("price")); book.setDescription(resultSet.getString("description")); books.add(book); } resultSet.close(); statement.close(); conn.close(); return books; } }
將查詢的結果List<Book> 按照lucene的結構進行分詞並存儲到索引庫,程序員
public class CreateIndex { @Test public void test() throws Exception { //獲取查詢結果並將其按照lucene的結果進行封裝成Document, List<Book> books = new BookDaoImpl().getBooks(); List<Document> documents = new ArrayList<Document>(); for (Book book : books ) { Document doc = new Document(); StringField stringFieldId = new StringField("id", book.getId().toString(), Field.Store.YES); TextField textFieldName = new TextField("name", book.getName(), Field.Store.YES); StoredField storedFieldPic = new StoredField("pic", book.getPic()); FloatField floatFieldPrice = new FloatField("price", book.getPrice(), Field.Store.YES); TextField textFieldDesc = new TextField("description", book.getDescription(), Field.Store.NO); doc.add(stringFieldId); doc.add(textFieldName); doc.add(storedFieldPic); doc.add(floatFieldPrice); doc.add(textFieldDesc); documents.add(doc); } //進行分詞 Analyzer analyzer = new StandardAnalyzer(); Directory directory = FSDirectory.open(new File("E:\\itHone")); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_4, analyzer); IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); /* for (Document document:documents ) { indexWriter.addDocument(document); }*/ indexWriter.addDocuments(documents); //提交,關流 indexWriter.commit(); indexWriter.close(); } }