ElasticSearch入门教程

2018-01-19  本文已影响15人  FlyXhc

什么是ElasticSearch

可应用场景

一线公司实际应用场景

安装

Windows系统下Elasticsearch安装与Elasticsearch-head插件安装

分布式安装

  1. 修改D:\Program Files\elasticsearch-5.5.2\config\elasticsearch.yml文件
    在文件末尾加入:
cluster.name: fly #集群名
node.name: master #节点名
node.master: true #是否为主节点
network.host: 127.0.0.1 #ip地址
  1. 将elasticsearch-5.5.2.zip解压为elasticsearch-1,修改elasticsearch-1\config\elasticsearch.yml文件,在文件末尾加入:
http.cors.enabled: true  #跨域
http.cors.allow-origin: "*"  #跨域
cluster.name: fly #集群名
node.name: slave1 #随从节点名
network.host: 127.0.0.1 #IP地址
http.port: 8200 #端口号
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]  #设置寻找主节点
  1. 将elasticsearch-5.5.2.zip解压为elasticsearch-2,修改elasticsearch-2\config\elasticsearch.yml文件,在文件末尾加入:
http.cors.enabled: true  #跨域
http.cors.allow-origin: "*"  #跨域
cluster.name: fly #集群名
node.name: slave2 #随从节点名
network.host: 127.0.0.1 #IP地址
http.port: 7200 #端口号
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]  #设置寻找主节点

访问localhost:9100如下图:


localhost:9100

基础概念

索引创建

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

如下图:


image

点击send,返回内容:

{
    "acknowledged": true,
    "shards_acknowledged": true
}

访问127.0.0.1:9100,将会看到以添加people节点

文档插入

文档插入又分为:

{
    "name":"石头",
    "age":"28",
    "countory":"Chian",
    "date":"1990-11-21"
}

点击send,返回内容:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}
{
    "name":"疯狂的石头",
    "age":"29",
    "countory":"Chian",
    "date":"1990-11-22"
}

点击send,返回内容

{
    "_index": "people",
    "_type": "man",
    "_id": "AWEc_pyp21iCO45paMTd",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

刷新localhost:9100,看到people索引的docs的数量为2

修改文档

{
    "doc":{
        "age":30
    }
}

访问localhost:9100,点击数据浏览,刷新,将会看到age已经变为30了

{
    "script":{
        "lang":"painless",
        "inline":"ctx._source.age += 10"
    }
}

访问localhost:9100,点击数据浏览,刷新,将会看到age已经变为40了

文档查询

GET localhost:9200/people/man/1

返回结果

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 3,
    "found": true,
    "_source": {
        "name": "石头",
        "age": 40,
        "countory": "Chian",
        "date": "1990-11-21"
    }
}
POST 127.0.0.1:9200/people/man/_search
body输入内容:
{
    "query":{
        "match_all":{}
    }
}

返回结果

{
    "took": 7, #耗时
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "people",
                "_type": "man",
                "_id": "AWEc_pyp21iCO45paMTd",
                "_score": 1,
                "_source": {
                    "name": "疯狂的石头",
                    "age": "29",
                    "countory": "Chian",
                    "date": "1990-11-22"
                }
            },
            {
                "_index": "people",
                "_type": "man",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "name": "石头",
                    "age": 40,
                    "countory": "Chian",
                    "date": "1990-11-21"
                }
            }
        ]
    }
}

指定文档起始位置与大小查询

POST 127.0.0.1:9200/people/man/_search
body输入内容:
{
    "query":{
        "match_all":{}
    },
    "from": 1,
    "size": 1
}

从第一个文档查询,查询大小为1,返回结果:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "people",
                "_type": "man",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "name": "石头",
                    "age": 40,
                    "countory": "Chian",
                    "date": "1990-11-21"
                }
            }
        ]
    }
}

按条件查询

POST 127.0.0.1:9200/people/man/_search
body输入内容:
{
    "query":{
        "match":{
            "name":"石头"
        }
    },
    "sort":{
        "age":{
            "order":"desc"
        }
    }
}

sort:排序
返回内容:

{
    "took": 148,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": null,
        "hits": [
            {
                "_index": "people",
                "_type": "man",
                "_id": "1",
                "_score": null,
                "_source": {
                    "name": "石头",
                    "age": 40,
                    "countory": "Chian",
                    "date": "1990-11-21"
                },
                "sort": [
                    40
                ]
            },
            {
                "_index": "people",
                "_type": "man",
                "_id": "AWEc_pyp21iCO45paMTd",
                "_score": null,
                "_source": {
                    "name": "疯狂的石头",
                    "age": "29",
                    "countory": "Chian",
                    "date": "1990-11-22"
                },
                "sort": [
                    29
                ]
            }
        ]
    }
}
上一篇 下一篇

猜你喜欢

热点阅读