elasticsearch安装整合springboot使用教程

elasticsearch安装配置kdown编辑器

Elasticsearch安装文档
1.官网下载
https://www.elastic.co/cn/downloads/elasticsearch
不推荐官网,可以用国内的华为云:
https://mirrors.huaweicloud.com/

注意版本的问题:
最新版的7.8.0 内置了jdk 11 版本,如果本地环境是1.8 不建议安装最新版本,启动会与本地的jdk冲突。

2.进行解压
tar -zxvf elasticsearch-7.7.1-linux-aarch64.tar.gz

3.root 用户无法启动,需要使用其他用户启动
1)建用户组: groupadd es
2)向组内添加用户 useradd -g es es
3)对新用户授权 chown -R es:es /Es/elasticsearch-7.7.1
4)授权成功
在这里插入图片描述

4.修改config下的elasticsearch.yml,

5.切换到新用户es启动: 前台启动:./elasticsearch 后台启动:./elasticsearch -d
在这里插入图片描述

6.测试是否启动成功:
在这里插入图片描述
7.安装遇到的问题总结
1)没有切换用户启动报错:
在这里插入图片描述
解决方法: es

2)启动时报错:max number of threads [3895] for user [elk] is too low, increase to at least

修改:vi /etc/security/limits.conf 修改为如下图:
在这里插入图片描述
注意:重启服务器生效
设置/etc/sysctl.conf
添加如下配置:vm.max_map_count=655360 并执行: sysctl -p

Elasticsearch启动内存不足解决方案:
修改 jvm空间分
cd到elasticsearch目录
vim config/jvm.options
修改:
-Xms2g
-Xmx2g

-Xms256m
-Xmx256m

3)其他遇到问题自行百度

Elasticsearch.yml
cluster.name: es-name #集群的名称,同一个集群该值必须设置成相同的
node.name: es-node1 #该节点的名字
node.master: true #该节点有机会成为master节点
node.data: true #该节点可以存储数据
path.data: /opt/elasticsearch-7.6.2/data # 数据的默认存放路径
path.logs: /opt/elasticsearch-7.6.2/logs # 日志的默认存放路径
network.bind_host: 0.0.0.0 #设置绑定的IP地址,可以是IPV4或者IPV6
network.publish_host: 192.168.1.129 #设置其他节点与该节点交互的IP地址
network.host: 192.168.1.129 #该参数用于同时设置bind_host和publish_host
transport.tcp.port: 9300 #设置节点之间交互的端口号
transport.tcp.compress: true #设置是否压缩tcp上交互传输的数据
http.port: 9200 #设置对外服务的http端口号
http.max_content_length: 100mb #设置http内容的最大大小

discovery.zen.ping_timeout: 120s #设置集群中自动发现其他节点时ping连接的超时时间
discovery.seed_hosts:

  • 192.168.1.130:9300
  • 192.168.1.131:9300 #设置集群中的Master节点的初始列表,可以通过这些节点来自动发现其他新加入集群的节点
    cluster.initial_master_nodes:
  • es-node1
  • es-node2
  • es-node3
    http.cors.enabled: true #跨域连接相关设置
    http.cors.allow-origin: “*” #跨域连接相关设置
    gateway.expected_nodes: 3
    gateway.expected_master_nodes: 3

spring-boot集成spring-data-elasticsearch
依赖
父依赖版本

org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE

es依赖

org.springframework.boot
spring-boot-starter-data-elasticsearch

application.yml
spring:
data:
elasticsearch:
cluster-name: elasticsearch-cluster
cluster-nodes: 182.61.19.23:9200
repositories:
enabled: true

configuration
/**

  • Factory method ‘elasticsearchClient’ threw exception; nested exception is java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8]
    解决此类报错
    */
    @SpringBootConfiguration
    public class ElasticSearchConfig {
    @PostConstruct
    void init() {
    System.setProperty(“es.set.netty.runtime.available.processors”, “false”);
    }
    }

文档
/**

  • @Document 作用在类,标记实体类为文档对象,一般有两个属性
  • indexName:对应索引库名称
  • type:对应在索引库中的类型
  • shards:分片数量,默认5
  • replicas:副本数量,默认1
  • @Id 作用在成员变量,标记一个字段作为id主键
  • @Field 作用在成员变量,标记为文档的字段,并指定字段映射属性:
  • type:字段类型,是枚举:FieldType,可以是text、long、short、date、integer、object等
  • text:存储数据时候,会自动分词,并生成索引
  • keyword:存储数据时候,不会分词建立索引
  • Numerical:数值类型,分两类
  • 基本数据类型:long、integer、short、byte、double、float、half_float
  • 浮点数的高精度类型:scaled_float
  • 需要指定一个精度因子,比如10或100。elasticsearch会把真实值乘以这个因子后存储,取出时再还原。
  • Date:日期类型
  • elasticsearch可以对日期格式化为字符串存储,但是建议我们存储为毫秒值,存储为long,节省空间。
  • index:是否索引,布尔类型,默认是true
  • store:是否存储,布尔类型,默认是false
  • analyzer:分词器名称,这里的ik_max_word即使用ik分词器

*/
@Data
@Document(indexName = “poms”, type = “content”)
public class ESDocument {
@Id
private String id;

@Field(analyzer = "ik_smart", searchAnalyzer = "ik_smart")
private String name;

private String projectId;

public ESDocument(String id, String name, String projectId) {
    this.id = id;
    this.name = name;
    this.projectId = projectId;
}

public ESDocument() {

}

}

@Repository
public interface DocumentSearchRepository extends ElasticsearchRepository<ESDocument,String> {
}

public interface DocumentSearchService {
void save(ESDocument esDocument);
void delete(String id);
void getById(String id);
void getByName(String name,String projectId);
}

@Service
public class DocumentSearchServiceImpl implements DocumentSearchService {
@Autowired
private DocumentSearchRepository documentSearchRepository;

/*@Resource
private ElasticsearchTemplate elasticsearchTemplate;*/

@Override
public void save(ESDocument esDocument) {
    ESDocument save = documentSearchRepository.save(esDocument);
    System.out.println(save.toString());
}

@Override
public void delete(String id) {
    documentSearchRepository.deleteById(id);
}

@Override
public void getById(String id) {
    ESDocument esDocument = documentSearchRepository.findById(id).orElse(new ESDocument());
    System.out.println(esDocument.toString());

}

@Override
public void getByName(String name, String projectId) {
   /* SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("name",name)).withQuery(matchQuery("projectId",projectId)).build();
    List<ESDocument> esDocuments = elasticsearchTemplate.queryForList(searchQuery, ESDocument.class);
    esDocuments.forEach(e-> System.out.println(e.toString()));*/
}

}

开始测试:

@SpringBootTest
class SpringBootEsApplicationTests {

@Autowired
private DocumentSearchService documentSearchServiceIml;

@Test
public void save() {
    documentSearchServiceIml.save(new ESDocument(UUID.randomUUID().toString(),"name1","1"));
}
@Test
public void getById(){
    documentSearchServiceIml.getById("2efbd668-044c-4e97-bf47-6dd61389fbd7");
}
@Test
public void getByName(){
    documentSearchServiceIml.getByName("name1","1");
}
@Test
public void delete(){
    documentSearchServiceIml.delete("98c717e2-0e17-4887-86f6-e9cd347f97f7");
}

}

完成

另外推荐教程:
https://mp.weixin.qq.com/s/PSeW21-HiZOT-5-PI–pVw

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值