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. [原创]win10 命令行出现问号而且无法chcp 936

    现象: 命令行中中文字符显示为问号,输入chcp 936会提示 invlalid page code. 解决: 设置-区域和语言-时钟,语言和区域-区域-更改位置-管理-非Unicode中所使用的语言 ...

  2. redhat 更新 python 为 2.7.6

    1. 下载 wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tgz 2. 解压,编译 tar zxvf Python-2.7.6.tgz ./ ...

  3. Angular初学

    简介: angularjs是基本js开发的一个前端类库,主要致力于减轻开发人员在开发Ajax应用过程中的痛苦,适合来做单应用. 客户端模板: Angualr中,模板和数据都会被发送到浏览器中,然后在客 ...

  4. iOS 图片轮播图(自动滚动)

    iOS 图片轮播图(自动滚动) #import "DDViewController.h" #define DDImageCount 5 @interface DDViewContr ...

  5. Andy - 又一款速度流畅的免费安卓 Android 模拟器 (支持手机无线控制电脑模拟器)

    随着 Genymotion.BlueStacks 等电脑上的 Android 模拟器流行起来之后,似乎很多人都发现在电脑上运行使用安卓APP软件.畅玩手机游戏确实很有乐趣. 今天我们又发现了一款全新免 ...

  6. False 等效值

    False 等效值 下面这些值将被计算出 false (also known as Falsy values): false undefined null 0 NaN 空字符串 ("&quo ...

  7. Css、javascript、dom(二)

    一.css常用标签及页面布局 1.常用标签 position(定位) z-index(定位多层顺序) background(背景) margin(外边距) padding(内边距) font-size ...

  8. prince2 证书有用吗

    prince2 证书有用吗  ? 项目管理是一件非常困难的事情,新闻里充斥着虽利润高却未能成功支付的项目案例.这是为什么呢? 最主要的原因是项目工作比日常的商业工作要困难的多.日常的商业工作往往是重复 ...

  9. 分表的一个记录---Ruby

    sql1=" UPDATE user_red_info_"sql2=" SET status = '#{status}', update_time = '#{update ...

  10. Maven常用的命令

    mvn archetype:generate 产生一个新的项目 mvn compile 会执行 mvn resources:resources    mvn compiler:compile两个过程 ...