Solr的常用操作,
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*/@Testpublic void testCreateOrUpdateBySolrInputDocument() throws SolrServerException, IOException{// 连接服务String baseURL = "http://localhost:8080/solr/article/";HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址// 创建SolrInputDocumentSolrInputDocument solrInputDocument = new SolrInputDocument();solrInputDocument.addField("id", 1106327947);solrInputDocument.addField("title", "macbook");solrInputDocument.addField("content", "256G");// 添加数据httpSolrServer.add(solrInputDocument);httpSolrServer.commit();}/*** 通过注解添加或更新数据* @throws SolrServerException* @throws IOException*/@Testpublic 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*/@Testpublic 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*/@Testpublic 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*/@Testpublic 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*/@Testpublic void testDeleteByQuery() throws SolrServerException, IOException{// 连接服务String baseURL = "http://localhost:8080/solr/article/";HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址httpSolrServer.deleteByQuery("title:全高清");httpSolrServer.commit();}}
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。