elasticsearch 索引的创建与使用

一、查看集群信息以及索引监控

GET /_cat/health

Get /{index}/_cat/_settings

GET /index1,index2/_segments //n查看索引段信息

二、创建索引

//创建索引 ik分词器 多重字段
PUT /my_index10

{

  "settings": {

    "number_of_shards": "3",

    "number_of_replicas": "1",

    "analysis": {

      "analyzer": {

        "default": {

          "tokenizer": "ik_smart",

          "filter": [

            "synonym"

          ]

        }

      },

      "filter": {

        "synonym": {

          "type": "synonym",

          "synonyms_path": "analysis/synonym.txt"

        }

    }

    }

  },

"mappings": {

    "_doc": {

      "properties": {

          "@timestamp": {

            "type": "date"

          },

          "beat": {

            "properties": {

              "hostname": {

                "type": "text",

                "fields": {

                  "keyword": {

                    "type": "keyword",

                    "ignore_above": 256

                  }

                }

              },

              "name": {

                "type": "text",

                "fields": {

                  "keyword": {

                    "type": "keyword",

                    "ignore_above": 256

                  }

                }

              },

              "version": {

                "type": "text",

                "fields": {

                  "keyword": {

                    "type": "keyword",

                    "ignore_above": 256

                  }

                }

              }

            }

          },

          "message": {

            "type": "text",

            "fields": {

              "keyword": {

                "type": "keyword",

                "ignore_above": 256

              }

            }

          },

          "offset": {

            "type": "long"

          },

          "prospector": {

            "properties": {

              "type": {

                "type": "text",

                "fields": {

                  "keyword": {

                    "type": "keyword",

                    "ignore_above": 256

                  }

                }

              }

            }

          },

          "source": {

            "type": "text",

            "fields": {

              "keyword": {

                "type": "keyword",

                "ignore_above": 256

              }

            }

          }

      }

    }

  }

}

三、文档操作


3.1 新建


//id指定

PUT twitter/_doc/1

{"id": 1, "user" : "kimchy", "post_date": "2009-11-15T14:12:12", "message" : "trying out Elasticsearch"}

//id自动生成

POST twitter/_doc/ {     "id": 1,     "user" : "kimchy",     "post_date" : "2009-11-15T14:12:12",     "message" : "trying out Elasticsearch" }

3.2 根据id获取

HEAD twitter/_doc/11      //11是_id

GET twitter/_doc/1         //1是_id

GET twitter/_doc/1?_source=false

GET /twitter/_mget {  "docs" : [   {   "_type" : "_doc",    "_id" : "1" },   { "_type" : "_doc",     "_id" : "2"  }] }

GET /twitter/_doc/_mget { "ids" : ["1", "2"] }

3.3 删除

DELETE twitter/_doc/1  //指定_id删除

DELETE twitter/_doc/1?version=1  //使用版本号控制

POST twitter/_delete_by_query { "query": { "match": { "message": "some message" }}}  //查询删除

3.4 修改

指定文档id进行修改

PUT twitter/_doc/1 { "id": 1,"user" : "kimchy", "post_date": "2009-11-15T14:12:12", "message" : "trying out Elasticsearch"}

// 乐观锁并发更新控制

PUT twitter/_doc/1?version=1 { "id": 1,  "user" : "kimchy", "post_date" : "2009-11-15T14:12:12","message" : "trying out Elasticsearch" }
//


POST uptest/_doc/1/_update

{

   "script" : {

       "source": "ctx._source.counter += params.count",

       "lang":"painless",

       "params" : {

            "count" : 4

       }

   }

}

POST twitter/_update_by_query { "script": { "source": "ctx._source.likes++", "lang": "painless"   }, "query": { "term": { "user": "kimchy"  }  } } 

脚本说明:painless是es内置的一种脚本语言,ctx执行上下文对象(通过它还可访问_index, _type, _id, _version, _routing and _now (the current timestamp) ),params是参数集合

说明:脚本更新要求索引的_source 字段是启用的。更新执行流程:                                                                                                           1、获取到原文档

 2 、通过_source字段的原始数据,执行脚本修改。

 3、删除原索引文档

  4、索引修改后的文档 它只是降低了一些网络往返,并减少了get和索引之间版本冲突的可能性。 


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,323评论 19 139
  • width: 65%;border: 1px solid #ddd;outline: 1300px solid #...
    邵胜奥阅读 10,328评论 0 1
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,143评论 0 13
  • 义心
    贝贝150阅读 1,014评论 0 0
  • 这之前本来打算在这发表心情把自己的感悟记下什么来着,现在感觉感悟很少了。不过生活依然继续!等待新的涟漪
    遗忘是福阅读 9,227评论 0 2