美文网首页
Elasticsearch入门

Elasticsearch入门

作者: 离别刀 | 来源:发表于2018-06-26 17:04 被阅读0次

    小诗欣赏:
    小池
    宋代杨万里

    泉眼无声惜细流,树阴照水爱晴柔。
    小荷才露尖尖角,早有蜻蜓立上头。

    正文

    配置crul环境变量:
    E:\software\curl-7.60.0\I386

    622142330.png
    测试curl配置正确性
    curl http:///www.baidu.com
    返回:
    22142552.png

    查看所有索引

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

    health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    yellow open   website  tIG5OrIXR4W8MrivVZ87RA   5   1          2            0     10.1kb         10.1kb
    yellow open   megacorp 356W-zR3RAS9wmFGtemXOA   5   1          3            0     17.4kb         17.4kb
    

    查看节点

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

    ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
    127.0.0.1           29          49   9                          mdi       *      PoIa-5T
    

    查看集群健康程度

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

    epoch      timestamp cluster           status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
    1530061690 09:08:10  bruce-application yellow          1         1     10  10    0    0       10             0                  -                 50.0%
    

    查看版本

    curl http://localhost:9200/?pretty

    {
      "name" : "PoIa-5T",
      "cluster_name" : "bruce-application",
      "cluster_uuid" : "FfI8hS6cSaC7e67V2nejhA",
      "version" : {
        "number" : "6.3.0",
        "build_flavor" : "default",
        "build_type" : "zip",
        "build_hash" : "424e937",
        "build_date" : "2018-06-11T23:38:03.357887Z",
        "build_snapshot" : false,
        "lucene_version" : "7.3.1",
        "minimum_wire_compatibility_version" : "5.6.0",
        "minimum_index_compatibility_version" : "5.0.0"
      },
      "tagline" : "You Know, for Search"
    }
    

    文档操作

    1.新增文档:
    curl -H "Content-Type: application/json" -XPOST localhost:9200/megacorp/employee/1 -d "{"first_name":"Jane","last_name":"Smith","age":25,"about":"I like to collect rock albums","interests":["music"]}"
    或者
    curl -H "Content-Type: application/json" -XPUT localhost:9200/megacorp/employee/1 -d "{"first_name":"Jane","last_name":"Smith","age":25,"about":"I like to collect rock albums","interests":["music"]}"

    2.修改文档

    • curl -H "Content-Type: application/json" -XPUT localhost:9200/megacorp/employee/1 -d "{"first_name":"Jane","last_name":"Smith","age":25,"about":"I like to collect rock albums","interests":["music and pinpang"]}"
      如果更新想检查对应的id,然后再做操作有如下两种方法:

    PUT /website/blog/123?op_type=create
    { ... }

    PUT /website/blog/123/_create
    { ... }

    • 基于版本的更新

    内部版本更新
    PUT /website/blog/1?version=1
    {
    "title": "My first blog entry",
    "text": "Starting to get the hang of this..."
    }

    外部版本更新
    PUT /website/blog/2?version=5&version_type=external
    {
    "title": "My first external blog entry",
    "text": "Starting to get the hang of this..."
    }

    如果更新版本比当前版本低回报错如下:

    {
        "error": {
            "root_cause": [
                {
                    "type": "version_conflict_engine_exception",
                    "reason": "[blog][2]: version conflict, current version [10] is higher or equal to the one provided [1]",
                    "index_uuid": "tIG5OrIXR4W8MrivVZ87RA",
                    "shard": "2",
                    "index": "website"
                }
            ],
            "type": "version_conflict_engine_exception",
            "reason": "[blog][2]: version conflict, current version [10] is higher or equal to the one provided [1]",
            "index_uuid": "tIG5OrIXR4W8MrivVZ87RA",
            "shard": "2",
            "index": "website"
        },
        "status": 409
    }
    
    • 局部更新

    POST /website/blog/1/_update
    {
    "doc" : {
    "tags" : [ "testing" ],
    "views": 0
    }
    }
    查询该文档curl localhost:9200/website/blog/2

    {
        "_index": "website",
        "_type": "blog",
        "_id": "2",
        "_version": 13,
        "found": true,
        "_source": {
            "title": "My first blog entry",
            "text": "Starting to get the hang of this...",
            "views": 0,
            "tags": [
                "testing"
            ]
        }
    }
    

    4.检查文档是否存在
    curl -i -XHEAD http://localhost:9200/megacorp/employee/1
    返回200表示找到,404未找到

    5.查询文档

    {
      "_index" : "megacorp",
      "_type" : "employee",
      "_id" : "1",
      "_version" : 9,
      "found" : true,
      "_source" : {
        "first_name" : "Jane",
        "last_name" : "Smith",
        "age" : 25,
        "about" : "I like to collect rock albums",
        "interests" : [
          "music and pinpang"
        ]
      }
    }
    
    {
      "_index" : "megacorp",
      "_type" : "employee",
      "_id" : "1",
      "_version" : 9,
      "found" : true,
      "_source" : {
        "interests" : [
          "music and pinpang"
        ],
        "first_name" : "Jane"
      }
    }
    

    或者你只想得到_source字段而不要其他的元数据,你可以这样请求:
    curl http://localhost:9200/megacorp/employee/1/_source?pretty

    {
      "first_name" : "Jane",
      "last_name" : "Smith",
      "age" : 25,
      "about" : "I like to collect rock albums",
      "interests" : [
        "music and pinpang"
      ]
    }
    
    • _mget查询
      POST localhost:9200/website/blog/_mget
      {
      "ids": ["1","2"]
      }
      result:
    {
        "docs": [
            {
                "_index": "website",
                "_type": "blog",
                "_id": "1",
                "_version": 4,
                "found": true,
                "_source": {
                    "title": "My first blog entry",
                    "text": "Just trying this out..."
                }
            },
            {
                "_index": "website",
                "_type": "blog",
                "_id": "2",
                "_version": 13,
                "found": true,
                "_source": {
                    "title": "My first blog entry",
                    "text": "Starting to get the hang of this...",
                    "views": 0,
                    "tags": [
                        "testing"
                    ]
                }
            }
        ]
    }
    

    POST localhost:9200/website/blog/_mget
    {
    "docs" : [
    { "_id" : 2 }
    ]
    }
    result:

    {
        "docs": [
            {
                "_index": "website",
                "_type": "blog",
                "_id": "2",
                "_version": 13,
                "found": true,
                "_source": {
                    "title": "My first blog entry",
                    "text": "Starting to get the hang of this...",
                    "views": 0,
                    "tags": [
                        "testing"
                    ]
                }
            }
        ]
    }
    

    6.删除文档
    curl -XDELETE http://localhost:9200/megacorp/employee/1

    参考文档:https://es.xiaoleilu.com/

    相关文章

      网友评论

          本文标题:Elasticsearch入门

          本文链接:https://www.haomeiwen.com/subject/tjykyftx.html