es 安装

https://www.elastic.co/cn/

docker 安装 es

  1. docker pull docker.io/elasticsearch:7.6.1
  2. service docker start
  1. 运行容器启动 Elasticsearch
    image.png
    1. docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" b0e9f9f047e6

    Docker安装Elasticsearch、Kibana

    1. 下载镜像文件

    存储和检索数据 docker pull elasticsearch:7.4.2 # 可视化检索数据 docker pull kibana:7.4.2

    2. 配置挂载数据文件夹

    1. # 创建配置文件目录
    2. mkdir -p /mydata/elasticsearch/config
    3. # 创建数据目录
    4. mkdir -p /mydata/elasticsearch/data
    5. # 将/mydata/elasticsearch/文件夹中文件都可读可写
    6. chmod -R 777 /mydata/elasticsearch/
    7. # 配置任意机器可以访问
    8. echo "http.host: 0.0.0.0" >/mydata/elasticsearch/config/elasticsearch.yml

3. 启动Elasticsearch

命令后面的 \是换行符,注意前面有空格

  1. docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \ -e
  2. "discovery.type=single-node" \ -e ES_JAVA_OPTS="-Xms64m -Xmx512m" \ -v
  3. /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \ -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
  4. -d elasticsearch:7.6.1
  1. docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins -d elasticsearch:7.6.1
  • -p 9200:9200 -p 9300:9300:向外暴露两个端口,9200用于HTTP REST API请求,9300 ES 在分布式集群状态下 ES 之间的通信端口;
  • -e “discovery.type=single-node”:es 以单节点运行
  • -e ES_JAVA_OPTS=”-Xms64m -Xmx512m”:设置启动占用内存,不设置可能会占用当前系统所有内存
  • -v:挂载容器中的配置文件、数据文件、插件数据到本机的文件夹;
  • -d elasticsearch:7.6.1:指定要启动的镜像

访问 IP:9200 看到返回的 json 数据说明启动成功。
ES 基础 - 图2

4. 设置 Elasticsearch 随Docker启动

当前 Docker 开机自启,所以 ES 现在也是开机自启 docker update elasticsearch —restart=always

5. 启动可视化Kibana

  1. docker run --name kibana \ -e ELASTICSEARCH_HOSTS=http://120.77.34.253:9200 \
  2. -p 5601:5601 \ -d kibana:7.6.1
  1. docker run --name kibana -e ELASTICSEARCH_HOSTS=http://120.77.34.253:9200 -p 5601:5601 -d kibana:7.6.1

-e ELASTICSEARCH_HOSTS=http://192.168.163.131:9200: 这里要设置成自己的虚拟机IP地址
浏览器输入192.168.163.131:5601 测试:
ES 基础 - 图3

6. 设置 Kibana 随Docker启动

当前 Docker 开机自启,所以 kibana 现在也是开机自启 docker update kibana —restart=always

index 操作

搜索

使用 match_all 来查询全部数据

  1. GET _search
  2. {
  3. "_source": ["account_number", "balance"], // 指定返回的字段内容
  4. "query": {
  5. "match_all": {}
  6. }
  7. "from" 0,
  8. "size": 10
  9. "sort": { // 指定排序字段和排序顺序
  10. "balance": {
  11. "order": "desc"
  12. }
  13. }

条件搜索

使用match 表示匹配条件

  1. GET _search
  2. {
  3. "query": {
  4. "match": {
  5. "account_number": 20
  6. }
  7. }
  8. }

组合搜索

组合搜索,使用bool 来进行组合,相当于 SQL where 关键字,must 表示同时满足条件

  1. GET _search
  2. {
  3. "query": {
  4. "match": {
  5. "bool": {
  6. "must": [
  7. { "match": { "address": "mill"}},
  8. {"match": { "address": "mills"}}
  9. ]
  10. }
  11. }
  12. }
  13. }

should 表示满足其中任意一个条件

  1. GET _search
  2. {
  3. "query": {
  4. "match": {
  5. "bool": {
  6. "should": [
  7. { "match": { "address": "mill"}},
  8. {"match": { "address": "mills"}}
  9. ]
  10. }
  11. }
  12. }
  13. }

过滤搜索

搜索过滤,使用 filter 来表示,一般用于范围查询。
gt: greater than 大于
gte: greater than or equal 大于等于
lt: less than 小于
lte: less than or equal 小于等于

  1. GET _search
  2. {
  3. "query": {
  4. "match": {
  5. "bool": {
  6. "should": [
  7. { "match": { "address": "mill"}},
  8. {"match": { "address": "mills"}}
  9. ],
  10. "filter": {
  11. "range": {
  12. "count": { //count 字段
  13. "gte": 2000, //
  14. "lte": 3000
  15. }
  16. }
  17. }
  18. }

搜索聚合
对搜索结果进行聚合,使用aggs (aggregation) 组合 表示,类似于MySQL中的 group by ,例如对state 字段进行聚合,统计出相同 state 的文档数量

  1. GET _search
  2. {
  3. "size": 0,
  4. "aggs" {
  5. "group_by_state": { // 分组名
  6. "term": {
  7. "field": "state.keyword"
  8. }
  9. }
  10. }
  11. }

嵌套聚合,例如对state 字段进行聚合,统计出相同 state 的文档数量,再统计出balance 的平均值

  1. GET _search
  2. {
  3. "size": 0,
  4. "aggs" {
  5. "group_by_state": {
  6. "term": {
  7. "field": "state.keyword"
  8. },
  9. "aggs": {
  10. "average": {
  11. "avg":{
  12. "field": "balance"
  13. }
  14. }
  15. }
  16. }
  17. }
  1. public class TestEs {
  2. private String clusterName ="es-smk-sit";
  3. private String clusterNode = "192.168.23.10";
  4. private String clusterPort ="9301";
  5. private String poolSize = "10";
  6. private boolean snf = true;
  7. private String index = "smk-label";
  8. private String type = "label";
  9. public TransportClient transportClient() {
  10. TransportClient transportClient = null;
  11. try {
  12. Settings esSetting = Settings.builder()
  13. .put("cluster.name", clusterName) //集群名字
  14. .put("client.transport.sniff", snf)//增加嗅探机制,找到ES集群
  15. .put("thread_pool.search.size", Integer.parseInt(poolSize))//增加线程池个数,暂时设为5
  16. .build();
  17. //配置信息Settings自定义
  18. transportClient = new PreBuiltTransportClient(esSetting);
  19. TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(clusterNode), Integer.valueOf(clusterPort));
  20. transportClient.addTransportAddresses(transportAddress);
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. System.out.println("elasticsearch TransportClient create error!!");
  24. }
  25. System.out.println("es客户端创建成功");
  26. return transportClient;
  27. }
  28. public static String scrollId = "";
  29. /**
  30. * 第一次查询的方式
  31. * @param client
  32. * @return
  33. */
  34. private Map<String,Object> my(TransportClient client){
  35. BoolQueryBuilder mustQuery = QueryBuilders.boolQuery();
  36. //设置查询条件
  37. mustQuery.must(QueryBuilders.matchQuery("sex","男"));
  38. mustQuery.must(QueryBuilders.matchQuery("city","杭州市"));
  39. SearchResponse rep = client.prepareSearch()
  40. .setIndices(index) // 索引
  41. .setTypes(type) //类型
  42. .setQuery(mustQuery)
  43. .setScroll(TimeValue.timeValueMinutes(2)) //设置游标有效期
  44. .setSize(100) //每页的大小
  45. .execute()
  46. .actionGet();
  47. Map<String,Object> m = new HashMap<String,Object>();
  48. m.put("scrollId",rep.getScrollId());//获取返回的 游标值
  49. m.put("id", (rep.getHits().getHits())[0].getId());
  50. return m;
  51. }
  52. private Map<String,Object> my2(String scrollId,TransportClient client){
  53. SearchResponse rep1 = client.prepareSearchScroll(scrollId) //设置游标
  54. .setScroll(TimeValue.timeValueMinutes(2)) //设置游标有效期
  55. .execute()
  56. .actionGet();
  57. Map<String,Object> m = new HashMap<String,Object>();
  58. m.put("scrollId",rep1.getScrollId());
  59. SearchHit[] s = rep1.getHits().getHits();
  60. if(s == null || s.length == 0){
  61. return null;
  62. }
  63. m.put("id", (rep1.getHits().getHits())[0].getId());
  64. return m;
  65. }
  66. public static void main(String[] args) {
  67. TestEs t = new TestEs();
  68. TransportClient client = t.transportClient();
  69. Map<String,Object> m1 = t.my(client);
  70. System.out.println("first:"+m1.get("id"));
  71. String s = m1.get("scrollId").toString();
  72. System.out.println("first:"+s);
  73. int i = 0;
  74. while (true){
  75. i++;
  76. Map<String,Object> m2 = t.my2(s,client);
  77. // 查询不到数据了,就表示查询完了
  78. if(m2 == null){
  79. break;
  80. }
  81. System.out.println("insert to mysql");
  82. }
  83. System.out.println("总次数:"+i);
  84. System.out.println("end");
  85. }
  86. }