当前位置: 首页 > news >正文

es0102---语法格式、数据类型、整合springboot、创建库、创建映射、新增数据、自定义查询

ES

一、创建映射字段的语法格式

需要先构建索引库,在构建索引库中的映射关系

PUT /索引库名/_mapping
{"properties": {"字段名": {"type": "类型","index": true,"store": false,"analyzer": "分词器"}}
}

 

#新增数据  id自增
POST /hl/_doc
{"title":"小米手机","images":"http://image.lano.com/12479122.jpg","price":2699.00
}#自己指定id信息
POST /hl/_doc/2
{"title":"OPPO手机","images":"http://image.lano.com/12479122.jpg","price":2999.00
}#修改数据
POST /hl/_doc/2
{"title":"VIVO手机","images":"http://image.lano.com/12479122.jpg","price":2999.00
}#智能判断 根据数据自动添加到映射,判断并指定数据类型
POST /hl/_doc/3
{"title":"超米手机","images":"http://image.lanou.com/12479122.jpg","price":2899.00,"stock": 200,"saleable":true
}put /hl/_doc/4
{"title":"小米电视AAA","images":"http://image.lano.com/12479122.jpg","price":2999.00
}#查询数据
GET hl/_search
{"query": {"match_all": {}}
}#or查询数据  小米or电视
GET hl/_search
{"query": {"match": {"title": "小米电视"}}
}
# and查询
GET hl/_search
{"query": {"match": {"title": {"query": "小米电视","operator": "and"}}}
}PUT /hl/_doc/5
{"title":"乐视电视","images":"http://image.lanou.com/12479122.jpg","price":2899.00,"subTitle":"小米电视手机"
}#多字段查询
GET hl/_search
{"query": {"multi_match": {"query": "小米","fields": [ "title", "subTitle" ]}}
}
#单词条精准查询
GET /hl/_search
{"query":{"term":{"price":2699.00}}
}
#多词条精准查询
GET /hl/_search
{"query":{"terms":{"price":[2699.00,2899.00]}}
}
#只查询特定字段
GET /hl/_search
{"_source": ["title","price"],"query": {"term": {"price": 2699}}
}
#只查询特定字段  指定includes和excludes
GET hl/_search
{
"_source": {
"excludes": "images",
"includes": ["title","price"]
},
"query": {
"term": {
"price": 2899.00
}
}
}PUT /hl/_doc/6
{"title":"apple手机","images":"http://image.lanou.com/12479122.jpg","price":6899.00
}
#模糊半径为1查询
GET hl/_search
{"query": {"fuzzy": {"title": "app"}}
}#模糊半径为2查询
GET hl/_search
{"query": {"fuzzy": {"title": {"value": "app22","fuzziness": 2}}}
}
  • 类型名称:映射的名称,字段名:任意填写。Elasticsearch7.0之后不支持类名名称写法所以需要添加include_type_name=true参数进行支持设置。

  • type:类型,可以是text、long、short、date、integer、object等

  • index:是否可以使用索引查询,默认为true

  • store:是否额外存储,默认为false

  • analyzer:分词器,这里的ik_max_word即使用ik分词器

二、了解数据类型

1、字符串

text: 可分词 不可聚合

keyword:不可分词 可聚合

2、数值

整数和浮点(float、double、half_float、scaled_float)

3、日期

date

三、使用springboot创建es项目

1、jar包

spring-boot-starter-data-elasticsearch

2、配置文件

spring:
elasticsearch:
uris: http://1.94.230.82:9200

3、使用esTemplate模版工具类

@RestController
@RequestMapping("/es")
public class EsController {@Autowiredprivate ElasticsearchRestTemplate restTemplate;

四、Es实现的功能

1、创建索引库

restTemplate.indexOps(User.class).create();

/*
@Document(indexName = "索引库名",shards = 分片数,replicas = 副本数)*/
@Document(indexName = "user",shards = 1,replicas = 0)
public class User {
}
​
package com.hl.es.web;
​
import com.hl.es.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
@RequestMapping("/es")
public class EsController {@Autowiredprivate ElasticsearchRestTemplate restTemplate;
​@RequestMapping("/test")public void getEsTemplate(){boolean flag = restTemplate.indexOps(User.class).exists();System.out.println("索引库是否存在:"+flag);if(!flag){//创建索引库boolean flag2 = restTemplate.indexOps(User.class).create();System.out.println("索引库创建结果:"+flag2);}}
}

2、创建映射

@Document(indexName = "user",shards = 1,replicas = 0)
@Data
public class User {@Idprivate Integer id;@Field(type = FieldType.Text,analyzer = "ik_max_word")private String username;@Field(type = FieldType.Text,analyzer = "ik_max_word")private String desc;@Field(type = FieldType.Keyword,index = false)private String password;@Field(type = FieldType.Text,analyzer = "ik_max_word")private String address;@Field(type = FieldType.Double)private Double money;@Field(type = FieldType.Date,format = DateFormat.custom,pattern = "YYYY-MM-dd")private Date createTime;
}
@RequestMapping("/createMapping")
public Object createMapping(){Document document = restTemplate.indexOps(User.class).createMapping();boolean flag = restTemplate.indexOps(User.class).putMapping(document);System.out.println("创建映射:"+flag);return flag;
}

3、新增数据

@RequestMapping("/save")
public Object save(User user){User user2 = restTemplate.save(user);return user2;
}

4、查询数据

自定义查询
package com.hl.es.dao;
​
import com.hl.es.pojo.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
​
import java.util.List;
​
public interface UserDaoextends ElasticsearchRepository<User, Integer> {//根据用户名查询集合//单字段public List<User> findByUsername(String username);public List<User> findByAddress(String address);//多字段public List<User> findByDescAndAddress(String desc, String address);public List<User> findByDescOrAddress(String desc, String address);//范围查询public List<User> findAllByIdGreaterThanEqual(Integer minId);public List<User> findByMoneyBetween(Double minPrice, Double maxPrice);//先根据范围查询,再降序排序public List<User> findByMoneyBetweenOrderByMoneyDesc(Double minPrice, Double maxPrice);
​
}

http://www.dtcms.com/a/296679.html

相关文章:

  • tensorflow搭建神经网络
  • SQL基础⑩ | 数据类型篇
  • 哈希表模拟实现
  • Java学习日记_廖万忠
  • Spring Cloud Alibaba Sentinel 源码阅读之流量控制算法
  • C++编程基础四大件
  • Bright Data 实战指南:从竞品数据抓取到电商策略优化全流程
  • 探秘 VSAR软件:CAN报文转DBC信号的便捷工具
  • 力扣189:轮转数组
  • 5 个适合创意创作的网站,灵感不设限
  • 基于markdown封装的前端文档编辑工具,markdown.js的解析与应用
  • 蚁群优化算法(ACO)求解旅行商问题(TSP)
  • 碳油 PCB 技术解析:高精度制造与多场景应用实践
  • Python爬虫案例:Scrapy+XPath解析当当网网页结构
  • Spring Boot 3整合Spring AI实战:9轮面试对话解析AI应用开发
  • FreeRTOS—计数型信号量
  • 亚马逊Prime Day变革:精细化运营时代的号角
  • 基础05-Java控制流程:掌握if-else、switch和循环语句
  • 使用adb 发送广播 动态改变app内的值
  • 【PyTorch】图像二分类项目-部署
  • 【数字IC验证学习------- SOC 验证 和 IP验证和形式验证的区别】
  • NOTEPAD!NPCommand函数分析之comdlg32!GetSaveFileNameW--windows记事本源代码分析
  • 暑假集训篇之并发处理①练习题
  • prometheus监控k8s的metric详解-01-apiserver部分-05-其他
  • 局域网TCP通过组播放地址rtp推流和拉流实现实时喊话
  • 猎板碳油 PCB和普通PCB的区别
  • 【OpenCV实现多图像拼接】
  • kafka消费者组消费进度(Lag)深入理解
  • Redis--哨兵机制详解
  • Linux C:预处理命令