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的增删改查的更多相关文章

  1. 【Solr】solr的增删改查

    目录 创建工程 增 删 改 查 高量查询 回到顶部 创建工程 普通的java web工程即可,我采用的是spring mvc! 回到顶部 增 @Autowired private SolrServer ...

  2. 【ES】ElasticSearch初体验之使用Java进行最基本的增删改查~

    好久没写博文了, 最近项目中使用到了ElaticSearch相关的一些内容, 刚好自己也来做个总结. 现在自己也只能算得上入门, 总结下自己在工作中使用Java操作ES的一些小经验吧. 本文总共分为三 ...

  3. Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示

    Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...

  4. ASP.NET从零开始学习EF的增删改查

           ASP.NET从零开始学习EF的增删改查           最近辞职了,但是离真正的离职还有一段时间,趁着这段空档期,总想着写些东西,想来想去,也不是很明确到底想写个啥,但是闲着也是够 ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查

    系列目录 文章于2016-12-17日重写 在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的代码已经给出,里面包含了增删改,大家可以下载下来看下. 这讲主要是,制作漂亮的工具栏,虽 ...

  6. 通过Java代码实现对数据库的数据进行操作:增删改查

    在写代码之前,依然是引用mysql数据库的jar包文件:右键项目-构建路径-设置构建路径-库-添加外部JAR 在数据库中我们已经建立好一个表xs :分别有xuehao  xingming    xue ...

  7. Hibernate全套增删改查+分页

    1.创建一个web工程 2.导入jar包 3.创建Student表 4.创建实体类 package com.entity; public class Student { private Integer ...

  8. 使用 Json.Net 对Json文本进行 增删改查

    JSON 已经成为当前主流交互格式, 如何在C#中使用 Json.Net 对Json文本进行 增删改查呢?见如下代码 #region Create (从零创建) public static strin ...

  9. yii2 增删改查

    自己总结的yii2 advanced 版本的简单的增删改查,希望对大家有所帮助 1.gii生成的actionCreate()方法中 获取插入语句的id $id = $model->attribu ...

随机推荐

  1. Linux驱动程序学习【转】

    本文转载自: 一直在学习驱动,对于下面这篇文章,本人觉得简洁明了,基本符合我们学习驱动的进度与过程,现转发到自己的博客,希望能与更多的朋友分享. 了解Linux驱动程序技巧学习的方法很重要,学习lin ...

  2. HTML5的Video标签的属性,方法和事件汇总

    <video>标签的属性 src :视频的属性 poster:视频封面,没有播放时显示的图片 preload:预加载 autoplay:自动播放 loop:循环播放 controls:浏览 ...

  3. Python—操作redis

    Python操作redis 连接方式:点击 1.String 操作 redis中的String在在内存中按照一个name对应一个value来存储 set() #在Redis中设置值,默认不存在则创建, ...

  4. poj3069 Saruman's Army

    http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...

  5. 《编写可维护的JavaScript》——JavaScript编码规范(七)

    UI层的松耦合 在web开发中,用户界面(UI)是由三个彼此隔离又相互作用的层定义的. HTML用来定义页面的数据和语义. CSS用来给页面添加样式,创建视觉特征. JavaScript用来给页面添加 ...

  6. 【转】Description Resource Path Location Type Java compiler level&n

    转载地址:http://blog.sina.com.cn/s/blog_ae96abfd0101qbq0.html 在项目上右键Properties->Project Facets,在打开的Pr ...

  7. 解决对含有第三方jar包的项目打包出现java.lang.NoClassDefFoundError问题

    用eclipse普通的打包方式,对含有第三方jar包的项目进行打包.调用方法后一只出现java.lang.NoClassDefFoundError问题. 从网上搜寻,很多都是在MANIFEST.MF文 ...

  8. Oracle客户端配置

    1.  打开开发生产数据库系统,点击下载Oracle_12C_Client32,并且解压缩. 2.  找到文件下的setup.exe文件,并且执行. 3.  等待数秒,在如下界面中选择第二项,管理员, ...

  9. 首师大附中科创教育平台 我的刷题记录 0304 50095106扔核弹(XDC,你懂的)

    今天给大家献上"C"级题:50095106扔核弹(XDC,你懂的)!! 试题编号:0304   50095106扔核弹(XDC,你懂的) 难度级别:C: 运行时间限制:1000ms ...

  10. PHP---Mysql常用语法(增删改查)

    1.数据库操作:                                                                        创建数据库:create databas ...