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客户端配置

https://docs.spring.io/spring-data/elasticsearch/docs/4.0.2.RELEASE/reference/html/#elasticsearch.clients.configuration

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找到。

  1. # - 创建索引库:PUT /索引库名
  2. # - 查询索引库:GET /索引库名
  3. # - 删除索引库:DELETE /索引库名
  4. # - 添加字段:PUT /索引库名/_mapping
  5. # 删除索引库
  6. DELETE /mswk_csj_user
  7. # 新建索引库
  8. PUT /mswk_csj_user
  9. # 查询索引库
  10. GET /mswk_csj_user
  11. # 测试分词器是否设置成功
  12. GET /_analyze
  13. {
  14. "analyzer": "ik_max_word",
  15. "text": "明思维科123"
  16. }
  17. # 创建索引库时指定mapping
  18. PUT /mswk_csj_user
  19. {
  20. "mappings": {
  21. "properties": {
  22. "name":{
  23. "type": "text"
  24. }
  25. }
  26. }
  27. }
  28. # mapping不支持修改,只能新增
  29. PUT /mswk_csj_user/_mapping
  30. {
  31. "properties":{
  32. "id":{
  33. "type":"keyword"
  34. }
  35. }
  36. }
  37. # 索引库新增映射关系
  38. PUT /mswk_csj_user/_mapping
  39. {
  40. "properties": {
  41. "name": {
  42. "type": "keyword"
  43. },
  44. "age": {
  45. "type": "integer"
  46. },
  47. "address": {
  48. "type": "text",
  49. "analyzer": "ik_max_word",
  50. "index": true
  51. }
  52. }
  53. }
  1. # 插入文档,未指定id自动生成,指定id如果存在-更新
  2. POST /mswk_csj_user/_doc/1
  3. {
  4. "name":"陈帅杰",
  5. "age":"25",
  6. "address":"北京市西城区",
  7. "id":"41412321312"
  8. }
  9. # 根据id查询文档
  10. GET /mswk_csj_user/_doc/1
  11. # 删除文档,指定id
  12. DELETE /mswk_csj_user/_doc/1
  13. # put 全量更改 post 局部修改
  14. PUT /mswk_csj_user/_doc/1
  15. {
  16. "name":"明思维科",
  17. "age":"18",
  18. "address":"嘿嘿额黑",
  19. "id":"23123123"
  20. }
  21. POST /mswk_csj_user/_update/1
  22. {
  23. "doc": {
  24. "name":"北京市"
  25. }
  26. }
  1. java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonParser.getReadCa
  2. jackson版本过高,降低版本
  1. docker run -d \
  2. --name es_7.13.0 \
  3. -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
  4. -e "discovery.type=single-node" \
  5. -v es-data:/usr/share/elasticsearch/data \
  6. -v es-plugins:/usr/share/elasticsearch/plugins \
  7. -v es-logs:/usr/share/elasticsearch/logs \
  8. --privileged \
  9. --network es_net_7.13.0 \
  10. -p 9200:9200 \
  11. -p 9300:9300 \
  12. elasticsearch:7.13.0
  13. docker run -d \
  14. --name kibana_7.13.0 \
  15. -e ELASTICSEARCH_HOSTS=http://es_7.13.0:9200 \
  16. --network=es_net_7.13.0 \
  17. -p 5601:5601 \
  18. kibana:7.13.0

ES SQL

es 7.13 sql语句

  1. # 简单查询
  2. # _sql:sql查询方式
  3. # format:返回数据格式 txt 类似数据库查询结果,也可以指定json格式
  4. POST /_sql?format=txt
  5. {
  6. "query": """
  7. SELECT * FROM "user"
  8. """
  9. }
  10. # 带条件查询,在kibana中建议sql使用三引号,条件字符串使用单引号
  11. POST /_sql?format=txt
  12. {
  13. "query": """
  14. SELECT name FROM "user" where name = '陈帅杰'
  15. """
  16. }
  17. # 使用DSL筛选条件
  18. POST /_sql?format=txt
  19. {
  20. "query": """
  21. SELECT * FROM "user"
  22. """,
  23. "filter":{
  24. "term": {
  25. "name.keyword": "陈帅杰"
  26. }
  27. }
  28. }
  29. # limit 分页 ORDER BY 排序
  30. POST /_sql?format=txt
  31. {
  32. "query": """
  33. SELECT * FROM "user" ORDER BY id desc limit 2
  34. """
  35. }
  36. # translate 支持将sql转换为es dsl查询
  37. POST /_sql/translate
  38. {
  39. "query": """
  40. SELECT * FROM "user" ORDER BY id desc
  41. """,
  42. "fetch_size": 2
  43. }
  1. <!--es jdbc-->
  2. <dependency>
  3. <groupId>org.elasticsearch.plugin</groupId>
  4. <artifactId>x-pack-sql-jdbc</artifactId>
  5. <version>${elasticsearch.version}</version>
  6. </dependency>
  1. # es数据源
  2. esDatasource:
  3. url: jdbc:es://http://172.16.88.128:9200
  4. driver-class-name: org.elasticsearch.xpack.sql.jdbc.EsDriver
  5. druid:
  6. # 是否测试链接
  7. test-while-idle: false
  8. mybatis:
  9. # xml包
  10. mapper-locations: classpath:esMapper/*Mapper.xml
  1. package com.csj.demo.business.config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5. import org.mybatis.spring.SqlSessionFactoryBean;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.core.io.DefaultResourceLoader;
  11. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  12. import tk.mybatis.spring.annotation.MapperScan;
  13. import javax.sql.DataSource;
  14. /**
  15. * @Desc : es数据源
  16. * @Author : chen
  17. * @Date : 2022/4/26 13:42
  18. */
  19. @Slf4j
  20. @Configuration
  21. @MapperScan(basePackages = {"com.csj.demo.business.esDao"}, sqlSessionFactoryRef = "esSqlSessionFactory")
  22. public class ESDruidConfig {
  23. @Value("${spring.esDatasource.url}")
  24. private String url;
  25. @Value("${spring.esDatasource.driver-class-name}")
  26. private String driverClassName;
  27. @Value("${spring.esDatasource.druid.test-while-idle}")
  28. private boolean testWhileIdle;
  29. @Bean(name = "esDataSource")
  30. public DataSource esDataSource() {
  31. DruidDataSource dataSource = new DruidDataSource();
  32. dataSource.setDriverClassName(driverClassName);
  33. dataSource.setUrl(url);
  34. dataSource.setTestWhileIdle(testWhileIdle);
  35. log.info("==============初始化ES DataSource连接"+url+"=================");
  36. return dataSource;
  37. }
  38. @Value("${mybatis.config-location}")
  39. private String configLocation;
  40. @Value("${mybatis.type-aliases-package}")
  41. private String typeAliasesPackage;
  42. @Value("${spring.esDatasource.mybatis.mapper-locations}")
  43. private String mapperLocations;
  44. @Bean("esSqlSessionFactory")
  45. public SqlSessionFactory esSessionFactory(@Qualifier("esDataSource") DataSource dataSource) throws Exception {
  46. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  47. bean.setDataSource(esDataSource());
  48. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
  49. bean.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
  50. bean.setTypeAliasesPackage(typeAliasesPackage);
  51. return bean.getObject();
  52. }
  53. }