4.4 ES数据类型
- 简单数据类型
- 字符串
聚合:相当于mysql 中的sum(求和)
text:会分词,不支持聚合 keyword:不会分词,将全部内容作为一个词条,支持聚合
- 数值
- 布尔:boolean
- 二进制:binary
- 范围类型
integer_range, float_range, long_range, double_range, date_range
- 日期:date
- 复杂数据类型
•数组:[ ] Nested: nested
(for arrays of JSON objects 数组类型的JSON对象)
•对象:{ } Object: object(for single JSON objects 单个JSON对象)
4.5 操作映射
PUT person GET person #添加映射 PUT /person/_mapping { "properties":{ "name":{ "type":"text" }, "age":{ "type":"integer" } } }
#创建索引并添加映射
#创建索引并添加映射 PUT /person1 { "mappings": { "properties": { "name": { "type": "text" }, "age": { "type": "integer" } } } } GET person1/_mapping
添加字段
#添加字段 PUT /person1/_mapping { "properties": { "name": { "type": "text" }, "age": { "type": "integer" } } }
4.6 操作文档
•添加文档,指定id
POST /person1/_doc/2 { "name":"张三", "age":18, "address":"北京" } GET /person1/_doc/1
•添加文档,不指定id
#添加文档,不指定id POST /person1/_doc/ { "name":"张三", "age":18, "address":"北京" } #查询所有文档 GET /person1/_search
#删除指定id文档 DELETE /person1/_doc/1
5 分词器
5.1分词器-介绍
•IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包
•是一个基于Maven构建的项目
•具有60万字/秒的高速处理能力
•支持用户词典扩展定义
•下载地址:https://github.com/medcl/elasticsearch-analysis-ik/archive/v7.4.0.zip
安装包在资料文件夹中提供
5.2 ik分词器安装
参见 ik分词器安装.md
执行如下命令时如果出现 打包失败(501码)将maven镜像换成阿里云的
mvn package
/opt/apache-maven-3.1.1/conf/setting.xml
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
5.3-ik分词器使用
IK分词器有两种分词模式:ik_max_word和ik_smart模式。
1、ik_max_word
会将文本做最细粒度的拆分,比如会将“乒乓球明年总冠军”拆分为“乒乓球、乒乓、球、明年、总冠军、冠军。
#方式一ik_max_word GET /_analyze { "analyzer": "ik_max_word", "text": "乒乓球明年总冠军" }
ik_max_word分词器执行如下:
{ "tokens" : [ { "token" : "乒乓球", "start_offset" : 0, "end_offset" : 3, "type" : "CN_WORD", "position" : 0 }, { "token" : "乒乓", "start_offset" : 0, "end_offset" : 2, "type" : "CN_WORD", "position" : 1 }, { "token" : "球", "start_offset" : 2, "end_offset" : 3, "type" : "CN_CHAR", "position" : 2 }, { "token" : "明年", "start_offset" : 3, "end_offset" : 5, "type" : "CN_WORD", "position" : 3 }, { "token" : "总冠军", "start_offset" : 5, "end_offset" : 8, "type" : "CN_WORD", "position" : 4 }, { "token" : "冠军", "start_offset" : 6, "end_offset" : 8, "type" : "CN_WORD", "position" : 5 } ] }
2、ik_smart
会做最粗粒度的拆分,比如会将“乒乓球明年总冠军”拆分为乒乓球、明年、总冠军。
#方式二ik_smart GET /_analyze { "analyzer": "ik_smart", "text": "乒乓球明年总冠军" }
ik_smart分词器执行如下:
{ "tokens" : [ { "token" : "乒乓球", "start_offset" : 0, "end_offset" : 3, "type" : "CN_WORD", "position" : 0 }, { "token" : "明年", "start_offset" : 3, "end_offset" : 5, "type" : "CN_WORD", "position" : 1 }, { "token" : "总冠军", "start_offset" : 5, "end_offset" : 8, "type" : "CN_WORD", "position" : 2 } ] }
由此可见 使用ik_smart可以将文本"text": "乒乓球明年总冠军"分成了【乒乓球】【明年】【总冠军】
这样看的话,这样的分词效果达到了我们的要求。
5.4 使用IK分词器-查询文档
•词条查询:term
词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时才匹配搜索
•全文查询:match
全文查询会分析查询条件,先将查询条件进行分词,然后查询,求并集
1.创建索引,添加映射,并指定分词器为ik分词器
PUT person2 { "mappings": { "properties": { "name": { "type": "keyword" }, "address": { "type": "text", "analyzer": "ik_max_word" } } } }
2.添加文档
POST /person2/_doc/1 { "name":"张三", "age":18, "address":"北京海淀区" } POST /person2/_doc/2 { "name":"李四", "age":18, "address":"北京朝阳区" } POST /person2/_doc/3 { "name":"王五", "age":18, "address":"北京昌平区" }
3.查询映射
GET person2
4.查看分词效果
GET _analyze { "analyzer": "ik_max_word", "text": "北京海淀" }
5.词条查询:term
查询person2中匹配到"北京"两字的词条
GET /person2/_search { "query": { "term": { "address": { "value": "北京" } } } }
6.全文查询:match
全文查询会分析查询条件,先将查询条件进行分词,然后查询,求并集
GET /person2/_search { "query": { "match": { "address":"北京昌平" } } }
6 ElasticSearch JavaApi
6.1SpringBoot整合ES
①搭建SpringBoot工程
②引入ElasticSearch相关坐标
<!--引入es的坐标--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.4.0</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.4.0</version> </dependency>
③测试
ElasticSearchConfig
@Configuration @ConfigurationProperties(prefix="elasticsearch") public class ElasticSearchConfig { private String host; private int port; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } @Bean public RestHighLevelClient client(){ return new RestHighLevelClient(RestClient.builder( new HttpHost(host,port,"http") )); } }
ElasticsearchDay01ApplicationTests
注意:使用@Autowired注入RestHighLevelClient 如果报红线,则是因为配置类所在的包和测试类所在的包,包名不一致造成的
@SpringBootTest class ElasticsearchDay01ApplicationTests { @Autowired RestHighLevelClient client; /** * 测试 */ @Test void contextLoads() { System.out.println(client); } }
拓展知识
使用spring-boot-starter-data-elasticsearch整合es
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
spring.elasticsearch.rest.uris=http://192.168.111.135:9200
package com.example.esdemo; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "user_haha") public class User { @Id private String id; private String username; private String address; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
package com.example.esdemo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.IndexOperations; @SpringBootTest(classes = EsDemoApplication.class) public class EsDemoApplicationTests { // @Test // void contextLoads() { // } @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; @Test public void t(){ elasticsearchRestTemplate.indexOps(User.class).create(); } @Test public void sava(){ User user = new User(); user.setAddress("aa"); user.setId("222"); user.setUsername("huojintao"); elasticsearchRestTemplate.save(user); } }
6.2-创建索引
1.添加索引
/** * 添加索引 * @throws IOException */ @Test public void addIndex() throws IOException { //1.使用client获取操作索引对象 IndicesClient indices = client.indices(); //2.具体操作获取返回值 //2.1 设置索引名称 CreateIndexRequest createIndexRequest=new CreateIndexRequest("itheima"); CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT); //3.根据返回值判断结果 System.out.println(createIndexResponse.isAcknowledged()); }
2.添加索引,并添加映射
/** * 添加索引,并添加映射 */ @Test public void addIndexAndMapping() throws IOException { //1.使用client获取操作索引对象 IndicesClient indices = client.indices(); //2.具体操作获取返回值 //2.具体操作,获取返回值 CreateIndexRequest createIndexRequest = new CreateIndexRequest("oldlu"); //2.1 设置mappings String mapping = "{\n" + " \"properties\" : {\n" + " \"address\" : {\n" + " \"type\" : \"text\",\n" + " \"analyzer\" : \"ik_max_word\"\n" + " },\n" + " \"age\" : {\n" + " \"type\" : \"long\"\n" + " },\n" + " \"name\" : {\n" + " \"type\" : \"keyword\"\n" + " }\n" + " }\n" + " }"; // 版本不同 选择不同 createIndexRequest.mapping(mapping,XContentType.JSON); createIndexRequest.mapping("_doc",mapping, XContentType.JSON); CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT); //3.根据返回值判断结果 System.out.println(createIndexResponse.isAcknowledged()); }
6.3-查询、删除、判断索引
查询索引
/** * 查询索引 */ @Test public void queryIndex() throws IOException { IndicesClient indices = client.indices(); GetIndexRequest getRequest=new GetIndexRequest("oldlu"); GetIndexResponse response = indices.get(getRequest, RequestOptions.DEFAULT); Map<String, MappingMetaData> mappings = response.getMappings(); //iter 提示foreach for (String key : mappings.keySet()) { System.out.println(key+"==="+mappings.get(key).getSourceAsMap()); } }
删除索引
/** * 删除索引 */ @Test public void deleteIndex() throws IOException { IndicesClient indices = client.indices(); DeleteIndexRequest deleteRequest=new DeleteIndexRequest("itheima"); AcknowledgedResponse delete = indices.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); }
索引是否存在
/** * 索引是否存在 */ @Test public void existIndex() throws IOException { IndicesClient indices = client.indices(); GetIndexRequest getIndexRequest=new GetIndexRequest("itheima"); boolean exists = indices.exists(getIndexRequest, RequestOptions.DEFAULT); System.out.println(exists); }
6.4-添加文档
1.添加文档,使用map作为数据
@Test public void addDoc1() throws IOException { Map<String, Object> map=new HashMap<>(); map.put("name","张三"); map.put("age","18"); map.put("address","北京二环"); IndexRequest request=new IndexRequest("oldlu").id("1").source(map); IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println(response.getId()); }
2.添加文档,使用对象作为数据
@Test public void addDoc2() throws IOException { Person person=new Person(); person.setId("2"); person.setName("李四"); person.setAge(20); person.setAddress("北京三环"); String data = JSON.toJSONString(person); IndexRequest request=new IndexRequest("oldlu").id(person.getId()).source(data,XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println(response.getId()); }
package com.example.esdemo; public class Person { private String id; private String name; private int age; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Person{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + '}'; } }
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.4</version> </dependency>
6.5-修改、查询、删除文档
1.修改文档:添加文档时,如果id存在则修改,id不存在则添加
/** * 修改文档:添加文档时,如果id存在则修改,id不存在则添加 */ @Test public void UpdateDoc() throws IOException { Person person=new Person(); person.setId("2"); person.setName("李四"); person.setAge(20); person.setAddress("北京三环车王"); String data = JSON.toJSONString(person); IndexRequest request=new IndexRequest("oldlu").id(person.getId()).source(data,XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println(response.getId()); }
2.根据id查询文档
/** * 根据id查询文档 */ @Test public void getDoc() throws IOException { //设置查询的索引、文档 GetRequest indexRequest=new GetRequest("oldlu","2"); GetResponse response = client.get(indexRequest, RequestOptions.DEFAULT); System.out.println(response.getSourceAsString()); }
3.根据id删除文档
/** * 根据id删除文档 */ @Test public void delDoc() throws IOException { //设置要删除的索引、文档 DeleteRequest deleteRequest=new DeleteRequest("oldlu","1"); DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(response.getId()); }