赞
踩
一、简介
Elasticsearch(ES)是一个基于Lucene构建的开源、分布式、RESTful接口的全文搜索引擎,也是一个分布式文档数据库,其中每个字段均可被索引,而且每个字段的数据均可被搜索,ES能够横向扩展至数以百计的服务器存储以及处理PB级的数据。可以在极短的时间内存储、搜索和分析大量的数据。通常作为具有复杂搜索场景情况下的核心发动机。
二、作用
ES可以根据用户搜索内容,为用户提供精准搜索,推荐相关商品。
当你想收集日志或者交易数据,想要分析并挖掘这些数据,从而寻找趋势,进行统计、总结或发现异常时,可以使用Logstash或者其他工具来进行收集数据,并将数据存储到ElasticsSearch中,进行搜索和汇总这些数据,找到任何你感兴趣的信息。
三、整合
下载地址:
http://nodejs.cn/download/
可选择msi安装程序,直接安装,或使用zip解压安装
在文件中加入
http.cors.enabled: true
http.cors.allow-origin: “*”
node.master: true
node.data: true
释放network.host: 192.168.0.1的注释并改为network.host: 0.0.0.0
释放http.port ,cluster.name和node.name的注释
d.elasticsearch-head下载
下载地址:https://github.com/mobz/elasticsearch-head
解压,修改elasticsearch-head-master\Gruntfile.js 添加
进入elasticsearch-head-master_site修改app.js 中下文内容为服务器地址,若是本机部署,也可不修改。
cmd切换到对应的解压目录,进行npm install安装
启动: npm run start 或grunt server
输入http://localhost:9200出现
则表示安装成功
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.li</groupId> <artifactId>demo-es</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-es</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--alibaba druid连接池 --> <!--Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 通用 UTIL 包 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version> </dependency> <!-- es核心jar包 --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <!-- <version>3.0.9</version> --> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency> <!--添加 lombok 依赖(@Getter和@Setter 方法)--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在配置文件中添加
package com.li.demoes.entity; import lombok.Getter; import lombok.Setter; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Field; import java.io.Serializable; /** * @author li * @Description 实体类 * @project springboot_learn * @package com.dalaoyang.entity * @email yangyang@dalaoyang.cn * @date 2018/5/4 */ @Document(indexName = "test-boot",type = "goods",shards = 1,replicas = 0, refreshInterval = "-1") //indexName索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常 //type类型 可以理解为表名 @Getter @Setter public class Elastic implements Serializable { @Id private String id; @Field private String name; @Field private String content; @Field private String price; }
package com.li.demoes.dao;
import com.li.demoes.entity.Elastic;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
@Component
public interface ESDao extends ElasticsearchRepository<Elastic, String> {
Elastic queryEmployeeById(String id);
}
package com.li.demoes.controller; import com.google.gson.Gson; import com.li.demoes.dao.ESDao; import com.li.demoes.entity.Elastic; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/es") public class ElasticSearchController { @Autowired private ESDao er; //增加 @RequestMapping("/add/{id}") public String add(@PathVariable("id")String id){ Elastic employee=new Elastic(); employee.setId(id); employee.setName("Y.S.K"); employee.setContent("ooo"); employee.setPrice("26"); er.save(employee); System.err.println("add a obj"); return "success"; } //删除 @RequestMapping("/delete") public String delete(){ Elastic employee=new Elastic(); employee.setId("1"); er.delete(employee); return "success"; } //局部更新 @RequestMapping("/update") public String update(){ Elastic employee=er.queryEmployeeById("1"); employee.setName("哈哈"); er.save(employee); System.err.println("update a obj"); return "success"; } //查询 @RequestMapping("/query/{id}") public Elastic query(@PathVariable("id")String id){ Elastic accountInfo=er.queryEmployeeById(id); System.err.println(new Gson().toJson(accountInfo)); return accountInfo; } }
可输入http://localhost:8080/demo/es/add/1测试,出现
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。