solr的配置请查看:http://www.cnblogs.com/byteworld/p/5898651.html

创建Core:(可以复制模版到solrhome\test\conf文件夹中)

简化了(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

 

最新文章

  1. Android中的 init.rc文件简介
  2. windows系统下简单nodej.s环境配置 安装
  3. Linux命令(1)- grep
  4. c# 多线程 调用带参数函数
  5. 单选按钮控件(Ridio Button)的使用
  6. FlashBuilder启动时一闪而过
  7. bzoj1485:[HNOI2009]有趣的数列
  8. nginx正向代理
  9. Node.js之eventproxy详解
  10. foreach循环中为什么不要进行remove/add操作
  11. 滚动条实现RGB颜色的调制(窗体程序)--JAVA基础
  12. IT技术有感
  13. 第一天学JAVA,下载JDK,配置JAVA环境变量!!!
  14. SQLServer之视图简介
  15. 博客作业06--结构体&amp;指针
  16. 3、Linux连接oracle
  17. Windows Server 2016-Active Directory域服务端口汇总
  18. 设置DNS 代理
  19. 技术blog链接
  20. IOS-网络(JSON解析数据与XML解析数据)

热门文章

  1. 关于在left join的on子句中限制左边表的取值时出现非期望的结果
  2. html5,加密元素
  3. mvc中测试网络
  4. win2008使用FireDac连接ORACLE数据库问题
  5. Centos搭建Python+Nginx+Tornado+Mysql环境[转载]
  6. jQuery获取隐藏文本域
  7. presto的动态化应用(一):presto节点的横向扩展与伸缩
  8. Node.js入门教程:Node.js如何安装配置并部署第一个网站
  9. 扩展GridView实现的一个自定义无刷新分页,排序,支持多种数据源的控件TwfGridView
  10. django 富文本展示 以及 post提交出错