ES支持的数据类型
- text:全文搜索字符串
- keyword:用于精确字符串匹配和聚合
- date 及 date_nanos:格式化为日期或数字日期的字符串
- byte, short, integer, long:整数类型
- boolean:布尔类型
- float,double,half_float:浮点数类型
- 分级的类型:object 及 nested。
java.net.UnknownHostException
如果你在使用docker部署elasticsearch集群服务中出现如下错误:java.net.UnknownHostException,那么请调大分配给docker的内存空间至4个G,官网给出的解决方案就是提升docker可用内存至4GB
es客户端配置
hightlevelclent
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.13/index.html
ik分词器
ES 中文分词器 ik分词器地址,分词器版本要和es版本一致
https://github.com/medcl/elasticsearch-analysis-ik/releases
REST 高级客户端
REST 高级客户端的 javadoc 可以在https://artifacts.elastic.co/javadoc/org/elasticsearch/client/elasticsearch-rest-high-level-client/7.13.4/index.html找到。
# - 创建索引库:PUT /索引库名
# - 查询索引库:GET /索引库名
# - 删除索引库:DELETE /索引库名
# - 添加字段:PUT /索引库名/_mapping
# 删除索引库
DELETE /mswk_csj_user
# 新建索引库
PUT /mswk_csj_user
# 查询索引库
GET /mswk_csj_user
# 测试分词器是否设置成功
GET /_analyze
{
"analyzer": "ik_max_word",
"text": "明思维科123"
}
# 创建索引库时指定mapping
PUT /mswk_csj_user
{
"mappings": {
"properties": {
"name":{
"type": "text"
}
}
}
}
# mapping不支持修改,只能新增
PUT /mswk_csj_user/_mapping
{
"properties":{
"id":{
"type":"keyword"
}
}
}
# 索引库新增映射关系
PUT /mswk_csj_user/_mapping
{
"properties": {
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"address": {
"type": "text",
"analyzer": "ik_max_word",
"index": true
}
}
}
# 插入文档,未指定id自动生成,指定id如果存在-更新
POST /mswk_csj_user/_doc/1
{
"name":"陈帅杰",
"age":"25",
"address":"北京市西城区",
"id":"41412321312"
}
# 根据id查询文档
GET /mswk_csj_user/_doc/1
# 删除文档,指定id
DELETE /mswk_csj_user/_doc/1
# put 全量更改 post 局部修改
PUT /mswk_csj_user/_doc/1
{
"name":"明思维科",
"age":"18",
"address":"嘿嘿额黑",
"id":"23123123"
}
POST /mswk_csj_user/_update/1
{
"doc": {
"name":"北京市"
}
}
java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonParser.getReadCa
jackson版本过高,降低版本
docker run -d \
--name es_7.13.0 \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-v es-data:/usr/share/elasticsearch/data \
-v es-plugins:/usr/share/elasticsearch/plugins \
-v es-logs:/usr/share/elasticsearch/logs \
--privileged \
--network es_net_7.13.0 \
-p 9200:9200 \
-p 9300:9300 \
elasticsearch:7.13.0
docker run -d \
--name kibana_7.13.0 \
-e ELASTICSEARCH_HOSTS=http://es_7.13.0:9200 \
--network=es_net_7.13.0 \
-p 5601:5601 \
kibana:7.13.0
ES SQL
# 简单查询
# _sql:sql查询方式
# format:返回数据格式 如 txt 类似数据库查询结果,也可以指定json格式
POST /_sql?format=txt
{
"query": """
SELECT * FROM "user"
"""
}
# 带条件查询,在kibana中建议sql使用三引号,条件字符串使用单引号
POST /_sql?format=txt
{
"query": """
SELECT name FROM "user" where name = '陈帅杰'
"""
}
# 使用DSL筛选条件
POST /_sql?format=txt
{
"query": """
SELECT * FROM "user"
""",
"filter":{
"term": {
"name.keyword": "陈帅杰"
}
}
}
# limit 分页 ORDER BY 排序
POST /_sql?format=txt
{
"query": """
SELECT * FROM "user" ORDER BY id desc limit 2
"""
}
# translate 支持将sql转换为es dsl查询
POST /_sql/translate
{
"query": """
SELECT * FROM "user" ORDER BY id desc
""",
"fetch_size": 2
}
<!--es jdbc-->
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>x-pack-sql-jdbc</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
# es数据源
esDatasource:
url: jdbc:es://http://172.16.88.128:9200
driver-class-name: org.elasticsearch.xpack.sql.jdbc.EsDriver
druid:
# 是否测试链接
test-while-idle: false
mybatis:
# xml包
mapper-locations: classpath:esMapper/*Mapper.xml
package com.csj.demo.business.config;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
/**
* @Desc : es数据源
* @Author : chen
* @Date : 2022/4/26 13:42
*/
@Slf4j
@Configuration
@MapperScan(basePackages = {"com.csj.demo.business.esDao"}, sqlSessionFactoryRef = "esSqlSessionFactory")
public class ESDruidConfig {
@Value("${spring.esDatasource.url}")
private String url;
@Value("${spring.esDatasource.driver-class-name}")
private String driverClassName;
@Value("${spring.esDatasource.druid.test-while-idle}")
private boolean testWhileIdle;
@Bean(name = "esDataSource")
public DataSource esDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setTestWhileIdle(testWhileIdle);
log.info("==============初始化ES DataSource连接"+url+"=================");
return dataSource;
}
@Value("${mybatis.config-location}")
private String configLocation;
@Value("${mybatis.type-aliases-package}")
private String typeAliasesPackage;
@Value("${spring.esDatasource.mybatis.mapper-locations}")
private String mapperLocations;
@Bean("esSqlSessionFactory")
public SqlSessionFactory esSessionFactory(@Qualifier("esDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(esDataSource());
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
bean.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
bean.setTypeAliasesPackage(typeAliasesPackage);
return bean.getObject();
}
}