solr6.6初探之solrj

简介: 一. solrj简介:   solrj可以使Java应用程序很方便的访问与操作solr。solrj有几个核心类,分别为:1.SolrClient 2.SolrRequests 3.SolrQuerys 4.

一. solrj简介:

  solrj可以使Java应用程序很方便的访问与操作solr。solrj有几个核心类,分别为:1.SolrClient 2.SolrRequests 3.SolrQuerys 4.SolrReponses

    tips:该jar包可以在${solr.home}/dist/solrj-lib 找到

  gralde配置:

  

buildscript {
    ext {
        springBootVersion = '1.5.8.RELEASE'
    }
    repositories {
        maven { url = "http://maven.aliyun.com/nexus/content/groups/public" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.bdqn.lyrk.study'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
    maven { url = "http://maven.aliyun.com/nexus/content/groups/public" }
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-solr')
    compile('org.springframework.boot:spring-boot-starter-web')
    compileOnly('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
View Code

    maven配置:

  

   <dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-solrj</artifactId>
    <version>x.y.z</version>
  </dependency>
View Code

 

二 核心类介绍:

  1. SolrClient (操作solr的核心类):

    由于SolrClient是抽象的,因此要连接solr的远程服务需要创建HttpSolrClient 或者 CloudSolrClient 对象,两者都是以Http请求与solr通信,只不过前者需要一个明确的solr地址信息,后者是基于zookeeper的地址并对solrCloud进行操作,在这里是基于前者的教程

        创建代码:

String urlString = "http://localhost:8983/solr/techproducts";
SolrClient solr = new HttpSolrClient.Builder(urlString).build();

  solrClient常用方法:

public UpdateResponse add(String collection, SolrInputDocument doc) throws SolrServerException, IOException 

向solr中添加一条数据

参数:

collection:在对应的core中添加数据

doc:该参数封装了具体要添加的值

public UpdateResponse addBean(String collection,Object obj) throws IOException, SolrServerException 
将对应的bean添加至文档索引当中
public UpdateResponse commit(String collection) throws SolrServerException, IOException
提交本次操作的数据
public UpdateResponse rollback(String collection) throws SolrServerException, IOException
回滚本次操作的数据
public UpdateResponse deleteById(String collection, String id) throws SolrServerException, IOException
根据唯一索引删除索引信息
public UpdateResponse deleteByQuery(String collection, String query) throws SolrServerException, IOException
根据查询删除索引里的文档信息
public SolrDocument getById(String id, SolrParams params) throws SolrServerException, IOException
根据ID进行查询
public QueryResponse query(String collection, SolrParams params, METHOD method) throws SolrServerException, IOException 

查询方法

参数:

collection:查询的core

solrParams:查询参数对象

method:Http请求方式

 

2.SolrDocumentBase

  该类是抽象类,并实现Map接口,子类包含SolrDocument和SolrInputDocument。该类主要作用是代表了solr索引中的一条数据信息,对象中的field值必须在magage-schema中定义。通常需要操作solr索引字段时用到SolrInputDoucument

该类常用方法:

void addField(String name, Object value)
向solr中索引中添加一个Field
Object getFieldValue(String name)
 根据field获取对应的第一个值或者一组值
 
Collection<String> getFieldNames()
 获取所有的Fields信息

  

 

 

 

 

3.SolrParams

该类是抽象类,主要用于封装与solr请求时所传的参数信息。该类最常用的子类为SolrQuery, SolrQuery类提供了查询所需的分组,高亮,排序等方法

3.1 SolrInputDocument:

void addField(String name, Object value)
在索引添加一条Field对应的值
void setField(String name, Object value)
在索引中更新一条Field对应的值

 

 

 

3.2  SolrQuery:

ModifiableSolrParams set( String name, String ... val )
设置对应的查询参数,具体见下方实例
SolrQuery addHighlightField(String f)
添加高亮显示字段
 SolrQuery addFilterQuery(String ... fq)
 添加过滤字段
 SolrQuery addSort(String field, ORDER order)
 添加排序字段

 

 

 

 

 

 

 

4.SolrResponse

    该类是抽象类,主要用于solr对用户请求之后做出的响应。

  

 

三 简单用例:

实体类:

package com.bdqn.lyrk.study.springboot.entity;

import lombok.Data;
import org.apache.solr.client.solrj.beans.Field;

/**
 * @author chen.nie
 */
@Data
public class StudentEntity {
    @Field("id" ) //对应solr中索引的字段
    private String id;
    @Field("userName")
    private String userName;
    @Field("classId")
    private String classId;

}
View Code

dao对应的实现类:

package com.bdqn.lyrk.study.springboot.dao.solr;

import com.bdqn.lyrk.study.springboot.dao.StudentDao;
import com.bdqn.lyrk.study.springboot.entity.StudentEntity;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.CommonParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

/**
 * dao针对于solr的实现
 * @author chen.nie
 */
@Repository(value = "solrStudentDao")
public class SolrStudentDaoImpl implements StudentDao {

    public static final String COLLECTION = "machines";

    @Autowired
    private SolrClient solrClient;


    @Override
    public int save(StudentEntity studentEntity) throws Exception {
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        solrInputDocument.addField("id", studentEntity.getId());
        solrInputDocument.addField("userName", studentEntity.getUserName());
        solrClient.add(COLLECTION, solrInputDocument);
        UpdateResponse updateResponse = solrClient.commit(COLLECTION);
        return updateResponse.getStatus();
    }

    @Override
    public List<StudentEntity> query(StudentEntity studentEntity) throws Exception {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set(CommonParams.Q, "userName:" + studentEntity.getUserName());
        QueryResponse queryResponse = solrClient.query(COLLECTION, solrQuery);
        List<StudentEntity> list = queryResponse.getBeans(StudentEntity.class);
        return list;
    }

    @Override
    public int update(StudentEntity studentEntity) throws Exception {
        /*
         * 注意该类对solr对应字段的原子更新,必须设置对象对应的唯一值
         */
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        solrInputDocument.addField("id", studentEntity.getId());
        if (StringUtils.hasLength(studentEntity.getUserName())) {
            Map<String, Object> map = new HashMap<>(1);
            map.put("set", studentEntity.getUserName());
            solrInputDocument.addField("userName", map);
        }
        if (StringUtils.hasLength(studentEntity.getClassId())) {
            Map<String, Object> map = new HashMap<>(1);
            map.put("set", studentEntity.getClassId());
            solrInputDocument.addField("classId", map);
        }
        solrClient.add(COLLECTION, solrInputDocument);
        UpdateResponse updateResponse = solrClient.commit(COLLECTION);
        return updateResponse.getStatus();
    }
}
View Code

 注意:操作的field必须为存储(store="true"),否则更新的索引会覆盖前面的索引

目录
相关文章
|
机器学习/深度学习 编解码 语音技术
音频基础知识 2
音频基础知识
551 0
|
7月前
|
存储 人工智能 Serverless
一键解锁 AI 动画视频创作,赢好礼
短视频行业的快速增长使得内容创作的速度和质量成为竞争关键。传统动画故事制作复杂且昂贵,限制了创作者对市场热点的快速反应和创新实现。本方案通过 AI 生成剧本和动画,简化创作流程并降低技术门槛,使创作者能高效生产高质量作品,迅速适应市场需求。
224 11
|
6月前
|
数据采集 监控 数据挖掘
静态IP代理的应用场景及企业使用指南
静态IP代理提供固定IP地址,具备高稳定性和安全性,适用于跨境电商、社交媒体管理、SEO、网络数据采集、远程办公及爬虫分析等场景。企业通过选择可靠的供应商、配置网络设置并合理应用,可有效提升业务效率和安全性。例如,某电商公司利用静态住宅代理IP进行数据采集,成功分析竞争对手策略,实现销售额20%的增长。
190 1
|
9月前
|
机器学习/深度学习 存储 人工智能
【AI系统】自定义计算图 IR
本文介绍了模型转换的方法及流程,重点讲解了计算图的自定义方法和优化技术。通过IR(Intermediate Representation)将不同AI框架的模型转换为统一格式,实现跨平台高效部署。计算图由张量和算子构成,支持多种数据类型和内存排布格式,通过算子融合等优化技术提高模型性能。文章还详细说明了如何使用FlatBuffers定义计算图结构,包括张量、算子和网络模型的定义,为自定义神经网络提供了实践指南。
255 3
【AI系统】自定义计算图 IR
|
10月前
|
机器学习/深度学习 自然语言处理 语音技术
智能语音识别技术的现状与未来####
在这篇文章中,我们将深入探讨智能语音识别技术的发展历程、当前的应用现状以及未来的发展趋势。通过分析该技术在不同领域的应用情况,我们可以更好地理解其重要性和潜力。此外,文章还将讨论当前面临的主要挑战和解决方案,为读者提供一个全面的视角。 ####
|
10月前
|
机器学习/深度学习 人工智能 TensorFlow
探索深度学习与计算机视觉的融合:构建高效图像识别系统
探索深度学习与计算机视觉的融合:构建高效图像识别系统
243 0
|
C语言
C语言中的math库概述
C语言中的math库概述
449 1
|
存储 安全 5G
让你的相册变成私有云!Synology Photos 的公网访问功能指南
让你的相册变成私有云!Synology Photos 的公网访问功能指南
|
机器学习/深度学习 人工智能 自然语言处理
全新TextGrad框架:用GPT-4o作引擎,自动优化端到端任务
【7月更文挑战第15天】TextGrad框架利用GPT-4o自动优化AI系统,通过文本反馈调整组件性能,提升问答、编程任务和分子设计等领域的效果。在Proof QA中提升准确率至55%,LeetCode难题解决效率提高20%。虽依赖LLM质量且易用性有限,但展示了强大的潜力,尚处于研究阶段。[arXiv:2406.07496](https://arxiv.org/abs/2406.07496)**
272 1
FLBOOK、云展网、草料二维码3款电子画册制作软件深度对比
本文亲测国内主流的电子画册制作软件,为大家精心挑选了3款电子画册制作软件,以下将根据它们的功能特点、操作界面、应用范围以及价格区间进行深度分析,帮助你选择最适合自己需求的电子画册制作工具
FLBOOK、云展网、草料二维码3款电子画册制作软件深度对比

热门文章

最新文章