赞
踩
- public class Product {
- //Field中的value要遵循核心中schema中<fields>的规则
- @Field(value="id")
-
- private String id;
-
- @Field(value="name")
- private String name;
-
- @Field(value="description")
- private String desc;
-
- @Field(value="price")
- private float price;
- // setter getter
- // ......
- }
@Field 表示 当前属性与索引库中哪个已经声明的字段名相对应 (具体规则由schema指定)
- package liuxun.solrj.dao;
-
- import java.io.IOException;
- import java.util.List;
- import java.util.Map;
-
- import org.apache.solr.client.solrj.SolrQuery;
- import org.apache.solr.client.solrj.SolrServer;
- import org.apache.solr.client.solrj.SolrServerException;
- import org.apache.solr.client.solrj.impl.HttpSolrServer;
- import org.apache.solr.client.solrj.response.QueryResponse;
- import org.apache.solr.common.SolrDocument;
- import org.apache.solr.common.SolrDocumentList;
- import org.apache.solr.common.SolrInputDocument;
-
- import liuxun.solrj.bean.Product;
-
- /**
- * 使用solrJ 向solr 提交请求,增删改查, solrJ 底层页是发送http 协议...
- *
- * @author liuxun
- */
- public class SolrDao {
- // 在solr 当中有一些默认的字段和动态字段规则,配置在文件/example/solr/collection1/conf/schema.xml
-
- // 添加默认索引属性
- public void addDefaultField() throws SolrServerException, IOException {
- // 声明要连接solr服务器的地址
- String url = "http://localhost:8983/solr";
- SolrServer solr = new HttpSolrServer(url);
- SolrInputDocument doc = new SolrInputDocument();
- doc.addField("id", "默认情况下必须添加的字段,用来区分文档的唯一标识");
- doc.addField("name", "默认的名称属性字段");
- doc.addField("description", "默认的表示描述信息的字段");
- solr.add(doc);
-
- solr.commit();
- }
-
- // 添加动态索引属性
- public void addDynamicField() throws SolrServerException, IOException {
- // 声明要连接solr服务器的地址
- String url = "http://localhost:8983/solr";
- SolrServer solr = new HttpSolrServer(url);
-
- SolrInputDocument doc = new SolrInputDocument();
- doc.addField("id", "adwweqwewe");
- doc.addField("nam_s", "动态字段的StringField类型格式为*_s");
- doc.addField("desc_s", "动态字段的TextField类型格式为*_t");
- solr.add(doc);
- solr.commit();
- }
-
- // 添加索引
- public void addIndex(Product product) throws SolrServerException, IOException {
- // 声明要连接solr服务器的地址
- String url = "http://localhost:8983/solr";
- SolrServer solr = new HttpSolrServer(url);
- solr.addBean(product);
- solr.commit();
- }
-
- // 更新索引
- public void updateIndex(Product product) throws IOException, SolrServerException {
- // 声明要连接solr服务器的地址
- String url = "http://localhost:8983/solr";
- SolrServer solr = new HttpSolrServer(url);
- solr.addBean(product);
- solr.commit();
- }
-
- // 删除索引
- public void delIndex(String id) throws SolrServerException, IOException {
- // 声明要连接solr服务器的地址
- String url = "http://localhost:8983/solr";
- SolrServer solr = new HttpSolrServer(url);
- solr.deleteById(id);
- // solr.deleteByQuery("id:*");
- solr.commit();
- }
-
- // 查找索引
- public void findIndex() throws SolrServerException {
- // 声明要连接solr服务器的地址
- String url = "http://localhost:8983/solr";
- SolrServer solr = new HttpSolrServer(url);
-
- // 查询条件
- SolrQuery solrParams = new SolrQuery();
- solrParams.setStart(0);
- solrParams.setRows(10);
- solrParams.setQuery("name:苹果 +description:全新4G");
- // 开启高亮
- solrParams.setHighlight(true);
- solrParams.setHighlightSimplePre("<font color='red'>");
- solrParams.setHighlightSimplePost("</font>");
-
- // 设置高亮的字段
- solrParams.setParam("hl.fl", "name,description");
- // SolrParams是SolrQuery的子类
- QueryResponse queryResponse = solr.query(solrParams);
-
- // (一)获取查询的结果集合
- SolrDocumentList solrDocumentList = queryResponse.getResults();
-
- // (二)获取高亮的结果集
- // 第一个Map的键是文档的ID,第二个Map的键是高亮显示的字段名
- Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
-
- for (SolrDocument solrDocument : solrDocumentList) {
- System.out.println("=====" + solrDocument.toString());
- Map<String, List<String>> fieldsMap = highlighting.get(solrDocument.get("id"));
- List<String> highname = fieldsMap.get("name");
- List<String> highdesc = fieldsMap.get("description");
- System.out.println("highname======" + highname);
- System.out.println("highdesc=====" + highdesc);
- }
-
- // (三) 将响应结果封装到Bean
- List<Product> products = queryResponse.getBeans(Product.class);
-
- System.out.println(products + "+++++++++++");
- for (Product product : products) {
- System.out.println(product);
- }
- }
- }
测试:
- package liuxun.solrj.junit;
-
- import static org.junit.Assert.*;
-
- import java.io.IOException;
-
- import org.apache.solr.client.solrj.SolrServerException;
- import org.junit.Before;
- import org.junit.Test;
-
- import liuxun.solrj.bean.Product;
- import liuxun.solrj.dao.SolrDao;
-
- public class SolrDaoTest {
-
- private SolrDao solrDao = new SolrDao();
-
- @Test
- public void testAddDefaultField() throws SolrServerException, IOException {
- solrDao.addDefaultField();
- }
-
- @Test
- public void testAddDynamicField() throws SolrServerException, IOException {
- solrDao.addDynamicField();
- }
-
- @Test
- public void testAddIndex() throws SolrServerException, IOException {
- for(int i=1;i<=2;i++){
- Product product=new Product();
- product.setId(String.valueOf(i));
- product.setName("苹果(Apple)iPhone 5c 16G版 3G手机(白色)电信版");
- product.setDesc("选择“购机送费版”北京用户享全新4G合约套餐资费全网最低!");
- product.setPrice(9f);
- solrDao.addIndex(product);
- }
- }
-
- @Test
- public void testUpdateIndex() throws IOException, SolrServerException {
- Product product=new Product();
- product.setId(String.valueOf(2));
- product.setName("毛衣的毛衣黑色毛衣");
- product.setDesc("女士专用 三点不漏..");
- product.setPrice(9f);
- solrDao.updateIndex(product);
- }
-
- @Test
- public void testDelIndex() throws SolrServerException, IOException {
- solrDao.delIndex("2");
- }
-
- @Test
- public void testFindIndex() throws SolrServerException {
- solrDao.findIndex();
- }
-
- }
控制台打印:
- <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
- <lst name="defaults">
- <str name="config">data-config.xml</str>
- </lst>
- </requestHandler>
然后再solrconfig.xml文件统计的目录下新增上面声明的data-config.xml。内容如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <dataConfig>
- <dataSource type="JdbcDataSource"
- driver="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/solr_test"
- user="root"
- password="root"/>
- <document>
- <entity name="id" query="select id,name,manu from solr" dataSource="JdbcDataSource"></entity>
- </document>
- </dataConfig>
因为需要连接数据库所以需要连接数据库的jar 包,以及操作索引库的jar 包,在collection 下面创建lib 目录将jar 包拷贝到lib 目录下 (
solr-dataimporthandler-4.7.0,
solr-dataimporthandler-extras-4.7.0以及连接
mysql 的jar 包)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。