问题:
解决:
- 搭建solr服务器
12docker run --name my_solr -d -p 8983:8983 -t solrdocker exec -it --user=solr my_solr bin/solr create_core -c products
- 设置scheme(也可以通过admin ui scheme 中设置)
123curl -X POST -H 'Content-type:application/json' --data-binary '{"add-field": {"name":"productId", "type":"pint", "multiValued":false, "stored":true}}' http://localhost:8983/solr/products/schemacurl -X POST -H 'Content-type:application/json' --data-binary '{"add-field": {"name":"title", "type":"string", "multiValued":false, "stored":true}}' http://localhost:8983/solr/products/schemacurl -X POST -H 'Content-type:application/json' --data-binary '{"add-field": {"name":"createDate", "type":"pdate", "multiValued":false, "stored":true}}' http://localhost:8983/solr/products/schema
- solr client
- pom.xml
12345<dependency><groupId>org.apache.solr</groupId><artifactId>solr-solrj</artifactId><version>7.2.0</version></dependency>
- App.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192package demo.solr1;import java.util.Date;import java.util.List;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.beans.Field;import org.apache.solr.client.solrj.impl.HttpSolrClient;import org.apache.solr.client.solrj.response.QueryResponse;/**** */public class App {public static class Product {@Fieldprivate Integer id;@Fieldprivate String title;@Fieldprivate Date createDate;public Product() {}public Product(Integer id, String title, Date createDate) {this.id = id;this.title = title;this.createDate = createDate;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public Date getCreateDate() {return createDate;}public void setCreateDate(Date createDate) {this.createDate = createDate;}@Overridepublic String toString() {return "Product{" + "id=" + id + ", title=" + title + ", createDate=" + createDate + '}';}}public static void main(String[] argv) throws Exception {final String collName = "products";//final String collName = "gettingstarted";final String url = "http://localhost:8983/solr";final SolrClient client = new HttpSolrClient.Builder(url).build();{//addfor (int i = 0; i < 100; ++i) {final Product product = new Product(i, "title_" + i, new Date());client.addBean(collName, product);}client.commit(collName);}//client.deleteById(collName, "10");//client.commit(collName);{//queryfinal SolrQuery q = new SolrQuery("id:10");//q.addField("id");QueryResponse r = client.query(collName,q);final List<Product> products = r.getBeans(Product.class);for (Product product : products) {System.out.println(product);}}}}
- dd
- pom.xml
- ddd
参考:
https://store.docker.com/images/solr
http://lucene.apache.org/solr/guide/7_2/using-solrj.html