---
title: "ElasticSearch入门"
author: "lijiejava"
date: "2017-11-02T13:50:01Z"
tags: []
categories: ["互联网"]
slug: "elasticsearch"
draft: false
lastmod: "2017-12-20T16:27:03Z"
id: 2398407
url: "https://lijiejava.iteye.com/blog/2398407"
---

<http://www.muxuanli.com/lmx/>

1.	下载zip 文件，解压  到E:\tools\elasticsearch\bin 目录下 打开 elasticsearch.bat 
http://localhost:9200

2.	安装 elasticsearch-head 插件
bin目录运行以下命令： plugin install mobz/elasticsearch-head

3.	安装 bigdesk 插件  plugin install lukas-vlcek/bigdesk 

https://www.elastic.co/guide/en/elasticsearch/plugins/2.3/installation.html

插件的作用：

elasticsearch-head:

bigdek：bigdesk是elasticsearch的一个集群监控工具，可以通过它来查看es集群的各种状态，如：cpu、内存使用情况，索引数据、搜索情况，http连接数等。

 ![](http://dl.iteye.com/upload/picture/pic/137537/13ed0449-8d9f-3a1c-887b-1522d9d55d2e.png)


```java
Elasticsearch把数据存储在一个或多个[color=red]索引[/color]上，每个索引包含各种类型的[color=red]文档[/color]！

Elasticsearch 使用文档的唯一标识符来计算文档应该被放到哪个分片中。索引请求发送到一个节点后，该节点会转发文档到持有相关分片的目标节点中。 
```



***增删改查操作***


```java


一. 创建： 

curl -XPUT "http://localhost:9200/blog/article/1" -d '
{
   "title":"About MaiMeng Tech",
   "content":"MaiMeng Tach Suzhou China & Very Good", 
   "tags":["web","2020","tech"]
}
'

返回：
{
    "_index": "blog",
    "_type": "article",
    "_id": "1",
    "_version": 1,
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}



二. 获取：

curl -XGET http://localhost:9200/blog/article/1

返回：
{"_index":"blog","_type":"article","_id":"1","_version":1,"found":true,"_source"
:{
   "title":"About MaiMeng Tech",
   "content":"MaiMeng Tach Suzhou China & Very Good",
   "tags":["web","2020","tech"]
}}



三. 更新：_update  注意：不需要发送整个文档，只需要发送改变的部分。更新时需要使用_source字段；如果更新时新增一个字段，使用upsert

curl -XPOST http://localhost:9200/blog/article/1/_update -d '{
   "script":"ctx._source.content = \"This is new content\""
}'
                  
报错：
{
    "error": {
        "root_cause": [
            {
                "type": "remote_transport_exception",
                "reason": "[gamora][127.0.0.1:9300][indices:data/write/update[s]]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "failed to execute script",
        "caused_by": {
            "type": "script_exception",
            "reason": "scripts of type [inline], operation [update] and lang [groovy] are disabled"
        }
    },
    "status": 400
}

http://www.open-open.com/lib/view/open1455238146511.html
 在最新版本的Elasticsearch中,基于安全考虑(如果用不到,请保持禁用),默认禁用了动态脚本功能.如果被禁用，则报上述错误。
 可以用以下方式完全开启动态脚本功能，在config/elasticsearch.yml文件,在最后添加以下代码： 
	script.inline: on 
	script.indexed: on  
	script.file: on

 配置后，重启Elasticsearch。

 执行结果： {"_index":"blog","_type":"article","_id":"1","_version":4,"_shards":{"total":2,"successful":1,"failed":0}}


 四. 删除

 curl -XDELETE http://localhost:9200/blog/article/1


```


***
MySQL数据同步到ES***


```java





curl -XDELETE localhost:9200/estest
// curl -XDELETE localhost:9200/_river/estest


创建索引 estest 
curl -XPUT http://localhost:9200/estest/

创建表与索引映射
curl -XPUT 'http://localhost:9200/estest/employee/_mapping' -d '
{
   "employee":{
       "_all": { "enabled": false}, 
       "_source" : {"enabled" : true}, 
       "properties" : { 
          "id" : {"type" : "long","index" : "not_analyzed"},  
	  "name" : {"type" : "string","index" : "no"},
	  "job" : {"type" : "string","index" : "no"}
       }   
   }
}'

运行river同步数据
curl -XPUT 'http://localhost:9200/_river/estest/_meta' -d '
{
    "type": "jdbc",
    "jdbc": {
        "driver": "com.mysql.jdbc.Driver",
        "url": "jdbc:mysql://localhost:3306/es",
        "user": "root",
        "password": "123456",
        "sql": "select id, id as \"_id\",name,job from t_employee",
        "index": "estest",
        "type": "employee" 
    }
}'
 

 "jdbc": {
        "driver": "com.mysql.jdbc.Driver",
        "url": "jdbc:mysql://localhost:3306/es",
        "user": "root",
        "password": "123456",
        "sql": "select id, id as as \"_id\",name,job from employee",
        "index": "estest",
        "type": "employee",
        "bulk_size": 100,
        "max_bulk_requests": 30,
        "bulk_timeout": "10s",
        "flush_interval": "5s",
        "schedule": "0 0-59 0-23 ? * *"
    }

 



 curl localhost:9200/estest/_count?pretty=true


```