本文有github地址:github
GitHub 、维基百科、电商网站、日志查询、站内搜索
JSON存储 的文档型数据库,一条记录在这里代表一个文档
支持数据类,
string(text(支持分词),keyword)
boolean
date
long
double
{
"name": "xiaoMing",
"age": "18",
"sex": "man"
}
关系型数据库如mysql: 数据库——表——行——列(column)
ES:索引(index)——类型(type)——文档(document)——字段(fields)
注意:索引名称必须小写
必须传入ID,除非是POST方式,会生成一个随机ID
### 创建索引索引所
PUT myindex
GET /myindex
PUT /myindex/user/id1
{
"name": "xiaoMing",
"age": "18",
"sex": "man"
}
GET /myindex/user/id1
## update name
PUT /myindex/user/id1
{
"name": "xiaoMing2",
"age": "18",
"sex": "man"
}
DELETE /myindex
指定版本:
PUT /myindex/user/id1?version=7
{
"name": "xiaoMing2",
"age": "18",
"sex": "man"
}
也可以用外部的版本控制(只有外部的提供的要大于自身的版本号)
# es配置
spring:
data:
elasticsearch:
#集群名称
cluster-name: xiyou
#这里的名字一定要和elasticsearch.yml中的一样
#集群地址
cluster-nodes: 192.168.1.100:9300
server:
port: 8082
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
注意:id要为String类型
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "index_user",type = "user")
public class UserEntity {
@Id
private String id ;
private String name;
private String sex;
private Integer age;
}
3.Dao
package com.mydubbo.es.dao;
import com.mydubbo.es.entity.UserEntity;
import org.springframework.data.repository.CrudRepository;
public interface UserDao extends CrudRepository<UserEntity,String> {
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.mydubbo.es")
public class ElastaicSearchApplication {
public static void main(String[] args) {
SpringApplication.run(ElastaicSearchApplication.class, args);
}
}
import com.mydubbo.es.dao.UserDao;
import com.mydubbo.es.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.Optional;
@RestController
public class UserController {
@Autowired
private UserDao userDao;
@GetMapping("/addUser")
public UserEntity addUser() {
UserEntity user = new UserEntity();
String userName = new Date().toString();
user.setName(userName);
user.setId(String.valueOf(userName.hashCode()));
user.setAge(18);
user.setSex("man");
return userDao.save(user);
}
@GetMapping("/getUser")
public Optional<UserEntity> getUser(String userId) {
return userDao.findById(userId);
}
}
9300是TCP宽口协议号,ES集群之间通信的端口号,springboot启动端口
9200 是暴露ES RESTFul接口的端口号
比如下面两个句子
句子A: 小米手机是很受欢迎的手机
句子B:华为手机是国产手机的骄傲
句子C:OPPO手机是主打拍照的手机,也很受欢迎
单词ID | 单词 | 倒排列表 |
---|---|---|
1 | 小米 | A |
2 | 手机 | ABC |
3 | 欢迎 | AC |
4 | 国产 | B |
5 | 骄傲 | B |
6 | OPPO | C |
7 | 主打 | C |
8 | 拍照 | C |
PUT /myindex/user/id3
{
"name": "xiaoMing",
"age": "18",
"sex": "man",
"juzi":" 报价"
}
PUT /myindex/user/id4
{
"name": "xiaoMing",
"age": "18",
"sex": "man",
"juzi":" 报价宝马包,宝马"
}
PUT /myindex/user/id5
{
"name": "xiaoMing",
"age": "18",
"sex": "man",
"juzi":"宝马"
}
## 查询全部的
GET /myindex/user/_search
GET /myindex/user/_search
{
"query": {
"term": {
"age": {
"value": "18"
}
}
}
}
GET /myindex/user/_search
{
"from": 0,
"size": 20,
"query": {
"match": {
"juzi": "宝马"
}
}
}
还有很多,
_source展示什么字段
ES-IK插件
但是你的ES是什么版本,就要安装什么版本的插件
插件地址:github
自定义热词:
进入Ik /config里面IKanalyer.cfg.xml里面,用户可以自已扩展自己的字典