ElasticSearch学习笔记

  本文是笔者学习ElasticSearch的笔记,ElasticSearch教程网址为http://www.imooc.com/learn/889

  ElasticSearch是基于Apache Lucene构建的开源搜索引擎,可以轻松地横向扩展,可支持PB级的结构化或者非结构化数据处理。

  ElasticSearch应用场景:

    海量数据分析引擎

    站内搜索引擎

    数据仓库

  ElasticSearch版本历史:1.x ——> 2.x ——> 5.x

  版本选择:

    2.x 稳定、成熟、插件多,但是官方不在维护

    5.x 效率高,官方在维护

    注:在我写这篇随笔时ElasticSearch已经更新到6.x了

  ElasticSearch安装:

    由于教程作者使用的是Mac,我使用的是Windows。所以在Windows上安装ElasticSearch是我在网上查找资料完成的。具体步骤如下:

    首先,从官网下载ES,官网地址:https://www.elastic.co/。下载zip

    第二,将下载的zip解压到响应位置。

    第三,cmd进入解压有的文件bin中,执行elasticsearch-service install 命令,等待安装结束。

    第四,cmd进入解压有的文件bin中,执行elasticsearch-service start 命令,启动ES。

    第五,cmd进入解压有的文件bin中,执行elasticsearch-service stop 命令,停止ES。

  ElasticSearch-head插件安装:

    由于教程作者使用的是Mac,我使用的是Windows。所以在Windows上安装ElasticSearch-head是我在网上查找资料完成的。地址如下:

    http://blog.csdn.net/u012270682/article/details/72934270

  ES基础概念

    集群:一个或多个ES节点构成;

    节点:一个节点只能从属于一个集群;

    索引:含有相同属性的文档集合;

    类型:索引可以定义一个或者多个类型;

    文档:可以被索引的基本数据单位,一个文档必须属于一个类型;

    索引、类型、文档可以看做是数据库、表、记录。

    分片:每个索引都有多个分片,每个分片是一个Lucene索引;

    备份:拷贝一份分片就是完成了分片的备份。

  ES基本用法

    ES的API组成:http://<ip>:<port>/<索引>/<类型>/<文档id>

    ES常用HTTP动词:GET/PUT/POST/DELETE

    1. 创建索引

      非结构化索引、结构化索引

      URL:http://127.0.0.1:9200/people

      动词:PUT

{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":1
    } ,
    "mapping":{
        "man":{
            "properties":{
                "name":{
                    "type":"text"
                },
                "country":{
                    "type":"keyword"
                },
                "age":{
                    "type":"integer"
                },
                "data":{
                    "type":"data",
                    "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
                }
            }
        }
    }
}

  

    2. 插入

      指定文档id插入

      URL:http://127.0.0.1:9200/people/man/1

      动作:PUT

{
    "name":"aston",
    "country":"中国",
    "age":27,
    "data":"1990-01-01"
}

    自动产生文档id插入

    URL:http://127.0.0.1:9200/people/man/

    动作:POST

{
    "name":"aston1",
    "country":"中国",
    "age":10,
    "data":"1987-01-01"
}

  

    3. 修改

      直接修改文档

      URL:http://127.0.0.1:9200/people/man/1/_update

      动词:POST

{
    "doc":{
        "name":"who is aston?"
    }
}

      脚本修改文档

      URL:http://127.0.0.1:9200/people/man/1/_update

      动词:POST

{
    "script":{
        "lang":"painless",
        "inline":"ctx._source.age = params.age",
        "params":{
            "age":100
        }
    }
}

    

    4. 删除

      删除文档

      URL:http://127.0.0.1:9200/people/man/1

      动作:DELETE

      删除索引

      URL:http://127.0.0.1:9200/book

      动作:DELETE

    5. 查询

      简单查询

      URL:http://127.0.0.1:9200/people/man/1

      动作:GET

      条件查询

      URL:http://127.0.0.1:9200/people/_search

      动作:POST

{
    "query":{
        "match_all":{
            
        }
    },
    "from":1,
    "size":1
}
{
    "query":{
        "match":{
            "name":"aston"
        }
    },
    "sort":[
        {"age":{"order":"desc"}}
    ]
}

      聚合查询

      URL:http://127.0.0.1:9200/people/_search

      动作:POST

{
    "aggs":{
        "group_by_word_count":{
            "terms":{
                "field":"age"
            }
        }
    }
}

  高级查询

原文地址:https://www.cnblogs.com/aston/p/7845140.html