SolrJ的基本操作

简介: SolrJ的基本操作

引言

 

我们如果在项目中使用solr肯定要用java语言来操作它,而这个操作的入口就是SolrJ,下面就介绍一下如何使用SolrJ来操作Solr

 

第一步:引入jar包


20170803010100991.png

上面这些jar包来源与下面两个路径:


20170803010253659.png


第二部分:



20170803010404574.png基本操作的代码


package com.itheima.solr;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
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;
/**
 * @author zhenghao
 * @date 创建时间:2017年8月2日 上午11:31:09
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class SolrManager {
  @Test
  public void testAdd() throws Exception {
    String baseURLString = "http://localhost:8080/solr";
    // 单机版
    SolrServer solrServer = new HttpSolrServer(baseURLString);
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "haha");
    doc.setField("name", "范冰冰");
    // 添加
    solrServer.add(doc);
    solrServer.commit();
  }
  // 删除
  @Test
  public void testDelete() throws Exception {
    String baseURLString = "http://localhost:8080/solr/collection2";
    // 单机版
    SolrServer solrServer = new HttpSolrServer(baseURLString);
    // 删除
    solrServer.deleteByQuery("*:*", 1000);
  }
  // update
  @Test
  public void testUpdate() throws Exception {
    String baseURLString = "http://localhost:8080/solr/collection2";
    // 单机版
    SolrServer solrServer = new HttpSolrServer(baseURLString);
    // 更新
    // 与添加相同 只要id相同 则为更新 id不同则为添加
  }
  // 查询
  @Test
  public void testSearch() throws Exception {
    String baseURLString = "http://localhost:8080/solr";
    // 单机版
    SolrServer solrServer = new HttpSolrServer(baseURLString);
    // 查询关键词
    SolrQuery solrQuery = new SolrQuery();
    // 关键词
    // solrQuery.set("q", "*:*");
    solrQuery.setQuery("item_title:三星 Note II (N7100) 云石白 联通3G手机");
    // solrQuery.set("fq", "product_catalog_name:幽默杂货");
    // 价格在10元以下
    solrQuery.set("fq", "item_price:[* TO 100000]");
    // 价格排序
    solrQuery.addSort("item_price", ORDER.desc);
    // 分页
    solrQuery.setStart(0);// 从第几页开始
    solrQuery.setRows(5);// 每页显示几条
    // // 默认域
    solrQuery.set("df", "item_title");
    // 只查询指定域
    solrQuery.set("fl", "id,item_title,item_sell_point,item_price,item_num,item_image");
    // // 高亮
    // // 打开高亮开关
    solrQuery.setHighlight(true);
    // 指定高亮域
    solrQuery.addHighlightField("item_title");
    // 前缀
    solrQuery.setHighlightSimplePre("<span style='color:red'>");
    // 后缀
    solrQuery.setHighlightSimplePost("</span>");
    // 执行查询
    QueryResponse response = solrServer.query(solrQuery);
    // 文档结果集
    SolrDocumentList docs = response.getResults();
    // 取出高亮的文档对象
    Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
    // 解释 高亮对象的文档结构 大Map 套小 map 小map 套 list
    // Map K id V Map
    // Map K 域名 V list
    // List list.get(0)
    //
    // 总条数
    long numFound = docs.getNumFound();
    System.out.println(numFound);
    for (SolrDocument doc : docs) {
      System.out.println(doc.get("item_price"));
      System.out.println(doc.get("item_num"));
      Map<String, List<String>> map = highlighting.get(doc.get("id"));
      //
      List<String> list = map.get("item_title");
      //
      System.out.println(list.get(0));
    }
  }
}

如果我们和框架进行整合以后,solrServer的创建则交由spring容器来管理:

 <!-- 配置SOlrJ -->
        <bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
          <constructor-arg value="http://localhost:8080/solr/collection1"/>
        </bean>

小结

 

以上就是solrJ的一些基本操作,这些就是我们在项目中常用的一些代码,尤其是查询的操作,在查询的时候比较不好写的就是查询条件的设置,其余的都比较容易!

目录
相关文章
|
Prometheus 监控 Cloud Native
[基础服务] Grafana/Prometheus/node-exporter 的安装和部署
[基础服务] Grafana/Prometheus/node-exporter 的安装和部署
1284 0
[基础服务] Grafana/Prometheus/node-exporter 的安装和部署
|
JSON JavaScript 数据格式
Typora+PicGo+SMMS、github、gitee实现Typora图片上传到图床
Typora+PicGo+SMMS、github、gitee实现Typora图片上传到图床
381 0
Typora+PicGo+SMMS、github、gitee实现Typora图片上传到图床
|
消息中间件 存储 JSON
区块链链游系统开发方案解析及(成熟代码)
区块链链游系统开发方案解析及(成熟代码)
247 0
|
Unix Linux
Symantec Backup Exec 报"Access denied to directory xxx" Error Code E0008488
使用Symantec Backup Exec将几台Linux服务器上的RMAN备份收带时,偶尔会遇到作业备份失败的情况,检查Job History,就会发现有“Access denied to directory u05/backup/backupsets" 这类错误,当然实际路径为/u05/backup/backupsets.
2684 0
|
监控 数据可视化 数据库
ELK统一日志系统的应用
收集和分析日志是应用开发中至关重要的一环,互联网大规模、分布式的特性决定了日志的源头越来越分散,产生的速度越来越快。在规模化场景下,grep、awk 无法快速发挥作用,我们需要一种高效、灵活的日志分析方式,可以给故障处理,问题定位提供更好的支持。
7126 0
|
8天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
6天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
334 130
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
19天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1331 8