Elasticsearch 笔记 (version:elasti

2017-11-19  本文已影响0人  SamHxm

概念

Index

类似于关系型数据库中的database。索引只是一个逻辑上的空间,物理上是分为多个文件来管理的。命名必须全小写。

Type

类似于关系型数据库中的table,根据用户需求每个index中可以新建任意数量的type。

Document

类似于关系型数据库中的row。每个Document是一个json格式的文本。

Mapping

更像是一个用来定义每个字段类型的语义规范在mysql中类似sql语句,在ES中经过包装后,都被封装为友好的Restful风格的接口进行操作。这一点也是为什么开发人员更愿意使用ES或者compass这样的框架而不是直接使用Lucene的一个原因。

安装

OS参数调整

vi /etc/security/limits.conf

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

vi /etc/sysctl.conf

vm.max_map_count=655360

ES参数调整

elasticsearch-2.4.5/config/jvm.options

-Xms512m
-Xmx512m

elasticsearch-2.4.5/config/elasticsearch.yml

network.host: 0.0.0.0
http.port: 9200

elasticsearch-analysis-ik安装

下载版本:elasticsearch-analysis-ik-1.10.6.zip

下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

上传elasticsearch-analysis-ik-1.10.6.zip至elasticsearch-2.4.5/plugins/analysis-ik(需先创建analysis-ik目录)

解压并修改plugin-descriptor.properties

java.version=1.8
elasticsearch.version=2.4.5

启动

elasticsearch-2.4.5/bin/elasticsearch

验证

GET http://{host}:9200

常见REST API 操作

查看集群状态

GET http://{host}:9200/_cat/health?v

查看所有节点状态

GET http://{host}:9200/_cat/nodes?v

创建索引

PUT http://{host}:9200/test_index

创建索引并指定配置

PUT http://{host}:9200/test_index

{
  "settings": {
     "refresh_interval": "1s",
     "number_of_shards" :   5,
     "number_of_replicas" : 0
  },
  "mappings": {
    "test_type": {
      "dynamic": false,
      "properties": {
        "name": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "ik"
        },
        "about": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "ik"
        },
        "interests": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "ik"
        }
      }
    }
  }
}

修改索引设置

PUT http://10.16.30.37:9200/cpinfo_index/_settings

{
    "settings": {
         "number_of_replicas" : 1
    }
}

查看所有索引

GET http://{host}:9200/_cat/indices?v

查看索引mapping

GET http://{host}:9200/{index_name}/_mapping?pretty

向索引添加新type

POST http://{host}:9200/{index_name}/{type_name}

{
  "mappings": {
    "test_type2": {
      "dynamic": false,
      "properties": {
        "name": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "standard"
        },
        "about": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "standard"
        }
      }
    }
  }
}

删除索引

DELETE http://{host}:9200/{index_name}

添加数据

PUT http://{host}:9200/{index_name}/{type_name}/{id}

{
    "name" : "Scott",
    "about" :  "Smith",
    "location": {
    "lat":     40.722,
    "lon":    -73.989
  }
}

批量添加数据(方法一)

curl -XPOST localhost:9200/_bulk --data-binary @data.json

data.json内容:
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "1" } }
{ "name" : "scott1", "about" : "work1",  "interests" : "read1"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "2" } }
{ "name" : "scott2", "about" : "work2",  "interests" : "read2"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "3" } }
{ "name" : "scott3", "about" : "work3",  "interests" : "read3"}

批量添加数据(方法二)

curl -XPOST http://127.0.0.1:9200/_bulk -d '
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "4" } }
{ "name" : "scott1", "about" : "work1",  "interests" : "read1"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "5" } }
{ "name" : "scott2", "about" : "work2",  "interests" : "read2"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "6" } }
{ "name" : "scott3", "about" : "work3",  "interests" : "read3"}
'

查询单条数据

GET http://{host}:9200/{index_name}/{type_name}/{id}?pretty

条件查询

curl -XPOST 'http://{host}:9200/{index_name}/{type_name}/_search?pretty' -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "interests": "阅读" } },
        { "match": { "age": 97 } }
      ]
    }
  },
  "sort": { "age": { "order": "desc" } },
  "from": 0,
  "size": 1000
}'

查看token

POST http://{host}9200/{index_name}/_analyze?analyzer=chinese

张三

查询解析

POST http://{host}:9200/{index_name}/{type_name}/_validate/query?explain

{
  "query": {
    "multi_match": {
      "query": "学习",
       "fields": ["name", "about"]
    }
  }
}
上一篇 下一篇

猜你喜欢

热点阅读