lucene_09_solrj的使用
什么是solrj
solrj 是访问Solr 服务的java客户端,提供索引(增删改)和搜索(查)的请求方法,Solrj 通常在嵌入在业务系统中,通过Solrj的API接口操作Solr服务,如下图:

solr的使用
第一步:配置pom.xml
内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.chen</groupId>
<artifactId>solr</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>solr</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.2.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency> </dependencies>
</project>
第二步:java代码
/**
* FileName: SolrManager
* Author: GET_CHEN
* Date: 2018/4/3 14:29
* Description:
*/
package com.chen; import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
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 org.junit.Test; import java.io.IOException;
import java.util.List;
import java.util.Map; public class SolrManager {
@Test
public void testAdd() throws IOException, SolrServerException { String baseSolrUrl = "http://localhost:8080/solr/collection2"; //solr的访问路径。collection2是solr的核。默认是collection1.
//连接solr服务器
HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(baseSolrUrl).build(); SolrInputDocument document = new SolrInputDocument();
document.addField("id", "333");
document.addField("blog_title", "solrj的使用");
document.addField("blog_keyWord", "solrj"); httpSolrClient.add(document); httpSolrClient.commit();
httpSolrClient.close(); } @Test
public void testDelete() throws IOException, SolrServerException {
String baseSolrUrl = "http://localhost:8080/solr/collection2";
//连接solr服务器
HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(baseSolrUrl).build();
// httpSolrClient.deleteById("333");//通过id删除
httpSolrClient.deleteByQuery("*:*");//通过语法删,删除全部
httpSolrClient.commit();
httpSolrClient.close();
} @Test
public void testQuery() throws Exception {
String baseSolrUrl = "http://localhost:8080/solr/collection2";
System.out.println(baseSolrUrl);
//连接solr服务器
HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(baseSolrUrl).build();
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("*:*");
QueryResponse queryResponse = httpSolrClient.query(solrQuery);
SolrDocumentList results = queryResponse.getResults();
for (SolrDocument document: results) {
System.out.println(document.get("id"));
System.out.println(document.get("blog_title"));
System.out.println(document.get("blog_summary"));
System.out.println(document.get("blog_keyWord"));
System.out.println("==========================");
}
httpSolrClient.commit();
httpSolrClient.close();
} @Test
public void testDiffQuery() throws Exception{ String baseSolrUrl = "http://localhost:8080/solr/collection2";
//连接solr服务器
HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(baseSolrUrl).build();
SolrQuery solrQuery = new SolrQuery();
//关键字
solrQuery.setQuery("大学");
//设置默认域
solrQuery.set("df","blog_title");
//设置查询域
solrQuery.set("fl","id,blog_summary");
//排序
solrQuery.setSort("id", SolrQuery.ORDER.desc);
//分页
solrQuery.setStart(0);
solrQuery.setRows(3);
//高亮
solrQuery.setHighlight(true);
//指定高亮域
solrQuery.addHighlightField("blog_title");
solrQuery.setHighlightSimplePre("<span style='color:red'>");
solrQuery.setHighlightSimplePost("</span>");
QueryResponse queryResponse = httpSolrClient.query(solrQuery);
SolrDocumentList results = queryResponse.getResults();
//获取高亮内容
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting(); for (SolrDocument document: results) {
System.out.println(document.get("id"));
System.out.println(document.get("blog_title"));
System.out.println(document.get("blog_summary"));
System.out.println(document.get("blog_keyWord"));
System.out.println("==========================");
Map<String, List<String>> map = highlighting.get(document.get("id"));
List<String> blog_summary = map.get("blog_title");
System.out.println(blog_summary.get(0));
System.out.println("==========highlighting===============");
}
httpSolrClient.commit();
httpSolrClient.close(); }
}
lucene_09_solrj的使用的更多相关文章
随机推荐
- spring mvc文件上传,request对象转换异常
spring 文件上传有现成的工具用起来也挺简单.就是在还不是非常熟悉的时候可能会出一些错. 近期碰到了 org.apache.catalina.connector.RequestFacade can ...
- 学习笔记——AOP
以下纯属个人刚了解点皮毛,一知半解情况下的心得体会: ==================================================================== AOP( ...
- jQuery - 制作点击显示二级菜单效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- codeforce1046 Bubble Cup 11 - Finals 题解
比赛的时候开G开了3h结果rose说一句那唯一一个AC的是羊的心态就崩了.. 这套题感觉质量挺好然后就back了下 A: AI robots 有三个限制条件:相互能够看见和智商的差.使用主席树,可以维 ...
- element快速开发建站的动态UI------优
网站快速成型工具 只为这样的你: Element,一套为开发者.设计师和产品经理准备的基于 Vue 2.0 的组件库,提供了配套设计资源,帮助你的网站快速成型 http://element.elem ...
- Oracle 12.2.0.1 RAC for rhel 7.X 数据库安装(节点1执行root.sh失败)
说明: 最开始是用的rehat7.2安装12.2.0.1,后面安装GI节点一执行root.sh脚本失败,排查原因,最开始以为是操作系统的问题,换成rehat7.6,同样的出现问题,经过一番折腾,后面通 ...
- B - Is your horseshoe on the other hoof?
Problem description Valera the Horse is going to the party with friends. He has been following the f ...
- supervisord 使用记录
#supervisor简介 Supervisor是一个 Python 开发的 client/server 系统,可以管理和监控类 UNIX 操作系统上面的进程. #组成部分 supervisord(s ...
- MySQL定期执行任务相关问题
在sqlyog某数据库下的事件里新建事件,并写入一下代码: DELIMITER $$ ALTER DEFINER=`root`@`%` EVENT `0` ON SCHEDULE EVERY 24 H ...
- Java多线程-synchronized关键字
进程:是一个正在执行中的程序.每一个进程执行都有一个执行顺序.该顺序是一个执行路径,或者叫一个控制单元. 线程:就是进程中的一个独立的控制单元.线程在控制着进程的执行. 一个进程中至少有一个线程 Ja ...