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();
}
}
Solr的常用操作
最新推荐文章于 2023-05-21 10:27:37 发布