org.apache.solr.client.solrj.SolrServerException: Server refused connection at: http://localhost:8080/solr/solrCore666 at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:559) at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:210) at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:206) at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:124) at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:68) at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:54) at org.abcdit.solr.test.ProductDaoTest.createIndex(ProductDaoTest.java:43) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178) at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304) at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445) at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57) at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:448)yi
POJO Bunken:
. 1 Package org.abcdit.solr.pojo; 2 . 3 public class Product { . 4 . 5 Private Integer PID; // the INT (. 11) the NOT NULL the AUTO_INCREMENT the COMMENT 'Product Number', . 6 Private String name; // VARCHAR (255) NULL the DEFAULT NULL COMMENT 'product name', 7 Private String catalogName; // VARCHAR (50) NULL the DEFAULT NULL COMMENT 'Category name', 8 Private Double. price; // DOUBLE NULL the DEFAULT NULL COMMENT 'price', 9 Private String the Description; / / LONGTEXT the COMMENT NULL 'product description' 10 private String picture; //VARCHAR(255) NULL DEFAULT NULL COMMENT '图片名称', 11 12 public Integer getPid() { 13 return pid; 14 } 15 16 public void setPid(Integer pid) { 17 this.pid = pid; 18 } 19 20 public String getName() { 21 return name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 public String getCatalogName() { 29 return catalogName; 30 } 31 32 public void setCatalogName(String catalogName) { 33 this.catalogName = catalogName; 34 } 35 36 public Double getPrice() { 37 return price; 38 } 39 40 public void setPrice(Double price) { 41 this.price = price; 42 } 43 44 public String getDescription() { 45 return description; 46 } 47 48 public void setDescription(String description) { 49 this.description = description; 50 } 51 52 public String getPicture() { 53 return picture; 54 } 55 56 public void setPicture(String picture) { 57 this.picture = picture; 58 } 59 60 public Product(Integer pid, String name, String catalogName, Double price, String description, String picture) { 61 this.pid = pid; 62 this.name = name; 63 this.catalogName = catalogName; 64 this.price = price; 65 this.description = description; 66 this.picture = picture; 67 } 68 69 public Product() { 70 super(); 71 } 72 73 @Override 74 public String toString() { 75 return "Product{" + 76 "pid=" + pid + 77 ", name='" + name + '\'' + 78 ", catalogName='" + catalogName + '\'' + 79 ", price=" + price + 80 ", description='" + description + '\'' + 81 ", picture='" + picture + '\'' + 82 '}'; 83 } 84 }
DAO layer file:
1 package org.abcdit.solr.dao; 2 3 import org.abcdit.solr.pojo.Product; 4 5 import java.sql.*; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import org.apache.solr.common.SolrInputDocument; 10 11 public class ProductDao { 12 13 private Connection connection; 14 private PreparedStatement pst; 15 private ResultSet rs; 16 17 public List<Product> findAll(){ 18 19 List<Product> products = new ArrayList<>(); 20 21 try { 22 //1.加载数据驱动 23 Class.forName("com.mysql.cj.jdbc.Driver"); 24 //2.获取connection连接 25 connection = DriverManager.getConnection( 26 "jdbc:mysql://localhost:3306/solr?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true", 27 "root", 28 "myroot" 29 ); 30 //3.执行PreparedStatement,预编译 31 pst = connection.prepareStatement("SELECT pid, name, catalog_name, price, description, picture FROM products"); 32 //4.执行SQL搜索 33 rs = pst.executeQuery(); 34 35 Product prc = null; 36 while (rs.next()){ 37 prc = new Product(); 38 39 prc.setPid(rs.getInt("pid")); 40 prc.setName(rs.getString("name")); 41 prc.setPrice(rs.getDouble("price")); 42 prc.setPicture(rs.getString("picture")); 43 prc.setCatalogName(rs.getString("catalog_name")); 44 prc.setDescription(rs.getString("description")); 45 46 products.add(prc); 47 } 48 49 } catch (Exception e) { 50 e.printStackTrace(); 51 } finally { 52 if (rs != null) { 53 try { 54 rs.close(); 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 } 58 } 59 if (pst != null){ 60 try { 61 pst.close(); 62 } catch (SQLException e) { 63 e.printStackTrace(); 64 } 65 } 66 if (connection != null){ 67 try { 68 connection.close(); 69 } catch (SQLException e) { 70 e.printStackTrace(); 71 } 72 } 73 74 } 75 76 return products; 77 } 78 79 80 public List<SolrInputDocument> getDocuments(List<Product> products) { 81 82 List<SolrInputDocument> docs = new ArrayList<>(); 83 SolrInputDocument doc = null; 84 85 for (Product product : products) { 86 87 doc = new SolrInputDocument(); 88 doc.addField("id", product.getPid()); //编号 89 doc.addField("productName", product.getName()); //名称 90 doc.addField("productCatalogName", product.getCatalogName());//类别 91 doc.addField("productPrice", product.getPrice()); //价格 92 doc.addField("productPicture", product.getPicture());//图片 93 doc.addField("productDescription", product.getDescription());//描述 94 95 docs.add(doc); 96 97 } 98 99 return docs; 100 101 } 102 103 104 105 }
First, see if modifications schema.xml, modify configuration is as follows:
<!--product--> <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="productName" type="text_general" indexed="true" stored="true"/> <field name="productCatalogName" type="string" indexed="true" stored="true" /> <field name="productPrice" type="double" indexed="true" stored="true"/> <field name="productDescription" type="text_general" indexed="true" stored="false" /> <field name="productPicture" type="string" indexed="false" stored="true" />
1. We find that field names and field names database indexes library settings are not the same, why?
In the actual development, we will maintain the same field index database fields and data. This example is not configured the same reason, to let us know, index fields is not the same database field
Specific connection configuration mysql8:
View my previous article link: 1.mybatis connection MySQL8 database 2. the mybatis connection problems arise MySQL8.0
Create a test class:
. 1 @Test 2 public void createIndex () { . 3 // . 1, HttpSolrServer create objects, and through which the connection is established Solr server. 4 // Parameters: solr server access address . 5 HttpSolrServer Server = new new HttpSolrServer ( "HTTP: // localhost: 8080 / solr / solrCore0719" ); . 6 // 2, by HttpSolrServer object SolrInputDocument added to the index database. . 7 the ProductDao DAO = new new the ProductDao (); . 8 the try { . 9 server.add (dao.getDocuments (dao.getAllProducts ())); 10 // . 3, submitted. 11 server.commit (); 12 is System.out.println ( "successfully created index database !!!" ); 13 is 14 } the catch (SolrServerException E) { 15 e.printStackTrace (); 16 } the catch (IOException E) { . 17 E .printStackTrace (); 18 is } . 19 }