首页 > 编程语言 >在java中使用solrj对solr进行CRUD

在java中使用solrj对solr进行CRUD

时间:2023-01-06 14:32:15浏览次数:55  
标签:java solrj doc CRUD 查询 import org solr solrQuery


如果想要知道如何安装solr,集成IKAnalyzer中文分词器,批量导入数据库数据,java使用参照以下本博主博文:

安装solr


集成IKAnalyzer中文分词器


solr使用浏览器批量导入数据库中数据


solr在java中的案例


github代码:

​https://github.com/mx342/luceneDemo​

在java中使用solrj对solr进行CRUD_solr复杂查询

1.必要的pom,本文使用的是springboot做案例

包含核心包solrj,http链接的包等,springboot自带日志

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.3</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<type>pom</type>
</dependency>

<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>wstx-asl</artifactId>
<version>3.2.7</version>
</dependency>

<dependency>
<groupId>org.noggit</groupId>
<artifactId>noggit</artifactId>
<version>0.5</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

2.测试代码

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
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 org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SolrApplicationTests {


@Value("${spring.data.solr.host}")
private String solrHost = "";

/**
* 作者 ZYL
* 功能描述 : 生成索引
* 日期 2019/5/1 17:30
* 参数
* 返回值 void
*/
@Test
public void testIndexCreate() throws Exception {
//连接solr服务端
SolrServer solrServer = new HttpSolrServer(solrHost);

SolrInputDocument doc = new SolrInputDocument();
//域要先定义后使用,必须要要注意要有id主键域
//solr中没有专用的修改方法,会根据id进行查找,如果找到了,删除原来的,根据新的进行查找
doc.addField("id","a001");
doc.addField("product_name","台灯111");
doc.addField("product_price","12.5");



solrServer.add(doc);
solrServer.commit();
}
/**
* 作者 ZYL
* 功能描述 : 删除操作
* 日期 2019/5/1 17:44
* 参数
* 返回值 void
*/
@Test
public void testIndexDel() throws Exception{
SolrServer solrServer = new HttpSolrServer(solrHost);

//根据主键id进行删除
solrServer.deleteById("a001");
// solrServer.deleteByQuery("*:*");

solrServer.commit();
}
/**
* 作者 ZYL
* 功能描述 : 简单查询
* 日期 2019/5/1 17:31
* 参数 null
* 返回值
*/
@Test
public void tesIndexSearch1() throws Exception{
SolrServer solrServer = new HttpSolrServer(solrHost);

//创建solr的查询条件对象
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("product_name:台灯");

//查询并获取查询响应对象
QueryResponse queryResponse = solrServer.query(solrQuery);
//从查询响应中获取查询结果集对象
SolrDocumentList results = queryResponse.getResults();
//遍历查询结果集
//打印查询的一共的多少条
System.err.println("总查询条数:" + results.getNumFound());
for (SolrDocument doc : results) {
System.err.println("==============" + doc.get("id"));
System.err.println("==============" + doc.get("product_name"));
System.err.println("==============" + doc.get("product_price"));
System.err.println("---------------------------------------------------");
}
solrServer.commit();
}
/**
* 作者 ZYL
* 功能描述 : 复杂查询
* 日期 2019/5/1 19:34
* 参数
* 返回值 void
*/
@Test
public void tesIndexSearch2() throws Exception {
SolrServer solrServer = new HttpSolrServer(solrHost);

//创建solr的查询条件对象
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("台灯");
//设置默认搜索与
solrQuery.set("df","product_keywords");
//设置过滤条件
solrQuery.addFilterQuery("product_price:[1 TO 100]");
//设置排序,降序
solrQuery.setSort("product_price", SolrQuery.ORDER.desc);
//设置分页
solrQuery.setStart(0);
//设置查询多少条
solrQuery.setRows(10);
//设置高亮hl
solrQuery.setHighlight(true);
//设置高亮显示的域
String highlightingField = "product_name";
solrQuery.addHighlightField(highlightingField);
//设置高亮前缀
solrQuery.setHighlightSimplePre("<span style=\"color:red\" >");
//设置高亮后缀
solrQuery.setHighlightSimplePost("</span>");

//=========================查询并获取查询响应对象=======================
QueryResponse queryResponse = solrServer.query(solrQuery);
//从查询响应中获取查询结果集对象
SolrDocumentList results = queryResponse.getResults();
//遍历查询结果集
//打印查询的一共的多少条
System.err.println("总查询条数:" + results.getNumFound());
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
for (SolrDocument doc : results) {
System.err.println("==============" + doc.get("id"));
Map<String, List<String>> highlightingById = highlighting.get(doc.get("id"));
if(highlightingById.containsKey(highlightingField)){
List<String> msgList = highlightingById.get(highlightingField);
for (String s : msgList) {
System.err.println("==============highlighting:" + s);
}
}
System.err.println("==============" + doc.get("product_name"));
System.err.println("==============" + doc.get("product_price"));
System.err.println("---------------------------------------------------");
}
}
}

 

 


标签:java,solrj,doc,CRUD,查询,import,org,solr,solrQuery
From: https://blog.51cto.com/u_15932265/5993550

相关文章

  • Java中在指定范围内生成整型、长整型、双精度随机数流
    场景Java8新特性-Stream对集合进行操作的常用API:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/126070657如果希望在指定范围内生成整型、长整型或双精度......
  • JAVA的FOR 语句
    快捷键 输入100.for等于输入for(inti=0;i<100;i++){}publicclassForDemo{publicstaticvoidmain(String[]......
  • Java 获取本机IP地址
    文章目录​​前言​​​​一、规则​​​​二、获取​​​​1.使用​​​​2.工具类​​前言在Java中如何准确的获取到本机IP地址呢?网上大部分的做法是​​InetAddress.getL......
  • 来自菜鸡Java工程师的日积月累
    项目简介目前​​Treasure​​​是个微服务架构的纯后端项目,前端部分后期进行;同时​​Treasure​​是一个来自菜鸡Java工程师本着对技术的积累与个人的成长的开源项目。主......
  • Java String为什么被设计为final的
    文章目录String被设计为不可变是因为String对象是缓存在字符串池中的,因此这些缓存的字符串是可以被多个客户端访问的,如果一个客户端的访问影响了别的客户端的行为,这样就存在......
  • Java 集合判空优化
    文章目录//反例LinkedList<Object>collection=newLinkedList<>();if(collection.size()==0){System.out.println("collectionisempty.");}//正例LinkedList<O......
  • Java 中map 遍历优化
    文章目录packagedemo.map;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.stream.Collectors;/***<p>*Map遍历优化*</P>......
  • Java嵌套if else优化
    文章目录​​1.传统实现​​​​2.策略模式+工厂模式+单例模式优化​​​​2.1策略接口​​​​2.2不同策略实现类​​​​2.2.1处理策略一​​​​2.2.2处理策略二​......
  • Java8 函数式接口
    文章目录​​一、特点​​​​二、函数式接口中允许定义的方法类型​​​​三、四大内置函数式接口​​​​1.`Consumer`消费​​​​1.1示例​​​​2.`Supplier`供应......
  • java实现微信公众号消息推送
    1.打开[(https://push.ggt1024.com)],微信扫码登录2.点击java生成代码复制3.在pom.xml中添加依赖<dependency><groupId>com.squareup.okhttp3</groupId>......