elasticSearch的部署和使用

Posted lori

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticSearch的部署和使用相关的知识,希望对你有一定的参考价值。

部署服务

docker run启动elastic服务

docker pull elasticsearch:6.7.2

docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:6.7.2

docker-compose.yml的方式部署

version: '2.2'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.7.2
    container_name: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - esnet
  elasticsearch2:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.7.2
    container_name: elasticsearch2
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=elasticsearch"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata2:/usr/share/elasticsearch/data
    networks:
      - esnet

volumes:
  esdata1:
    driver: local
  esdata2:
    driver: local

networks:
  esnet:

elastic常用命令

查看集群是否健康

  curl 'localhost:9200/_cat/health?v'

获取集群的节点列表

  curl 'localhost:9200/_cat/nodes?v'

列出所有索引

curl 'localhost:9200/_cat/indices?v'

建立名为customer的索引

 curl -XPUT 'localhost:9200/customer?pretty'

向索引customer,类型为external,添加数据

  curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
  
           "name": "John Doe"
  '

获取数据

获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观。

   curl -XGET 'localhost:9200/customer/external/1?pretty'

删除索引 DELETE

curl -XDELETE 'localhost:9200/customer?pretty'

修改数据,如果不存在,就新增

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
  

    "name": "John Doe"

  '

更新数据

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
  

    "doc":  "name": "Jane Doe", "age": 20 
  '

删除一条记录数据

curl -XDELETE 'localhost:9200/customer/external/2?pretty'

以上是关于elasticSearch的部署和使用的主要内容,如果未能解决你的问题,请参考以下文章

ElasticSearch第一天

ElasticSearch Client详解

ElasticSearch的备份迁移方案

从查询重写角度理解elasticsearch的高亮原理

ELK 实验elasticsearch集群搭建

ElasticSearchRepository和ElasticSearchTemplate的使用