关于elasticsearch是什么、elasticsearch的原理及elasticsearch能干什么,就不多说了,主要记录下自己的一个使用过程。

  1、安装

  elasticsearch是用java编写的,所以它的运行离不开jdk,jdk的安装这里不再啰嗦,我使用的是虚拟机是centos7,已经装好了jdk1.8,下面说下自己安装elasticsearch的过程。

  (1)到官网 https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html,查看各个操作系统的安装方式,找到对应的,我的是centos7,以root身份登录,执行 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.1.tar.gz。

  (2)解压 tar -zvxf elasticsearch-6.5.1.tar.gz -C /usr/local

  (3)默认ES 6.X 是不允许root用户运行的,否则ES运行的时候会报错,所以我们需要创建新的用户,groupadd es ——>useradd es -g es——>passwd es 按照提示设置密码即可。修改权限 chown -R es:es elasticsearch-6.5.1

  (4)修改配置,支持外网访问(记得关防火墙),在合适的位置创建两个文件夹,分别用来存储elasticsearch的数据和日志,执行vim config/elasticsearch.yml,取消下列对应的项的注释并修改      

  cluster.name: li-application #集群名称,可以自行修改
  node.name: linux-2 #节点名称,自行修改
  network.host: 0.0.0.0 #主机地址,这里写本机IP
  http.port: 9200 #端口
  path.data: /var/lib/es 数据存储位置
  path.logs: /var/logs/es 日志存储位置

  (5)执行vim /etc/security/limits.conf,在结尾加上如下配置,其中es是用户名

  es soft nofile 65536
  es hard nofile 131072
  es soft nproc 4096
  es hard nproc 4096

  (6)执行 vi /etc/sysctl.conf,在文件末尾添加 vm.max_map_count=655360,保存,并执行命令 sysctl -p

  (7)切换到es用户,进入/usr/local/elasticsearch-6.5.1,执行./bin/elasticsearch -d (后台启动)。

  (8)执行 curl -i localhost:9200,会有一个测试信息的响应,表示安装成功。

  2、整合springboot2.0

  (1)引入依赖

      <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency> <!--集合工具包-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>

  (2)添加配置,注意端口号是9300

spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.cluster-nodes=192.168.1.126:9300
spring.data.elasticsearch.repositories.enabled=true
server.port=11111

  (3)编写对应的代码

package com.example.demo;

import org.springframework.data.elasticsearch.annotations.Document;

import com.fasterxml.jackson.annotation.JsonProperty;

//indexName代表所以名称,type代表表名称
@Document(indexName = "wantu_notice_info", type = "doc")
public class Notice { //id
@JsonProperty("auto_id")
private Long id; //标题
@JsonProperty("title")
private String title; //公告标签
@JsonProperty("exchange_mc")
private String exchangeMc; //公告发布时间
@JsonProperty("create_time")
private String originCreateTime; //公告阅读数量
@JsonProperty("read_count")
private Integer readCount; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getExchangeMc() {
return exchangeMc;
} public void setExchangeMc(String exchangeMc) {
this.exchangeMc = exchangeMc;
} public String getOriginCreateTime() {
return originCreateTime;
} public void setOriginCreateTime(String originCreateTime) {
this.originCreateTime = originCreateTime;
} public Integer getReadCount() {
return readCount;
} public void setReadCount(Integer readCount) {
this.readCount = readCount;
} public Notice(Long id, String title, String exchangeMc, String originCreateTime, Integer readCount) {
super();
this.id = id;
this.title = title;
this.exchangeMc = exchangeMc;
this.originCreateTime = originCreateTime;
this.readCount = readCount;
} public Notice() {
super();
} }
package com.example.demo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component; @Component
public interface NoticeRepository extends ElasticsearchRepository<Notice, Long> { }
package com.example.demo;

import java.util.ArrayList;
import java.util.List; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.google.common.collect.Lists; @RestController
@RequestMapping("/api/v1/article")
public class NoticeController { @Autowired
private NoticeRepository nticeRepository; @GetMapping("/save")
public Object save(long id, String title){ Notice article = new Notice();
article.setId(id);
article.setReadCount(123);
article.setTitle(title);
return nticeRepository.save(article);
} /**
* @param title 搜索标题
* @param pageable page = 第几页参数, value = 每页显示条数
*/
@GetMapping("/search")
public Object search(String title ,@PageableDefault() Pageable pag){ //按标题进行搜索
QueryBuilder builder = QueryBuilders.matchQuery("title", title);
//如果实体和数据的名称对应就会自动封装,pageable分页参数
Iterable<Notice> listIt = nticeRepository.search(builder, pag);
//Iterable转list
List<Notice> list= Lists.newArrayList(listIt); return list;
} @RequestMapping("/all")
public List<Notice> all() throws Exception {
Iterable<Notice> data = nticeRepository.findAll();
List<Notice> ds = new ArrayList<>();
for (Notice d : data) {
ds.add(d);
}
return ds;
}
@RequestMapping("/id")
public Object findid(long id) throws Exception {
return nticeRepository.findById(id).get(); } }

  启动项目,先添加几条数据,然后依次访问全部记录、带"三"的记录、带"吃饭"的记录,效果如下:

最新文章

  1. AttributeError: type object &#39;_io.StringIO&#39; has no attribute &#39;StringIO&#39;
  2. windows下python安装pyquery
  3. Nginx+Nodejs搭建图片服务器
  4. C# 开发XML Web Service与Java开发WebService
  5. spring+hibernate+jpa+Druid的配置文件,spring整合Druid
  6. 初涉JavaScript模式 (11) : 模块模式
  7. 转:etc/fstab 文件详解
  8. grep搜索当前目录和递归搜索子目录中文本文件的特定pattern
  9. c++ string.find()函数的陷阱
  10. 【JSP】JSTL核心标签库的使用方法和示例
  11. Ubuntu下Chromium源码的编译
  12. java7 API详解
  13. 记录Winform开发过程中遇到的情况
  14. linux下文件和目录
  15. BZOJ 1269 文本编辑器editor(伸展树)
  16. javascript高级程序设计第3版——第二章使用javascript
  17. 199. Binary Tree Right Side View 从右侧看的节点数
  18. 2019年2月编程语言最新排行:java稳居第一(java优势在哪里)
  19. 免费SSL证书Let&#39;s Encrypt(certbot)安装使用教程
  20. jquery for&amp;&amp;each的用法

热门文章

  1. 18、Semantic-UI之进度条
  2. oracle数据库sqlldr命令的使用
  3. 【转】生活中的OO智慧——大话面向对象五大原则
  4. SSL证书 .pem转.pfx
  5. NOS服务监控实践
  6. Maven的安装环境变量配置
  7. Servlet实现Cookie读写
  8. [Swift]队列Queue的两种版本:(1)用类包装Queue (2)用泛型包装Queue
  9. NOI.AC省选模拟赛第一场 T1 (树上高斯消元)
  10. 【关于selenium自动化中,Webdriver的原理以及工作流程】