solr的增删改查
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
solr的增删改查的更多相关文章
- 【Solr】solr的增删改查
目录 创建工程 增 删 改 查 高量查询 回到顶部 创建工程 普通的java web工程即可,我采用的是spring mvc! 回到顶部 增 @Autowired private SolrServer ...
- 【ES】ElasticSearch初体验之使用Java进行最基本的增删改查~
好久没写博文了, 最近项目中使用到了ElaticSearch相关的一些内容, 刚好自己也来做个总结. 现在自己也只能算得上入门, 总结下自己在工作中使用Java操作ES的一些小经验吧. 本文总共分为三 ...
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- ASP.NET从零开始学习EF的增删改查
ASP.NET从零开始学习EF的增删改查 最近辞职了,但是离真正的离职还有一段时间,趁着这段空档期,总想着写些东西,想来想去,也不是很明确到底想写个啥,但是闲着也是够 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查
系列目录 文章于2016-12-17日重写 在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的代码已经给出,里面包含了增删改,大家可以下载下来看下. 这讲主要是,制作漂亮的工具栏,虽 ...
- 通过Java代码实现对数据库的数据进行操作:增删改查
在写代码之前,依然是引用mysql数据库的jar包文件:右键项目-构建路径-设置构建路径-库-添加外部JAR 在数据库中我们已经建立好一个表xs :分别有xuehao xingming xue ...
- Hibernate全套增删改查+分页
1.创建一个web工程 2.导入jar包 3.创建Student表 4.创建实体类 package com.entity; public class Student { private Integer ...
- 使用 Json.Net 对Json文本进行 增删改查
JSON 已经成为当前主流交互格式, 如何在C#中使用 Json.Net 对Json文本进行 增删改查呢?见如下代码 #region Create (从零创建) public static strin ...
- yii2 增删改查
自己总结的yii2 advanced 版本的简单的增删改查,希望对大家有所帮助 1.gii生成的actionCreate()方法中 获取插入语句的id $id = $model->attribu ...
随机推荐
- oracle中scn(系统改变号)
系统scn: select checkpoint_change# from v$database; 文件scn: select name ...
- C# 如何获取当前应用程序的上一级路径
Server.MapPath 的使用方法 用法: 1.Server.MapPath ("/") 应用程序根目录所在的位置 如 C:\Inetpub\wwwroot\ 2.Serve ...
- robots.txt文件没错,为何总提示封禁
大家好,我的robots.txt文件没错,为何百度总提示封禁,哪位高人帮我看看原因,在此谢过. 我的站点www.haokda.com,robots.txt如下: ## robots.txt for P ...
- mysql 慢查询日志切割
- 工作中总结的常用PHP代码
[目录] ◆PHP常用的代码 ◆HTML常用代码 [值传递 和 引用传递] [单例模式] [魔术常量] [代码调试(自定义一个简单的debug函数)] [thinkphp多表查询] [获取客户端IP地 ...
- List 用法和实例(转载)
写在粘贴复制前:英文的感觉也可以,也能看的懂,多看看英文资料没坏处的 Problem. You have questions about the List collection in the .NET ...
- easyui关于validatebox实现多重规则验证的实践
参考资料 http://blog.csdn.net/jumtre/article/details/38510975 http://blog.csdn.net/lybwwp/article/detail ...
- 升级python到2.7版本pip不可用
升级python到2.7版本pip不可用 [root@localhost pip-7.1.2]# pip Traceback (most recent call last): File "/ ...
- I2S/PCM/IOM-2、I2C/SPI/UART/GPIO/slimbus
概述 I2S,PCM,IOM-2都是数字音频接口,传数据的. I2C,SPI,UART,GPIO是控制接口,传控制信令的. I2S I2S(Inter-IC Sound Bus)是飞利浦公司为数字音频 ...
- Spring MVC上传文件
Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...