Solr的常用操作
来源:互联网 发布:eclipse 测试java程序 编辑:程序博客网 时间:2024/06/03 18:16
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.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
import cn.itcast.solr.domain.Article;
public class SolrjTest {
/**
* 根据SolrInputDocument添加数据或者更新数据
* @throws SolrServerException
* @throws IOException
*/
@Test
public void testCreateOrUpdateBySolrInputDocument() throws SolrServerException, IOException{
// 连接服务
String baseURL = "http://localhost:8080/solr/article/";
HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
// 创建SolrInputDocument
SolrInputDocument solrInputDocument = new SolrInputDocument();
solrInputDocument.addField("id", 1106327947);
solrInputDocument.addField("title", "macbook");
solrInputDocument.addField("content", "256G");
// 添加数据
httpSolrServer.add(solrInputDocument);
httpSolrServer.commit();
}
/**
* 通过注解添加或更新数据
* @throws SolrServerException
* @throws IOException
*/
@Test
public void testCreateOrUpdateByBean() throws SolrServerException, IOException{
// 连接服务
String baseURL = "http://localhost:8080/solr/article/";
HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
// Article自己写的一个对象
Article article = new Article();
article.setId("1106327948");
article.setTitle("oppoe");
article.setContent("充电五分钟通话两小时");
// 添加数据
httpSolrServer.addBean(article);
httpSolrServer.commit();
}
/**
* 根据SolrQuery查询
* @throws SolrServerException
* @throws IOException
*/
@Test
public void testQueryBySolrQuery() throws SolrServerException, IOException{
// 连接服务
String baseURL = "http://localhost:8080/solr/article/";
HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
//SolrParams params = new SolrQuery("三星"); //这样写也ok
//SolrQuery params = new SolrQuery("三星"); // SolrQuery可设置其他条件,这是默认字段的值
- SolrQuery params =new SolrQuery();
- params.set(("q", "id:1106327948"); // 设置制定字段的值
params.set("fl", "title");
QueryResponse queryResponse = httpSolrServer.query(params);
List<Article> articles = queryResponse.getBeans(Article.class);
System.out.println(articles);
}
/**
* 对结果高亮显示
* @throws SolrServerException
* @throws IOException
*/
@Test
public void testQueryByHL() throws SolrServerException, IOException{
// 连接服务
String baseURL = "http://localhost:8080/solr/article/";
HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
//SolrParams params = new SolrQuery("三星");
SolrQuery params = new SolrQuery("三星"); // SolrQuery可设置其他条件
params.addHighlightField("title");
params.setHighlight(true); // 开启高亮
params.setHighlightSimplePre("<em>");
params.setHighlightSimplePost("</em>");
QueryResponse queryResponse = httpSolrServer.query(params);
Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
System.out.println(map);
}
/**
* 根据id删除数据
* @throws SolrServerException
* @throws IOException
*/
@Test
public void testDeleteById() throws SolrServerException, IOException{
// 连接服务
String baseURL = "http://localhost:8080/solr/article/";
HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
httpSolrServer.deleteById("1105271766");
httpSolrServer.commit();
}
/**
* 先查后删
* @throws SolrServerException
* @throws IOException
*/
@Test
public void testDeleteByQuery() throws SolrServerException, IOException{
// 连接服务
String baseURL = "http://localhost:8080/solr/article/";
HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
httpSolrServer.deleteByQuery("title:全高清");
httpSolrServer.commit();
}
}
0 0
- Solr的常用操作
- solr常用操作
- 使用solrJ操作solr常用方法
- 3、solr的基本操作
- solrj 对solr 的操作
- solr的修改操作案例
- Solr java客户端的操作
- solr操作
- solr操作
- 一些常用的小玩意之solr
- 利用solrj操作solr的API
- (Slor)java对solr的基本操作
- SolrJ是操作Solr的JAVA客户端
- solr indexing 和基本的数据操作
- solr的optimize操作导致性能骤降
- Solr可视化简单的操作(二)
- 利用solrj操作solr的API
- Solr java客户端solrJ的CRUD操作
- 理想的程序员
- 类与对象
- Android Studio中使用lambda表达式
- 操作系统ucore lab2实验报告
- MyBatis/Ibatis中#和$的区别
- Solr的常用操作
- Oracle中的chr()函数 和 ascii()函数
- MySQL数据库交互基础类
- Hive 视图 索引 动态分区装载数据
- 字符串匹配算法 -- 暴力破解法(朴素法),RK算法,KMP算法
- cotangent matrix or laplacian mesh operator
- android中使用ViewPager实现无限轮播图
- C到C++完美过度(二)
- NYOJ746:整数划分(四)(区间dp)