5.6 spring集成solr

  Spring集成solr不能使用spring-integration框架。只能使用spring-data框架。所以也是分几个步骤:一、配置solr;二、配好maven;三、写好Java代码。

一 配置solr

  我是写个demo,所以我的solr配置得特别简单,只有两个字段。

<?xml version="1.0" encoding="UTF-8" ?>

<schema name="default-config" version="1.6">
    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
    <field name="name" type="string" indexed="true" stored="true" required="true" multiValued="false" />
	<!-- The StrField type is not analyzed, but indexed/stored verbatim. -->
    <fieldType name="string" class="solr.StrField" sortMissingLast="true" docValues="true" />
    <fieldType name="strings" class="solr.StrField" sortMissingLast="true" multiValued="true" docValues="true" />
</schema>

二 maven依赖

	<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-solr -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-solr</artifactId>
            <version>4.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.14</version>
        </dependency>

    </dependencies>

三 Java代码

  首先写好实体类

package com.youngthing.spring.solr;

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

public class Student {
    @Field
    private String id;
    @Field
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

  再简单写个main函数就完成了这个集成solr的demo了。

package com.youngthing.spring.solr;

import com.fasterxml.jackson.databind.json.JsonMapper;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.server.SolrClientFactory;
import org.springframework.data.solr.server.support.HttpSolrClientFactory;

import java.io.IOException;

@Configuration
public class SolrDemo {

    @Bean
    public SolrClient solrClient() {
        return new HttpSolrClient.Builder().withBaseSolrUrl("http://localhost:8984/solr").build();
    }

    @Bean
    public SolrClientFactory solrClientFactory(SolrClient solrClient) {
        return new HttpSolrClientFactory(solrClient);
    }

    @Bean
    public SolrTemplate solrTemplate(SolrClientFactory solrClientFactory) {
        return new SolrTemplate(solrClientFactory);
    }

    public static void main(String[] args) throws IOException {
        final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SolrDemo.class);
        final SolrTemplate template = ctx.getBean(SolrTemplate.class);
        Student[] students = JsonMapper.builder().build().createParser("[{\"id\":\"1\",\"name\":\"David\"}," +
                "{\"id\":\"2\",\"name\":\"Paul\"}]").readValueAs(Student[].class);
        for (Student s : students) {
            System.out.println(s);
            template.saveBean("student", s);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醒过来摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值