solr的配置请查看:http://www.cnblogs.com/byteworld/p/5898651.html
创建Core:(可以复制模版到solrhome estconf文件夹中)
简化了(schema.xml配置文件)
<?xml version="1.0" encoding="UTF-8" ?> <schema name="test_solr" version="1.5"> <!--版本这个也需要加--> <field name="_version_" type="long" indexed="true" stored="true"/> <field name="_root_" type="string" indexed="true" stored="false"/> <field name="id" type="string" stored="true" indexed="true"/> <field name="name" type="string" stored="true" indexed="true" omitNorms="false"/> <field name="price" type="string" stored="true" indexed="true"/> <fieldType name="string" class="solr.StrField" sortMissingLast="true" /> <!-- 这个主键必须加不然报错 --> <uniqueKey>id</uniqueKey> <!-- boolean type: "true" or "false" --> <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/> <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/> <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0"/> <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/> <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" positionIncrementGap="0"/> </schema>
java代码:
jar包下载:链接: http://pan.baidu.com/s/1kUIRWRt 密码: xvtu
package demo; import java.io.IOException; import java.util.ArrayList; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.common.SolrInputDocument; /** * @Description:solr添加 * @author byte-zbs * @date 2016年12月19日 下午2:28:33 */ public class AddDemo { public static final String SOLR_URL = "http://localhost:8090/solr/test_solr"; public static void main(String[] args) { addDoc(); } public static void addDoc() { String[] words = {"Document是Solr索引(动词,indexing)和搜索的最基本单元","它类似于关系数据库表中的一条记录","可以包含一个或多个字段(Field","每个字段包含一个name和文本值","字段在被索引的同时可以存储在索引中","搜索时就能返回该字段的值"}; long start = System.currentTimeMillis(); ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for(int i = 0;i < 300; i++) { SolrInputDocument input = new SolrInputDocument(); input.addField("id", "id"+i,1.0f); input.addField("name",words[i % 21], 1.0f); input.addField("price",10*i); docs.add(input); } @SuppressWarnings("resource") HttpSolrClient client = new HttpSolrClient(SOLR_URL); try { client.add(docs.iterator()); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(System.currentTimeMillis() - start); } }
/** * @Description:查询 * @param * @return void 返回类型 */ public static void query() { HttpSolrClient client = new HttpSolrClient(SOLR_URL); client.setMaxRetries(1); client.setConnectionTimeout(60*1000); client.setSoTimeout(60*1000); client.setDefaultMaxConnectionsPerHost(100); client.setMaxTotalConnections(1000); client.setFollowRedirects(false); client.setAllowCompression(true); client.setRequestWriter(new BinaryRequestWriter()); SolrQuery query = new SolrQuery(); query.setQuery("id:id*"); query.setFields("name","id","price"); query.setSort("price", ORDER.asc); query.setStart(0); query.setRows(20); try { QueryResponse res = client.query(query); SolrDocumentList DocumentList = res.getResults(); for (SolrDocument document:DocumentList) { String value = document.getFieldValue("id").toString(); String price = document.getFieldValue("price").toString(); System.out.println(value + "====" +price); } } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @Description:集合插入对象 * @param * @return void 返回类型 */ public static void pojoDocAll() { Goods good1 = new Goods("pojo_commit1", "苹果", 12.0); Goods good2 = new Goods("pojo_commit2", "橘子", 12.0); Goods good3 = new Goods("pojo_commit3", "香蕉", 12.0); ArrayList<Goods> list = new ArrayList<Goods>(); list.add(good1); list.add(good2); list.add(good3); HttpSolrClient client = new HttpSolrClient(SOLR_URL); try { client.addBeans(list); client.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @Description:插入对象 * @param * @return void 返回类型 */ public static void pojoDoc() { Goods good = new Goods("pojo_commit", "苹果", 12.0); HttpSolrClient Client = new HttpSolrClient(SOLR_URL); Client.setRequestWriter(new RequestWriter()); try { Client.addBean(good); //Client.optimize(); Client.commit(); } catch (IOException e) { e.printStackTrace(); } catch (SolrServerException e) { e.printStackTrace(); } } /** * @Description:删除文档 * @param * @return void 返回类型 */ public static void delDoc() { HttpSolrClient client = new HttpSolrClient(SOLR_URL); try { client.deleteById("id_update"); client.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @Description:添加的第一种方式 * @param * @return void 返回类型 */ public static void addone() { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "id_double"); doc.addField("name", "呵呵呵呵"); doc.addField("price", 100.0); HttpSolrClient client = new HttpSolrClient(SOLR_URL); try { client.add(doc); client.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @Description:添加第二种方式 * @param * @return void 返回类型 */ public static void addoneOther() { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "id_update"); doc.addField("name", "来个测试"); doc.addField("price", 100.0); UpdateRequest request = new UpdateRequest(); request.setAction(ACTION.COMMIT,false, false); request.add(doc); HttpSolrClient client = new HttpSolrClient(SOLR_URL); try { UpdateResponse resp = request.process(client); System.out.println(resp); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
package demo.dao; import org.apache.solr.client.solrj.beans.Field; public class Goods { @Field private String id; @Field(value = "name") private String goodsname; @Field private double price; public Goods(String id, String name, double price) { super(); this.id = id; this.goodsname = name; this.price = price; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return goodsname; } public void setName(String name) { this.goodsname = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
项目完整:链接: http://pan.baidu.com/s/1pLq9Kdt 密码: f558