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的使用的更多相关文章
随机推荐
- 飘逸的python - 极简的二叉树前中后序通杀函数
对于任一结点.能够按某种次序运行三个操作: 訪问结点本身(N) 遍历该结点的左子树(L) 遍历该结点的右子树(R) 用来表示顺序,即,前序NLR/中序LNR/后序LRN. 以下我们用namedtupl ...
- SQL SERVER的整型运算,让人大吃一惊
SQL SERVER的整型运算,让人大吃一惊好比下面代码:DECLARE @i INT = 50;DECLARE @dec DECIMAL(18,4);SET @dec = @i / 100;SELE ...
- 【c语言】字符串替换空格:请实现一个函数,把字符串中的每一个空格替换成“%20”
// 字符串替换空格:请实现一个函数,把字符串中的每一个空格替换成"%20". // 比如输入"we are happy.",则输出"we%20are ...
- System.IO.Path 操作
System.IO.Path 分类: C#2011-03-23 10:54 1073人阅读 评论(0) 收藏 举报 扩展磁盘string2010c System.IO.Path提供了一些处理文件名和路 ...
- B1270 [BeijingWc2008]雷涛的小猫 dp
这个题的原始方法谁都会,但是n^3会T.之后直接优化,特别简单,就是每次处理出来每层的最大值,而不用枚举.之前没这么做是因为觉得在同一棵树的时候没有下落,所以不能用这个方法.后来想明白了,在同一棵树上 ...
- 74.资金管理-员工工资配置 extjs 页面
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...
- spring 监听器 IntrospectorCleanupListener简介
转自:https://blog.csdn.net/ywb201314/article/details/51144256 其中JavaBeans Introspector是一个类,位置在Java.bea ...
- nodejs--Nodejs单元测试小结
前言 最近在写一课程的Project,用Node写了一个实时聊天小应用,其中就用到了单元测试.在写Node单元测试的时候,一方面感受到了单元测试的重要性,另一方面感受到了Node单元测试的不够成熟,尚 ...
- IPv6系列-初学者的10个常见困扰
本文是<IPv6系列>文章的第二篇<常见困扰>,紧接<入门指南>,用于解答IPv6的10个常见困扰. 小慢哥的原创文章,欢迎转载 目录 ▪ 本文缘由 ▪ 困扰1. ...
- [转]RDLC报表——动态添加列
本文转自:http://www.cnblogs.com/pszw/archive/2012/07/19/2599937.html 前言 最近接到一个需求:在给定的数据源中,某(些)列,可能需要单独统计 ...