solrj

article/2025/10/14 0:12:14

文章目录

  • 1.什么是solrj?
  • 2.搭建工程
    • 2.1.导入相关jar包
  • 3.对索引库做增删改查
    • 3.1.添加(以实体类的方式)
      • 添加方法
    • 3.3.修改(update)
    • 3.4.删除
      • 以ID删除
      • 批量删除(以ID)
      • 以条件删除(query)
      • 删除所有
    • 3.5.查询

1.什么是solrj?

SolrJ是操作Solr的JAVA客户端,它提供了增加、修改、删除、查询Solr 索引的JAVA接口。SolrJ针对Solr,提供了Rest的 HTTP接口进行了封装,SolrJ底层是通过使用HttpClient中的方法来完成Solr的操作。

2.搭建工程

2.1.导入相关jar包

非maven工程需要将solr安装包目录\dist\solrj-lib\内的所有jar导入

<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<dependency><groupId>org.apache.solr</groupId><artifactId>solr-solrj</artifactId><version>8.7.0</version>
</dependency>
<dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version>
</dependency>

3.对索引库做增删改查

3.1.添加(以实体类的方式)

solrj支持对数据表做实体映射连接,通过提供的@field注解,来与实体类做隐式映射

注解作用
@Field用来对应实体类的属性做域映射

data-config.xml文件里面的实体映射
在这里插入图片描述

package test_solrj;import org.apache.solr.client.solrj.beans.Field;public class Commodity {@Field("id")private String id;@Field("prod_title")private String title;@Field("prod_origin_price")private Double original_price;@Field("prod_price")private Double price;@Field("prod_category_name")private String category_name;@Field("prod_pic_url")private String main_pic_url;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public Double getOriginal_price() {return original_price;}public void setOriginal_price(Double original_price) {this.original_price = original_price;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}public String getCategory_name() {return category_name;}public void setCategory_name(String category_name) {this.category_name = category_name;}public String getMain_pic_url() {return main_pic_url;}public void setMain_pic_url(String main_pic_url) {this.main_pic_url = main_pic_url;}@Overridepublic String toString() {return "Commodity [id=" + id + ", title=" + title + ", original_price=" + original_price + ", price=" + price+ ", category_name=" + category_name + ", main_pic_url=" + main_pic_url + "]";}public Commodity() {super();}}

添加方法

public class Test01 {final static String baseUrl = "http://localhost:8081/solr/core_01";@Testpublic void addIndex() throws IOException, SolrServerException {// 1.建立连接HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();// 2.创建一个文档对象SolrInputDocument inputDocument = new SolrInputDocument();// 向文档中添加域以及对应的值(注意:所有的域必须在schema.xml中定义过,前两篇导入时已定义)inputDocument.addField("id", "6");inputDocument.addField("prod_title", "测试表标题");inputDocument.addField("prod_category_name", "测试分类");inputDocument.addField("prod_origin_price", 99.9);inputDocument.addField("prod_price", 87.0);inputDocument.addField("prod_pic_url", "test");// 3.添加UpdateResponse response = client.add(inputDocument);// 4.提交,关闭连接client.commit();client.close();System.out.println("添加完成");}}

在这里插入图片描述

3.3.修改(update)

solrj提供的修改其实是根据索引库的id来操作的,同样是用添加的方式
添加的时候会检查提供的唯一id,如果当前id不存在则直接添加,如果id存在,这会做覆盖的操作
因为索引库的id是具有唯一性的

我们修改一下上面的插入语句,id不变,再次执行语句,没有新插入,标题修改了

public class Test01 {final static String baseUrl = "http://localhost:8081/solr/core_01";@Testpublic void addIndex() throws IOException, SolrServerException {// 1.建立连接HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();// 2.创建一个文档对象SolrInputDocument inputDocument = new SolrInputDocument();// 向文档中添加域以及对应的值(注意:所有的域必须在schema.xml中定义过,前两篇导入时已定义)inputDocument.addField("id", "6");inputDocument.addField("prod_title", "修改了");inputDocument.addField("prod_category_name", "测试分类");inputDocument.addField("prod_origin_price", 99.9);inputDocument.addField("prod_price", 87.0);inputDocument.addField("prod_pic_url", "test");// 3.添加UpdateResponse response = client.add(inputDocument);// 4.提交,关闭连接client.commit();client.close();System.out.println("修改完成");}}

在这里插入图片描述

3.4.删除

删除有很多中方法
常用的通过id删除,
通过查询语句删除

以ID删除

public class Test01 {final static String baseUrl = "http://localhost:8081/solr/core_01";@Testpublic void addIndex() throws IOException, SolrServerException {// 1.建立连接HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();// 以id删除索引记录UpdateResponse response = client.deleteById("5");// 4.提交,关闭连接client.commit();client.close();System.out.println("完成");}}

批量删除(以ID)

public class Test01 {final static String baseUrl = "http://localhost:8081/solr/core_01";@Testpublic void addIndex() throws IOException, SolrServerException {// 1.建立连接HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();List<String> list = new ArrayList<String>();list.add("111");list.add("6");// 以id删除索引记录UpdateResponse response = client.deleteById(list);// 4.提交,关闭连接client.commit();client.close();System.out.println("完成");}}

以条件删除(query)

solrj支持以查询的方式删除符合条件的索引记录

public class Test01 {final static String baseUrl = "http://localhost:8081/solr/core_01";@Testpublic void addIndex() throws IOException, SolrServerException {// 1.建立连接HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();List<String> list = new ArrayList<String>();list.add("111");list.add("6");// 查询匹配分类类目“毛衣”UpdateResponse response = client.deleteByQuery("prod_category_name:毛衣");// 4.提交,关闭连接client.commit();client.close();System.out.println("完成");}}

删除所有

public class Test01 {final static String baseUrl = "http://localhost:8081/solr/core_01";@Testpublic void addIndex() throws IOException, SolrServerException {// 1.建立连接HttpSolrClient client = new HttpSolrClient.Builder(baseUrl).build();List<String> list = new ArrayList<String>();list.add("111");list.add("6");// 删除所有UpdateResponse response = client.deleteByQuery("*:*");// 4.提交,关闭连接client.commit();client.close();System.out.println("完成");}}

3.5.查询

查询单个

@Test
public void testQuery() throws Exception{//1.创建连接HttpSolrClient solrServer = new HttpSolrClient.Builder("http://localhost:8983/solr/zym").build();//2.创建查询语句SolrQuery query = new SolrQuery();//3.设置查询条件query.set("q", "id:5");//4.执行查询QueryResponse queryResponse = solrServer.query(query);//5.取文档列表(public class SolrDocumentList extends ArrayList<SolrDocument>)SolrDocumentList documentList = queryResponse.getResults();for (SolrDocument solrDocument : documentList) {System.out.println("id:"+solrDocument.get("id")+" ");System.out.println("标题:"+solrDocument.get("product_title")+" ");System.out.println("卖点:"+solrDocument.get("product_sell_point")+" ");}
}

条件查询

@Test
public void testQueryByCon() throws Exception{//1.创建连接HttpSolrClient solrServer = new HttpSolrClient.Builder("http://localhost:8983/solr/zym").build();//2.创建查询语句SolrQuery query = new SolrQuery();//3.设置查询条件query.set("q", "*款");//设置查询关键字query.setSort("id", SolrQuery.ORDER.desc);//按照id降序排列query.setStart(0);query.setRows(5);//分页条件query.set("df", "product_sell_point");//默认在商品卖点域进行查询//4、执行查询QueryResponse queryResponse = solrServer.query(query);//5.获取文档列表SolrDocumentList documentList = queryResponse.getResults();System.out.println("总记录数:" + documentList.getNumFound());for (SolrDocument solrDocument : documentList) {System.out.println("id:"+solrDocument.get("id")+" ");System.out.println("标题:"+solrDocument.get("product_title")+" ");System.out.println("卖点:"+solrDocument.get("product_sell_point")+" ");}
}

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

相关文章

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;分类是预测…

逻辑斯蒂回归算法

目录 逻辑斯蒂分布 二元逻辑斯蒂回归模型 二元逻辑斯蒂回归的损失函数及优化方法 二元逻辑斯蒂回归的正则化 多元逻辑斯蒂回归 逻辑斯蒂回归小结 LR的优点 LR的缺点 LR将连续特征离散化的原因 逻辑回归和线性回归的区别和联系 LR和SVM的关系 scikit-learn 逻辑回归…

逻辑斯蒂回归以及它的梯度下降法

文章目录 前言逻辑斯蒂分布 模型二项逻辑斯蒂回归模型多项逻辑斯蒂回归模型 策略算法如何求对数似然函数的最大值梯度下降法算法思想推导公式 注意 前言 预测任务分为&#xff1a; 回归问题&#xff1a;输入、输出变量为连续变量。分类问题&#xff1a;输出变量为有限个离散变…

逻辑斯蒂回归分类算法

逻辑斯蒂回归分类算法 首先来看一个线性回归来进行分类的问题&#xff1a; 怎样判断肿瘤是否恶性&#xff1f; 很明显线性回归用于分类问题无法处理边界点的位置。 同时&#xff0c;线性回归健壮性不够&#xff0c;一旦有噪声&#xff0c;立刻“投降” 使用逻辑斯蒂回归 ——…

逻辑斯蒂回归(二分类算法)理论+Python代码实现

逻辑斯蒂回归&#xff08;二分类算法&#xff09;理论Python代码实现 文章目录 逻辑斯蒂回归&#xff08;二分类算法&#xff09;理论Python代码实现一、理论基础&#xff08;一&#xff09; 基于 Logistic 回归和 Sigmoid 函数的分类&#xff08;二&#xff09; 模型训练与代价…

逻辑斯蒂回归 逻辑回归_逻辑回归简介

逻辑斯蒂回归 逻辑回归 Logistic regression is a classification algorithm, which is pretty popular in some communities especially in the field of biostatistics, bioinformatics and credit scoring. It’s used to assign observations a discrete set of classes(ta…

Logistic Regression 逻辑斯蒂回归

文章目录 5、Logistic Regression 逻辑斯蒂回归5.1 回归任务5.1.1 MNIST Dataset5.1.2 CIFAR-10 Dataset 5.2 Regression vs Classification 回归 vs 分类5.3 Sigmoid functions5.3.1 Logistic Function [0, 1]5.3.2 Other Functions [-1, 1] 5.4 Model 模型5.5.1 torch.sigmoi…

机器学习-逻辑斯蒂回归(Logistic Regression)

注&#xff1a;内容转自https://github.com/NLP-LOVE/ML-NLP/tree/master/Machine%20Learning&#xff0c;略有修改。 目录 逻辑 1. 什么是逻辑斯蒂回归 2. 什么是Sigmoid函数 3. 损失函数是什么 4.可以进行多分类吗&#xff1f; 5.逻辑斯蒂回归有什么优缺点 6. 逻辑斯…

逻辑斯蒂回归

一&#xff1a;二元逻辑回归模型 二项逻辑斯蒂回归模型是如下的条件概率分布&#xff1a; 其中&#xff0c;Y为输出&#xff0c;w称为权值向量&#xff0c;b称为偏置。逻辑斯蒂回归比较两个条件概率值的大小&#xff0c;将实例x分到概率值较大的那一类。 一个事件的几率是指该…

【笔记】逻辑斯蒂回归

一、逻辑斯蒂回归概述 &#xff08;1&#xff09;逻辑斯蒂回归的目的&#xff1a;分类。 逻辑斯蒂回归常常解决分类问题&#xff0c;特别是二分类问题。 &#xff08;2&#xff09;逻辑斯蒂回归的过程&#xff1a;回归。 计算结果是0~1之间的连续值&#xff0c;代表发生的可…

逻辑斯蒂回归java_逻辑斯蒂回归模型

http://blog.csdn.net/hechenghai/article/details/46817031 主要参照统计学习方法、机器学习实战来学习。下文作为参考。 第一节中说了&#xff0c;logistic 回归和线性回归的区别是&#xff1a;线性回归是根据样本X各个维度的Xi的线性叠加(线性叠加的权重系数wi就是模型的参数…