什么是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的使用的更多相关文章

随机推荐

  1. 托管在IIS上的wcf,在启动的时候,写log

    https://blogs.msdn.microsoft.com/wenlong/2006/01/11/how-to-initialize-hosted-wcf-services/ Using App ...

  2. 利用递归分割(Split)字符串

    利用递归分割(Split)字符串 SqlServer 递归 工作需要将表里的某个字段分割之后再插入到另一个表中,其实数据量不大,直接用游标一行一行的取,再利用循环来分割之后再实现数据的插入应该可以直接 ...

  3. php的self this parent的区别

    {一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行P ...

  4. 修改DIV滚动条样式

    /*滚动条样式*/ div::-webkit-scrollbar { /*滚动条整体样式*/ width: 5px; /*高宽分别对应横竖滚动条的尺寸*/ height: 5px; } div::-w ...

  5. html中canvas渲染图片,并转化成base64格式保存

    最近在做一个上传头像然后保存显示的功能,因为涉及到裁剪大小和尺寸比例,所以直接上传图片再展示的话,就会出现问题,所以就想用canvas来渲染裁剪后的图片,然后转化成base64格式的图片再存储,这样取 ...

  6. Spring Boot (13) druid监控

    druid druid是和tomcat jdbc一样优秀的连接池,出自阿里巴巴.除了连接池,druid哈hi有一个很实用的监控功能. pom.xml 添加了以下依赖后,会自动用druid连接池替代默认 ...

  7. Jquery IE8兼容性

    环境: jsp+jquery-1.11.1.min.js 问题描述: 使用$("#article标签id名").append(“xxxxxxxxx") ,chrome.f ...

  8. ios -使用NSLayoutConstraint实现多个view等宽等高等间距

    @interface ViewController () { UIView *firstView; UIView *secondView; UIView *thirdView; } @end @imp ...

  9. Laravel5.1学习笔记7 视图

    视图 (View) 基本用法 传递数据到视图 在多个视图中分享数据 视图组件   #基本用法 视图里面包含了你应用程序所提供的 HTML 代码,并且提供一个简单的方式来分离控制器和网页呈现上的逻辑.视 ...

  10. WIN10 64位下VS2015 C#直接添加 halcon 12导出的CS文件实现视觉检测

    C# halcon 12 联合编程的 实例 1.先调试好halcon程序,我以读取图片的程序为例.