elasticsearch 的安装配置与spring boot的整合应用 (2)

如果在这里有报错的话,就把那npm的镜像修改为淘宝的镜像
1.通过config命令

npm config set registry https://registry.npm.taobao.org npm info underscore (如果上面配置正确这个命令会有字符串response)

然后在执行

npm install

上一步好了以后执行

grunt server

接下来就可以在浏览器上打开localhost:9100(或本机ip:9100)运行head插件了

|
|
|

在centos上使用curl插入数据到elasticsearch里面的正确格式 curl -H "Content-Type: application/json" -XPOST 192.168.14.173:32000/test_index_1221/test_type/5 -d '{"user_name":"xiaoming"}' 在windows上使用curl插入数据到elasticsearch里面的正确格式 curl -H "Content-Type: application/json" -XPOST 192.168.14.173:32000/test_index_1221/test_type/5 -d "{"""user_name""":"""xiaoming"""}" spring boot整合elasticsearch

application.properties配置

//开启 Elasticsearch 仓库。(默认值:true。) spring.data.elasticsearch.repositories.enabled = true //集群的地址 spring.data.elasticsearch.cluster-nodes =192.168.175.131:9300 //集群名。(默认值: elasticsearch) spring.data.elasticsearch.cluster-name= es

pom.xml依赖

<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>

controller类:

package com.example.demo; import com.example.demo.User; import com.example.demo.dom; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class controller { @Autowired private dom userRepository; @ResponseBody //http://localhost:8888/save @GetMapping("save") public String save(){ User user = new User(System.currentTimeMillis(),"商品","这是一个测试商品",15,"2018-4-25 11:07:42"); userRepository.save(user); return "success"; } //http://localhost:8888/delete?id=1525415333329 @GetMapping("delete") public String delete(long id){ userRepository.deleteById(id); return "success"; } //http://localhost:8888/update?id=1525417362754&name=修改&description=修改 @GetMapping("update") public String update(long id,String name,String description,Integer age,String createtm){ User user = new User(id, name,description,age,createtm); userRepository.save(user); return "success"; } }

dom类继承ElasticsearchRepository:

package com.example.demo; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Component; import com.example.demo.User; @Component public interface dom extends ElasticsearchRepository<User, Long>{ }

User类:

package com.example.demo; import java.io.Serializable; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "q1", type = "user") public class User implements Serializable { private static final long serialVersionUID = 1L; /** 编号 */ private Long id; /** 姓名 */ private String name; /** 年龄 */ private Integer age; /** 描述 */ private String description; /** 创建时间 */ private String createtm; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getCreatetm() { return createtm; } public void setCreatetm(String createtm) { this.createtm = createtm; } public static long getSerialversionuid() { return serialVersionUID; } public User(Long id, String name, String description,Integer age,String createtm) { this.id = id; this.name = name; this.description = description; this.age=age; this.createtm=createtm; } }

然后在浏览器输入localhost:8080/save
浏览器会显示success
然后在head中看到以下数据

elasticsearch 的安装配置与spring boot的整合应用


到这里就spring boot整合成功!
=
=
=
=======================================================================================

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpzyjy.html