圖示 |
---|
圖示 |
---|
SolrJ核心包 /solr-4.10.3/dist/solr-solrj-4.10.3.jarjava
SolrJ依賴包 /solr-4.10.3/dist/solrj-lib下的全部包mysql
日誌依賴包 /solr-4.10.3/example/lib/ext目錄下的全部jar包sql
JDBC驅動包 mysql-connector-java-5.1.10-bin.jar數據庫
拷貝log4j.properties到src目錄下apache
項目結構 |
---|
package com.xkt.solr.pojo; /** * @author lzx * */ public class Product { private Integer pid; private String name; private String catalogName; private double price; private String description; private String picture; public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCatalogName() { return catalogName; } public void setCatalogName(String catalog_name) { this.catalogName = catalog_name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getPicture() { return picture; } public void setPicture(String picture) { this.picture = picture; } @Override public String toString() { return "Product [pid=" + pid + ", name=" + name + ", catalogName=" + catalogName + "]"; } }
package com.xkt.solr.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.solr.common.SolrInputDocument; import com.xkt.solr.pojo.Product; /** * @author lzx * */ public class ProductDao { private Connection conn; private PreparedStatement ps; private ResultSet rs; /** * 得到全部數據 * * @return */ public List<Product> getAll() { List<Product> products = new ArrayList<>(); try { // 1.加載驅動 Class.forName("com.mysql.jdbc.Driver"); // 2.創建鏈接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/solr", "root", "1234"); // 3.獲取prepareStatement預編譯 ps = conn.prepareStatement( "SELECT pid, name, catalog, catalog_name, price, number, description, picture, release_time FROM products"); // 4.執行搜索 rs = ps.executeQuery(); Product product = null; // Product product = new Product(); while (rs.next()) { product = new Product(); product.setPid(rs.getInt("pid")); product.setName(rs.getString("name")); product.setPrice(rs.getFloat("price")); product.setPicture(rs.getString("picture")); product.setDescription(rs.getString("description")); product.setCatalogName(rs.getString("catalog_name")); System.out.println(product); products.add(product); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != conn) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != ps) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != rs) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } return products; } }
package com.xkt.solr.test; import java.util.List; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.junit.Test; import com.xkt.solr.dao.ProductDao; import com.xkt.solr.pojo.Product; /** * @author lzx * */ public class ProductDaoTest { /** * 測試採集全部數據 */ @Test public void getAll() { ProductDao productDao = new ProductDao(); List<Product> products = productDao.getAll(); System.out.println(products); } }
測試結果 |
---|
<!--product--> <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="product_name" type="text_general" indexed="true" stored="true"/> <field name="product_catalog_name" type="string" indexed="true" stored="true" /> <field name="product_price" type="double" indexed="true" stored="true"/> <field name="product_description" type="text_general" indexed="true" stored="false" /> <field name="product_picture" type="string" indexed="false" stored="true" />
package com.xkt.solr.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.solr.common.SolrInputDocument; import com.xkt.solr.pojo.Product; /** * @author lzx * */ public class ProductDao { private Connection conn; private PreparedStatement ps; private ResultSet rs; /** * 得到全部數據 * * @return */ public List<Product> getAll() { List<Product> products = new ArrayList<>(); try { // 1.加載驅動 Class.forName("com.mysql.jdbc.Driver"); // 2.創建鏈接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/solr", "root", "1234"); // 3.獲取prepareStatement預編譯 ps = conn.prepareStatement( "SELECT pid, name, catalog, catalog_name, price, number, description, picture, release_time FROM products"); // 4.執行搜索 rs = ps.executeQuery(); Product product = null; // Product product = new Product(); while (rs.next()) { product = new Product(); product.setPid(rs.getInt("pid")); product.setName(rs.getString("name")); product.setPrice(rs.getFloat("price")); product.setPicture(rs.getString("picture")); product.setDescription(rs.getString("description")); product.setCatalogName(rs.getString("catalog_name")); System.out.println(product); products.add(product); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != conn) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != ps) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != rs) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } return products; } /** * 得到文檔 * * @param products * @return */ public List<SolrInputDocument> getDocuments(List<Product> products) { List<SolrInputDocument> docs = new ArrayList<>(); SolrInputDocument doc = null; for (Product product : products) { doc = new SolrInputDocument(); doc.addField("id", product.getPid()); doc.addField("product_name", product.getName()); doc.addField("product_catalog_name", product.getCatalogName()); doc.addField("product_price", product.getPrice()); doc.addField("product_description", product.getDescription()); doc.addField("product_picture", product.getPicture()); docs.add(doc); } return docs; } }
/** * 採集數據後建立索引 */ @Test public void creatIndex() { try { // 一、創建和服務器的鏈接 當不指定鏈接哪個solrCore時,默認鏈接第一個 HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/jd"); // 二、將文檔寫入索引庫,完成分詞、建立索引的過程 ProductDao dao = new ProductDao(); server.add(dao.getDocuments(dao.getAll())); // 三、提交事物 在solr中,事物提交能夠省略 server.commit(); System.out.println("建立索引成功"); } catch (Exception e) { e.printStackTrace(); } }
圖示 |
---|
/** * 搜索索引 */ @Test public void searchIndex() { // 1創建與服務器的鏈接 HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/jd"); // 2.建立查詢對象SolrQuery,用來封裝查詢條件 SolrQuery query = new SolrQuery(); // 3.設置查詢條件 query.set("q", "*:*"); try { // 4.執行搜索 QueryResponse response = server.query(query); // 5.渲染查詢結果 int status = response.getStatus(); if (0 == status) { SolrDocumentList docs = response.getResults(); long numFound = docs.getNumFound(); System.out.println("共查到" + numFound + "條數據"); System.out.println("---------華麗的分割線---------"); for (SolrDocument doc : docs) { String pid = (String) doc.get("id"); String name = (String) doc.get("product_name"); String catName = (String) doc.get("product_catalog_name"); double price = (double) doc.get("product_price"); String picture = (String) doc.get("product_picture"); String desc = (String) doc.get("product_description"); System.out.println("商品的id: " + pid); System.out.println("商品的名稱: " + name); System.out.println("商品的類別: " + catName); System.out.println("商品的價格: " + price); System.out.println("商品的圖片: " + picture); System.out.println("商品的描述: " + desc); } } } catch (SolrServerException e) { e.printStackTrace(); } }
圖示 |
---|
關於Solr分享就到這裏,有關安裝Dataimport插件和中文分詞器如有須要能夠評論留言。json
版權說明:歡迎以任何方式進行轉載,但請在轉載後註明出處!服務器