elasticsearch简介
elasticsearch是什么?
Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。——百度文库
概括一下,就是一个采用restful web的搜索引擎,支持分布式。
elastic search几个基本概念
索引:类似关系型数据库中的数据库
类型:类似关系型数据库中的表
cluster:集成,一群服务器。
shared:分片
replicas:索引副本
elastic search几个基本操作
- put:存储和修改信息
- delete:删除信息
- head:检索信息是否存在
- get:获取存储的信息
注:有请求体,格式为json。——感觉写了好多废话。
springboot 整合elastic search
第一步:创建一个spring boot空项目
第二步:引入如下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
第三步:创建如下目录结构
给出配置文件application.yml
spring:
elasticsearch:
rest:
uris: http://localhost:9200
第四步:给出实体类和数据访问层类
UserRepository.java
package com.lm.es.dao;
import com.lm.es.entities.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends ElasticsearchRepository<User, Integer> {
}
# 强烈建议看我写的注释
User.java
package com.lm.es.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
/**
* @Document:在类级别应用,以指示该类是映射到数据库的候选对象。最重要的属性是:
* indexName:用于存储此实体的索引的名称。它可以包含SpEL模板表达式,例如 "log-#{T(java.time.LocalDate).now().toString()}"
* type:映射类型。如果未设置,则使用小写的类的简单名称。(从版本4.0开始不推荐使用)
* shards:索引的分片数。
* replicas:索引的副本数。
* refreshIntervall:索引的刷新间隔。用于索引创建。默认值为“ 1s”。
* indexStoreType:索引的索引存储类型。用于索引创建。默认值为“ fs”。
* createIndex:标记是否在存储库引导中创建索引。默认值为true。请参见使用相应的映射自动创建索引
* versionType:版本管理的配置。默认值为EXTERNAL。
* @Id:在属性上使用,标识id。
* @Transient:默认情况下,存储或检索文档时,所有字段都映射到文档,此注释不包括该字段。
* @PersistenceConstructor:标记从数据库实例化对象时要使用的给定构造函数,甚至是受保护的程序包。构造函数自变量按名称映射到检索到的Document中的键值。
* @Field:在字段级别应用并定义字段的属性,大多数属性映射到各自的Elasticsearch映射定义(以下列表不完整,请查看注释Javadoc以获得完整参考):
* name:字段名称,因为它将在Elasticsearch文档中表示,如果未设置,则使用Java字段名称。
* type:字段类型,可以是以下类型之一:文本,关键字,长整数,整数,短整数,字节,双精度,浮点型,Half_Float,Scaled_Float,日期,
* Date_Nanos,布尔值,二进制,Integer_Range,Float_Range,Long_Range,Double_Range,Date_Range,Ip_ ,嵌套,Ip,TokenCount,
* 过滤器,展平,Search_As_You_Type。请参阅Elasticsearch映射类型
* format和日期类型的pattern定义。
* store:标记原始字段值是否应存储在Elasticsearch中,默认值为false。
* analyzer,searchAnalyzer,normalizer用于指定自定义分析和正规化。
* @GeoPoint:将字段标记为geo_point数据类型。如果字段是GeoPoint类的实例,则可以省略。
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "test", indexStoreType = "user")
public class User {
@Id
private Integer id;
@Field
private String name;
@Field
private String password;
@Field
private String detail;
}
第五步:编写测试类
package com.lm.es;
import com.lm.es.dao.UserRepository;
import com.lm.es.entities.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import java.util.Optional;
@SpringBootTest
class SpringbootElasticsearchApplicationTests {
@Autowired
UserRepository userRepository;
@Test
public void test1(){
User user = new User(1, "lm", "pass", "content");
userRepository.save(user);
}
@Test
public void test2(){
Optional<User> user = userRepository.findById(1);
System.out.println(user);
User user1 = user.get();
System.out.println(user1);
}
@Test
public void test3(){
}
}
读者自测
如果想要编写自己的搜索方法在repository中,只需要在repository类的方法上添加@query,然后给@query中添加匹配规则就行了。
例如:
package com.test.es.estest.dao;
import com.test.es.estest.entity.DocBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ElasticRepository extends ElasticsearchRepository<DocBean, Long> {
//默认的注释
//@Query("{\"bool\" : {\"must\" : {\"field\" : {\"content\" : \"?\"}}}}")
Page<DocBean> findByContent(String content, Pageable pageable);
@Query("{\"bool\" : {\"must\" : {\"field\" : {\"firstCode.keyword\" : \"?\"}}}}")
Page<DocBean> findByFirstCode(String firstCode, Pageable pageable);
@Query("{\"bool\" : {\"must\" : {\"field\" : {\"secordCode.keyword\" : \"?\"}}}}")
Page<DocBean> findBySecordCode(String secordCode, Pageable pageable);
}
spring boot整合elasticsearch官方文档
spring boot整合elasticsearch项目地址