elastisearch基础

1.安装
jdk-8u45-linux-x64.tar.gz
elasticsearch-1.7.2.tar.gz

/etc/profile
export JAVA_HOME=/data/program/jdk1.8
export JRE_HOME=/data/program/jdk1.8/jre
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar


tar -xzvf elasticsearch-1.7.2.tar.gz
cd elasticsearch-1.7.2/bin
./elasticsearch -d
内存设置
vim elasticsearch.in.sh
if [ "x$ES_MIN_MEM" = "x" ]; then
    ES_MIN_MEM=256m
fi
if [ "x$ES_MAX_MEM" = "x" ]; then
    ES_MAX_MEM=512m
fi

2.插件安装
./plugin --install mobz/elasticsearch-head
./plugin --install lukas-vlcek/bigdesk/2.5.0
./plugin --install elasticsearch/marvel/latest
网址访问
http://112.124.120.242:9200/_plugin/head
http://112.124.120.242:9200/_plugin/bigdesk
http://112.124.120.242:9200/_plugin/marvel


3.通过插件marvel去调用api接口进行增删改查
#GET _search
#{
#  "query": {
#    "match_all": {}
#  }
#}
PUT http://112.124.120.242:9200/library/
{
  "settings":{
    "index":{
            "number_of_shards":5,
            "number_of_replicas":1
    }
  }
  
}

PUT http://112.124.120.242:9200/library2/
{
  "settings":{
  "index":{
          "number_of_shards":5,
          "number_of_replicas":1
  }
 }
}
GET /library/_settings
GET /library,library2/_settings
GET /_all/_settings


PUT /library/books/1
{
  "title":"eddy books",
  "name":{
    "first":"eddy",
    "last":"tom"
  },
  "publish_date":"2016-01-09",
  "price":"59.99"
}

GET /library/books/1
GET /library/books/1?_source=title,price

POST /library/books/1/_update
{
  "doc":{
    "price":69.99
  }
}
DELETE /library/books/1
DELETE /library/books
DELETE /library



4.批量获取mget
# 同时获取多个文档信息
# 例子:获取index:bank, type:bank_account下面
# ID为1,2,3,4,15,6,28 的文档信息

GET /bank/bank_account/1
GET /bank/bank_account/2
GET /shakespeare/line/3
GET /bank/bank_account/4
GET /shakespeare/line/5


# 数组[]
GET /_mget
{
   "docs" : [
      {
         "_index" : "bank",
         "_type" :  "bank_account",
         "_id" :    1
      },
      {
         "_index" : "bank",
         "_type" :  "bank_account",
         "_id" :    2
      },
      {
         "_index" : "shakespeare",
         "_type" :  "line",
         "_id" :    3
      },
      {
         "_index" : "shakespeare",
         "_type" :  "line",
         "_id" :    4
      },
      {
         "_index" : "shakespeare",
         "_type" :  "line",
         "_id" :    15
      },
      {
         "_index" : "shakespeare",
         "_type" :  "line",
         "_id" :    6
      },
      {
         "_index" : "shakespeare",
         "_type" :  "line",
         "_id" :    28
      }
   ]
}

# 也可以指定_source字段,获取你想要的
GET /_mget
{
   "docs" : [
      {
         "_index" : "shakespeare",
         "_type" :  "line",
         "_id" :    6,
         "_source": "play_name"
      },
      {
         "_index" : "shakespeare",
         "_type" :  "line",
         "_id" :    28,
         "_source": "play_name"
         
      }
   ]
}

# 指定多个_source字段,数组的形式[]
GET /_mget
{
   "docs" : [
      {
         "_index" : "shakespeare",
         "_type" :  "line",
         "_id" :    6
      },
      {
         "_index" : "shakespeare",
         "_type" :  "line",
         "_id" :    28,
         "_source": ["play_name","speaker","text_entry"]
         
      }
   ]
}


# 获取相同index相同type下不同ID的文档
GET /shakespeare/line/_mget
{
  "docs" : [
      { "_id" : 6 },
      { "_type" : "line", "_id" :   28 }
   ]
}

# 可以这样简便的写
GET /shakespeare/line/_mget
{
   "ids" : [ "6", "28" ]
}

GET /shakespeare/line/_mget
{
   "ids" : [ "1", "2", "3", "4", "5", "6", "7" ]
}

5.批量操作bulk
默认是以换行为一条数据解析的
所以不能用已下格式
{"delete":
	{
	
		"_index":"library",
		"_type":"books",
		"_id":"1"
		
	}

}
只能是以下格式
{"delete":{"_index":"library","_type":"books","_id":"1"}}

#多重模式
POST /library/books/_bulk
{ "index":  { "_id": 1}}
{ "title":"Elasticsearch: The Definitive Guide","price":5 }
{ "index":  { "_id": 2}}
{ "title":"The Elasticsearch cookbook","price":15 }
{ "index":  { "_id": 3}}
{ "title":"Elasticsearch Blueprints","price":9 }
{ "index":  { "_id": 4}}
{ "title":"Thinking in Python","price":22 }
{ "index":  { "_id": 5}}
{ "title":"Thinking in Java","price":7 }
GET /library/
GET /library/books/_mget
{
   "ids" : [ "1", "2" , "3", "4", "5"]
}
# 当然还可以有delete ,update等操作
# 注意delete下面没有具体的request body
POST /library/books/_bulk
{ "delete": { "_index": "library", "_type": "books", "_id": "1" }}
{ "create": { "_index": "music", "_type": "classical", "_id": "1" }}
{ "title": "Ave Verum Corpus" }
{ "index":  { "_index": "music", "_type": "classical" }}
{ "title": "Litaniac de Venerabili Altaris Sacromento" }
{ "update": { "_index": "library", "_type": "books", "_id": "2"} }
{ "doc" : {"price" : "18"} }
GET /library/books/1
GET /library/books/_mget
{
   "ids" : [ "1", "2" , "3", "4", "5"]
}
bulk处理文档大小的最佳值
加载每个节点的RAM中在实际应用中逐次提升文档数目

6.Version版本控制
# Version版本控制
#
#内部版本控制
PUT /library/books/1
{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"

  },
  "publish_date":"2015-02-06",
  "price":"59.99"

}


GET /library/books/1


POST /library/books/1/_update
{
  "doc": {
     "price" : 10
  }
}


POST /library/books/1/_update?version=3
{
  "doc": {
     "price" : 15
  }
}




# 外部版本控制机制
#自己手动定义版本号
#但是版本号不能冲突
#版本号大于内部版本号即可
#版本号必须是整数
PUT /library/books/1?version=5&version_type=external
{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"

  },
  "publish_date":"2015-02-06",
  "price":"20"

}

PUT /library/books/1?version=6&version_type=external
{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"

  },
  "publish_date":"2015-02-06",
  "price":"25"

}


GET /library/books/1

7.映射
# 建立映射
POST /library
{
    "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1
    },
    "mappings": {
        "books": {
            "properties": {
                "title": { "type": "string"},
                "name": { "type": "string", "index": "not_analyzed"},
                "publish_date": {"type": "date", "index": "not_analyzed"},
                "price": {"type": "double"},
                "number": {"type": "integer"}
                }
            }
    }
}

# 动态映射
PUT /library
{
       "mappings": {
        "books": {
            "dynamic":  "strict",
            "properties": {
                "title": { "type": "string" },
                "name": { "type": "string", "index": "not_analyzed"},
                "publish_date": {"type": "date", "index": "not_analyzed"},
                "price": {"type": "double"},
                "number": {
                  "type":   "object",
                  "dynamic":  true
                  }
              }
          }
    }
   
}

GET /library/

# 获取某个索引的映射信息
GET /library/_mapping

GET /bank/_mapping/

# 获取某个索引下某个type的映射信息
GET /library/_mapping/books

# 获取这个集群内所有的映射信息
GET /_all/_mapping

# 获取这个集群内某两个或多个type的映射信息
GET /_all/_mapping/books,bank_account


# 更新修改Mapping映射
# 很遗憾,mapping一旦建立,就不能修改现有的字段映射
# 如果要推倒现有的映射,你得重新建立一个索引,然后重新定义映射
# 然后把之前索引里的数据导入到新建立的索引里
# ---------具体的方法----------
# 1. 给现有的索引定义一个别名,并且把现有的索引指向这个别名,运行步骤2
# 2. 运行 : PUT /现有索引/_alias/别名A
# 3. 新创建一个索引,定义好最新的映射
# 4. 将别名指向新的索引,并且取消之前索引的指向,运行步骤5
# 5. 运行: POST /_aliases
#  {
#    "actions": [
#        { "remove": { "index": "现有索引名", "alias": "别名A" }},
#        { "add":    { "index": "新建索引名", "alias": "别名A" }}
#    ]
#  }
#
# 注:通过这几个步骤就实现了索引的平滑过度,并且是零停机的
#


#删除映射
DELETE /library/books

DELETE /library/books/_mapping

DELETE /library/_mapping/books,bank

你可能感兴趣的:(elastisearch基础)