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的使用的更多相关文章
随机推荐
- EarthWarrior3D游戏ios源代码
这是一款不错的ios源代码源代码,EarthWarrior3D游戏源代码. 而且游戏源码支持多平台. 适用于cocos v2.1.0.0版本号 源代码下载: http://code.662p.com/ ...
- 【译文】利用STAN做贝叶斯回归分析:Part 2 非正态回归
[译文]利用STAN做贝叶斯回归分析:Part 2 非正态回归 作者 Lionel Hertzogn 前一篇文章已经介绍了怎样在R中调用STAN对正态数据进行贝叶斯回归.本文则将利用三个样例来演示怎样 ...
- 解析HTTP协议六种请求方法
标准Http协议支持六种请求方法,即: 1,GET 2,HEAD 3,PUT 4,DELETE 5,POST 6,OPTIONS 但其实我们大部分情况下只用到了GET和POST.如果想设计一个符合RE ...
- java的list类
java的list类 目录: list中添加,获取,删除元素: list中是否包含某个元素: list中根据索引将元素数值改变(替换): list中查看(判断)元素的索引: 根据元素索引位置进行的判断 ...
- Error-Java-IJ:Imported project refers to unknown jdks JavaSE-1.7
ylbtech-Error-Java-IJ:Imported project refers to unknown jdks JavaSE-1.7 Import from EclipseImported ...
- CSS--浏览器CSS Hack 收集
所谓的Hack就是只有特定浏览器才能识别这段hack代码.Hack 不是什么好东西,除非没有办法,我们尽量还是不要用着玩意. 下面是各个浏览器的CSS Hack 列表. Firefox 浏览器 @-m ...
- HDU3085 Nightmare Ⅱ
题目: Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were ...
- [转]深入javascript——原型链和继承
在上一篇post中,介绍了原型的概念,了解到在javascript中构造函数.原型对象.实例三个好基友之间的关系:每一个构造函数都有一个“守护神”——原型对象,原型对象心里面也存着一个构造函数的“位置 ...
- RecyclerView的基础用法
为了让RecyclerView可以在所有的Android版本中都能使用,Android开发团队将RecyclerView定义在support.v7包当中.在使用该控件时需要打开当前Modile的bui ...
- 附加MySQL数据库的方法
下面讲解附加MySQL数据库的方法. (1)将“光盘\Code\04\Project\数据库”文件夹中的扩展名为.sql的文件拷贝到本地机器中. (2)在如图1所示的MySQL工作台界面中,单击Dat ...