Solr与Spring Boot集成 - Viithiisys

栏目: Java · 发布时间: 6年前

内容简介:在本教程中,我将向您展示如何通过将Solr与Spring Boot集成来改进搜索。Apache Solr既是搜索引擎又是支持SQL的分布式文档数据库。Solr核心是个搜索引擎,但远不止于此。它是一个具有事务支持的NoSQL数据库。

在本教程中,我将向您展示如何通过将Solr与Spring Boot集成来改进搜索。 Spring-data-solr 是Spring Data的扩展,用于将Solr与Spring Boot starter集成。

什么是Solr

Apache Solr既是搜索引擎又是支持 SQL 的分布式文档数据库。Solr核心是个搜索引擎,但远不止于此。它是一个具有事务支持的NoSQL数据库。

步骤:

  • 配置Solr
  • 创建Solr文档
  • 创建Solr存储库

注意: github项目中 提供了Solr REST API的完整示例。

配置Solr

你可以 在这里 下载Solr 

基本Solr命令:

Directory solr/solr-5.3.1
Start Solr: ./bin/solr start
Stop Solr: ./bin/solr stop
Check Logs: tail -f server/logs/solr.log
Start Solr on a different port: ./bin/solr start -p 2000

运行Solr:

cd solr/solr-5.3.1
./bin/solr start

创建Core:

./bin/solr create -c user_core

与Spring Boot Project集成

添加依赖项Maven:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-solr</artifactId>
  </dependency>

Gradle:

implementation('org.springframework.boot:spring-boot-starter-data-solr')

创建Solr文档:

Solr文档是使用@SolrDocument注释创建的,其核心是在其中定义的。

@Indexed注释用于字段以使其可搜索。

<b>import</b> org.springframework.data.annotation.Id;
<b>import</b> org.springframework.data.solr.core.mapping.Indexed;
<b>import</b> org.springframework.data.solr.core.mapping.SolrDocument;

<font><i>/**
 * @author anuragdhunna
 */</i></font><font>

@SolrDocument(solrCoreName = </font><font>"user_core"</font><font>)
<b>public</b> <b>class</b> UserDoc {

    @Id
    @Indexed
    <b>private</b> String id;

    @Indexed(name = </font><font>"username"</font><font>, type = </font><font>"string"</font><font>)
    <b>private</b> String username;

    @Indexed(name = </font><font>"email"</font><font>, type = </font><font>"string"</font><font>)
    <b>private</b> String email;

    @Indexed(name = </font><font>"phone_number"</font><font>, type = </font><font>"string"</font><font>)
    <b>private</b> String phoneNumber;

    </font><font><i>// Getter Setters </i></font><font>
}
</font>

创建Solr仓储:

<b>import</b> com.anuragdhunna.solrIntegration.documents.UserDoc;
<b>import</b> org.springframework.beans.factory.annotation.Qualifier;
<b>import</b> org.springframework.data.solr.repository.Query;
<b>import</b> org.springframework.data.solr.repository.SolrCrudRepository;
<b>import</b> org.springframework.stereotype.Repository;

<b>import</b> java.util.List;

<font><i>/**
 * @author anuragdhunna
 */</i></font><font>

@Repository
@Qualifier(</font><font>"userSolrRepo"</font><font>)
<b>public</b> <b>interface</b> UserSolrRepo <b>extends</b> SolrCrudRepository<UserDoc, String> {

    @Query(value = </font><font>"*:*"</font><font>)
    List<UserDoc> getUsers();

}
</font>

注意:如果Solr在不同的端口上运行(8983),请在application.properties文件中添加一个属性:

spring.data.solr.host = http:<font><i>//127.0.0.1:3000/solr</i></font><font>
</font>

对于完整的API参考,您可以在Github上检查项目。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pro CSS and HTML Design Patterns

Pro CSS and HTML Design Patterns

Michael Bowers / Apress / April 23, 2007 / $44.99

Design patterns have been used with great success in software programming. They improve productivity, creativity, and efficiency in web design and development, and they reduce code bloat and complexit......一起来看看 《Pro CSS and HTML Design Patterns》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具