Solr文档学习--Solrj的使用

首先启动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
    @Field
    private String id;
    @Field
    private String[] title;
    @Field
    private Date[] time;
    @Field
    private String[] author;
    @Field
    private 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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Solr基于Lucene的全文搜索服务器。同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展并对查询性能进行了优化,并且提供了一个完善的功能管理界面,是一款非常优秀的全文搜索引擎 课程特点毕业后接触的第一个中间件就是Solr,在工作中用处广泛,为了便于大家快速掌握该技能,开始录制相关课程,该专栏特点如下:1.采用Solr最新版本视频录制,全网最新课程(Solr8.1于2019年5月16日发布)2.技能点全网最全,会结合工作经验,项目中用到的技能点都会有所涉及,更新章节比较全面3.适用范围广,从零基础到高级架构以及分布式集群都涵盖,适用初级、高级、项目实战等多个层次开发者4.多种维度辅助学习,采用独立solr粉丝群辅助教学,学员问题会及时得到解决,程序员突破圈 打卡制度,督促学员学习关注后再购买、 关注后再购买、 关注后再购买课程能得到什么1.快速学习到最新版本的全文检索技术,从视频、文章、圈子、粉丝交流等快速促进学习2.通过该技术,获得面试进阶指导3.结交人脉(庞大的粉丝群)..End初期学员100人,价格不会太高,也是为了帮助更多的开发者但是个人精力有限,所以限制条件如下1.求知欲强,有想向技术更深一层了解的2.乐于交流,喜欢探讨技术者3.学习惰性者慎入,购买后会督促大家学习,购买不是目的,学习到该技能才是该专栏的主要目的正式进入学习状态了吗,专栏群见。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值