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的使用的更多相关文章
随机推荐
- Android中Calendar类的用法总结
Calendar是Android开发中需要获取时间时必不可少的一个工具类,通过这个类可以获得的时间信息还是很丰富的,下面做一个总结,以后使用的时候就不用总是去翻书或者查资料了. 在获取时间之前要先获得 ...
- sublime -text 删除已安装插件
按ctr+shift +p然后输入remove 回车,再输入要删除的插件名
- php 原生简版日志导出
<?phpfunction writeLog($msg){ $logFile = date('Y-m-d').'.txt'; $msg = date('Y-m-d H:i:s').' >& ...
- Your configuration specifies to merge with the ref 'refs/heads/v.autoCheckProduct.20190325' from the remote, but no such ref was fetched.
问题: 创建新的分支,当我们执行git pull,出现如下错误 解决办法: 1.切换到主分支(或者被依赖的分支,也就是你从哪个分支上拉取新的分支),博主这里是master分支 2.执行以下两个命令 3 ...
- mvp 不错的链接
http://www.imooc.com/wenda/detail/216700 http://www.cnblogs.com/mybkn/archive/2012/04/12/2443676.htm ...
- 第一课trie 树 POJ 2001
最短前缀(Openjudge上抄的) 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的 ...
- mac下idea卡顿问题解决
idea在加载相对来说比较大的系统时,经常性出现卡顿,就是直接卡死,以至于写起代码特别难受. 最后找到的解决方案是修改idea.vmoptions中的内存大小 执行 find / -name idea ...
- MVC异步上传图片到本地/服务器
这两天朋友问我,有没有异步上传图片到本地/服务器这种demo,他有用, 我就想,好吧, 那刚好周末了,整理一套出来. 主要用到的是jquery uploadify 这个juqery的插件 ,可以无刷新 ...
- Android开发圆形ImageView实现
效果图如下 1.自定义属性,在value文件夹下新建attrs文件,声明如下属性 <declare-styleable name="CircleImageView"> ...
- 安装pywinauto的步骤
team准备搞自动化测试(桌面WPF系统),这几天一直在找自动化测试工具.发现了pywinauto这款工具,许多网友反应很好用,于是下载下来试用.不得不说遇到的坑真不少,记录下来以备不时之需. 前段时 ...