Solr文档学习--Solrj的使用

article/2025/10/13 22:08:46

首先启动solr

solr.cmd start

这里写图片描述

SolrClient

主要通过SolrClient来连接到Solr服务器

这里写图片描述

SolrClient有4个实现类

CloudSolrClient

SolrJ client class to communicate with SolrCloud. Instances of this class communicate with Zookeeper to discover Solr endpoints for SolrCloud collections, and then use the {@link BHttpSolrClient} to issue requests.

用来连接到SolrCloud的

ConcurrentUpdateSolrClient

ConcurrentUpdateSolrClient buffers all added documents and writes them into open HTTP connections. This class is thread safe. Params from {@link UpdateRequest} are converted to http request parameters. When params change between UpdateRequests a new HTTP request is started.

线程安全的SolrClient

LBHttpSolrClient

LBHttpSolrClient or “LoadBalanced HttpSolrClient” is a load balancing wrapper around {@link HttpSolrClient}. This is useful when you have multiple Solr servers and the requests need to be Load Balanced among them.

负载均衡的HttpSolrClient

HttpSolrClient

A SolrClient implementation that talks directly to a Solr server via HTTP

通过HTTP直接连接到Solr服务器

SolrClient

javaDoc解释

There are two ways to use an HttpSolrClient:
1) Pass a URL to the constructor that points directly at a particular core

 
SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr/core1");
QueryResponse resp = client.query(new SolrQuery(":"));

In this case, you can query the given core directly, but you cannot query any other cores or issue CoreAdmin requests with this client.
2) Pass the base URL of the node to the constructor
 
SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr");
QueryResponse resp = client.query("core1", new SolrQuery(":"));

In this case, you must pass the name of the required core for all queries and updates, but you may use the same client for all cores, and for CoreAdmin requests.

这里写图片描述

正确的实现方式

在查询中指出collection(我的collection为mycollections)

    String url = "http://localhost:8983/solr";SolrClient client = new HttpSolrClient.Builder(url).build();QueryResponse resp = client.query("mycollections", new SolrQuery("*:*"));

在连接url中指出collection

    String url = "http://localhost:8983/solr/mycollections";SolrClient client = new HttpSolrClient.Builder(url).build();QueryResponse resp = client.query(new SolrQuery("*:*"));

管理员客户端的查询结果

这里写图片描述

看一下程序的运行结果

{responseHeader = {status = 0,QTime = 0,params = {q =  *  :  * ,wt = javabin,version = 2}},response = {numFound = 1,start = 0,docs = [SolrDocument {id = 123456,info = [我爱北京天安门],_version_ = 1544602190025326592}]}
}

顺利连接到服务器

添加/更新

定义一个文章实体,包括id,标题,时间,作者,内容

import java.util.Arrays;
import java.util.Date;import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.annotation.Id;public class Article {@Id@Fieldprivate String id;@Fieldprivate String[] title;@Fieldprivate Date[] time;@Fieldprivate String[] author;@Fieldprivate String[] info;// 省略getter和setter
}

初始化

    private static SolrClient client;private static String url;static {url = "http://localhost:8983/solr/mycollections";client = new HttpSolrClient.Builder(url).build();}

添加/更新方法

    /*** 保存或者更新solr数据* * @param res*/public static boolean saveSolrResource(Article article) {DocumentObjectBinder binder = new DocumentObjectBinder();SolrInputDocument doc = binder.toSolrInputDocument(article);try {client.add(doc);System.out.println(client.commit());} catch (Exception e) {e.printStackTrace();return false;}return true;}

测试

        Article article = new Article();article.setId("123456");article.setTitle(new String[] {"测试solr"});article.setAuthor(new String[]{"程高伟"});article.setTime(new Date[]{new Date()});article.setInfo(new String[]{"The Files screen lets you browse & view the various configuration files"});saveSolrResource(article);

这里写图片描述

如果id相同则是修改操作

删除

    /*** 删除solr 数据* * @param id*/public static boolean removeSolrData(String id) {try {client.deleteById(id);client.commit();} catch (Exception e) {e.printStackTrace();return false;}return true;}

根据id删除

查询

        SolrQuery query = new SolrQuery();query.setQuery("程高伟");QueryResponse rsp = client.query(query);List<Article> articleList = rsp.getBeans(Article.class);System.out.println(articleList);

结果

这里写图片描述

Solr还有很多高级功能。

目前存在的问题是只能是字符串,而且是字符串数组。

还有自定义schema好好研究一下。

参考文献

http://wiki.apache.org/solr/Solrj#Reading_Data_from_Solr

http://101.110.118.72/archive.apache.org/dist/lucene/solr/ref-guide/apache-solr-ref-guide-6.1.pdf


http://chatgpt.dhexx.cn/article/sjpwbuYI.shtml

相关文章

solrj 对solr 的操作

使用SolrJ操作Solr会比利用httpClient来操作Solr要简单。SolrJ是封装了httpClient方法&#xff0c;来操作solr的API的。SolrJ底层还是通过使用httpClient中的方法来完成Solr的操作。 1、 首先&#xff0c;你需要添加如下jar包 其中apache-solr-solrj-3.4.0.jar、slf4j-api-1.6…

SolrJ的使用

CommonsHttpSolrServer CommonsHttpSolrServer 使用HTTPClient 和solr服务器进行通信。 Java代码 String url "http://localhost:8983/solr"; SolrServer server new CommonsHttpSolrServer( url ); String url "http://localhost:8983/solr";Sol…

sorl

solr&#xff1a; 层面搜索、命中醒目显示并且支持多种输出格式&#xff08;包括 XML/XSLT 和 JSON 格式&#xff09;&#xff0c;Solr 可以阅读和使用构建到其他 Lucene 应用程序中的索引。 安装&#xff1a;http://blog.csdn.net/millery22/article/details/51446014 对外提…

Solr和Solrj的使用

Solr(4.10.3)&Solrj 部署环境 1.拷贝solr solr-4.10.3\example\solr 目录到【如E:/solr/】,重命名solr为solrhome 【E:/solr/solrhome】 solrhome:solr里面有几个文件 collection1:有一个默认名称为collection1的SolrCore索引库 conf&#xff1a;SolrCore运行配置信息 co…

SolrJ的查询

1.solr是一个全文检索引擎系统,通过部署到tomcat下就可以独立运行,通过http协议对外提供全文检索服务, 就是索引和文档的正删改查服务 2. solr直接操作索引库和文档库, 我们的业务系统中可以使用solrJ(solr的客户端,就是一堆jar包)来调用solr服务端, 让solr服务端操作文档库和…

使用solrJ操作solr常用方法

既然学的是java那么肯定需要用java代码来进行对solr的操作&#xff0c;如果知道在solr后台管理界面进行增删改查等操作&#xff0c;那么用solrJ操作solr会更好理解。 solrJ介绍 solrJ是一个用来访问solr的java客户端&#xff0c;提供了索引和搜索的方法&#xff08;将一些常用…

solr快速上手:整合SolrJ实现客户端操作(九)

0. 引言 我们前面学习了solr的服务端基础操作&#xff0c;实际项目中我们还需要在客户端调用solr&#xff0c;就像调用数据库一样&#xff0c;我们可以基于solrJ来实现对solr的客户端操作 1. SolrJ简介 SolrJ 是 Solr官方提供的 Java 客户端库&#xff0c;主要用于与 Solr 服…

全文检索solr(五)Solrj的使用

什么是solrj solrj是访问Solr服务的java客户端&#xff0c;提供索引和搜索的请求方法&#xff0c;如下图&#xff1a; Solrj和图形界面操作的区别就类似于数据库中使用jdbc和mysql客户端的区别一样。 需求 使用solrj调用solr服务实现对索引库的增删改查操作。 环境准备 So…

Solr-Solrj简单使用

一、SolrJ简介 SolrJ是操作Solr的Java客户端&#xff0c;它提供了增加、修改、删除、查询Solr索引的Java接口。通过solrJ提供的API接口来操作solr服务&#xff0c;SolrJ底层是通过使用httpClient中的方法来完成Solr的操作。 二、依赖配置 <dependency><groupId>…

solrj

文章目录 1.什么是solrj?2.搭建工程2.1.导入相关jar包 3.对索引库做增删改查3.1.添加&#xff08;以实体类的方式&#xff09;添加方法 3.3.修改&#xff08;update&#xff09;3.4.删除以ID删除批量删除&#xff08;以ID&#xff09;以条件删除&#xff08;query&#xff09;…

Solr系列四:Solr(solrj 、索引API 、 结构化数据导入)

一、SolrJ介绍 1. SolrJ是什么&#xff1f; Solr提供的用于JAVA应用中访问solr服务API的客户端jar。在我们的应用中引入solrj&#xff1a; <dependency><groupId>org.apache.solr</groupId><artifactId>solr-solrj</artifactId><version>7…

solr学习之solrj

solrJ是访问Solr服务的JAVA客户端&#xff0c;提供索引和搜索的请求方法&#xff0c;SolrJ通常嵌入在业务系统中&#xff0c;通过solrJ的API接口操作Solr服务。 一 .maven的环境jar包配置 <!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj --><d…

Pytorch实现逻辑斯蒂回归模型 代码实操

初学者学习Pytorch系列 第一篇 Pytorch初学简单的线性模型代码实操 第二篇 Pytorch实现逻辑斯蒂回归模型 代码实操 文章目录 初学者学习Pytorch系列前言一、先上代码二、测试结果1. 数据结果2.画图结果 总结 前言 上一篇的数据中&#xff0c;是这样子的例子 x_data代表的学习的…

回归分析(三)二项逻辑斯蒂回归模型

回归分析&#xff08;三&#xff09;二项逻辑斯蒂回归 学了一段时间突然又遇到逻辑斯蒂回归&#xff0c;结果发现已经忘完了&#xff0c;所以今天重新梳理一下。 &#xff08;1&#xff09;逻辑斯蒂分布 先看一下逻辑斯蒂分布函数 F ( x ) F(x) F(x)&#xff0c;其概率密度函数…

回归分析:逻辑斯蒂回归模型,可视化分类决策边界

文章目录 逻辑斯蒂回归模型逻辑斯蒂回归模型python案例 逻辑斯蒂回归模型 前面的例子都是在用线性模型解决回归任务&#xff0c;那么线性模型能否完成分类任务呢&#xff1f;相较于回归任务&#xff0c;分类任务的预测值是离散的&#xff0c;比如二分类问题&#xff0c;可以用…

机器学习:逻辑斯蒂回归

目录 逻辑回归模型介绍逻辑斯蒂分布二项逻辑斯谛回归模型目标函数 逻辑回归模型介绍 原理&#xff1a; 逻辑斯谛回归&#xff08;logistic regression&#xff09;是经典的分类方法&#xff0c;它属于对数线性模型&#xff0c;原理是根据现有的数据对分类边界线建立回归公式&a…

《PyTorch深度学习实践》06 逻辑斯蒂回归 代码

视频&#xff1a;06.逻辑斯蒂回归_哔哩哔哩_bilibili 参考文章&#xff1a;pytorch 深度学习实践 第6讲 逻辑斯蒂回归_会游泳的小雁的博客-CSDN博客 网络模型的基本框架 1步骤&#xff1a; 1.Prepare dataset 2.Design model using Class &#xff08;inherit from nn.Modul…

逻辑斯蒂回归 matlab实现

说明 我将试图从感知机的基础上说明逻辑回归的一般性原理和学习及预测方法&#xff0c;其中缺少一些必要的证明&#xff0c;包括了一个二分类问题的实例。其中关于感知机的实验在 机器学习 专栏中有介绍。 从感知机到逻辑斯蒂回归 感知机模型&#xff1a; 应用范围&#xf…

Lecture6 逻辑斯蒂回归(Logistic Regression)

目录 1 常用数据集 1.1 MNIST数据集 1.2 CIFAR-10数据集 2 课堂内容 2.1 回归任务和分类任务的区别 2.2 为什么使用逻辑斯蒂回归 2.3 什么是逻辑斯蒂回归 2.4 Sigmoid函数和饱和函数的概念 2.5 逻辑斯蒂回归模型 2.6 逻辑斯蒂回归损失函数 2.6.1 二分类损失函数 2.…

机器学习之逻辑斯蒂回归

目录 一、分类与回归 二、逻辑回归不是回归 三、生成式逻辑回归 四、判别式逻辑回归 五、逻辑回归为什么不用均方误差做损失函数 六、判别模型与生成模型的比较 七、写在最后 一、分类与回归 回归与分类是机器学习的基本问题。回归是预测连续值&#xff0c;分类是预测…