常用操作
创建文档
PUT /indexName/indexType/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
检索文档
GET /_search //all
GET /megacorp/employee/1 //id
GET /megacorp/employee/_search //全部
GET /megacorp/employee/_search?q=last_name:Smith
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
//搜索姓氏为 Smith 的员工,且年龄大于 30 的
GET /megacorp/employee/_search
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
//仅匹配同时包含 “rock” 和 “climbing” ,并且 二者以短语 “rock climbing” 的形式紧挨着的记录
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
//挖掘员工中最受欢迎的兴趣爱好
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
//检查查询语句不合法的信息,或为何匹配/为何不匹配
GET /indexName/indexType/_validate/query?explain
{
"query": {
"tweet" : {
"match" : "really powerful"
}
}
}
//查询目前所有索引
curl -XGET 127.0.0.1:9200/_cat/indices
*
删除文档
DELETE /website/blog/123
更新文档
//部分更新,增加字段 tags 和 views
POST /website/blog/1/_update
{
"doc" : {
"tags" : [ "testing" ],
"views": 0
}
}
//使用脚本来增加博客文章中 views 的数量
POST /website/blog/1/_update
{
"script" : "ctx._source.views+=1"
}
并发更新控制
https://www.elastic.co/guide/cn/elasticsearch/guide/current/optimistic-concurrency-control.html
创建索引
PUT /blogs
{
"settings" : {
"number_of_shards" : 3, //主分片
"number_of_replicas" : 1 //副本分片
}
}
删除索引
DELETE /indexName
DELETE /_all //删除所有索引,可以配置不允许这样操作
创建索引指定映射
PUT /indexName
{
"mappings": {
"indexType" : {
"properties" : {
"tweet" : {
"type" : "string",
"analyzer": "english"
},
"date" : {
"type" : "date"
},
"name" : {
"type" : "string"
},
"user_id" : {
"type" : "long"
}
}
}
}
}
增加mapping
PUT /indexName/_mapping/indexType
{
"properties" : {
"tag" : {
"type" : "string",
"index": "not_analyzed"
}
}
}
配置索引模版
PUT /_template/模版名称
{
"template": "logstash-*",
"order": 1, //越大优先级越高
"settings": {
"number_of_shards": 1 //限制主分片数量为 1
},
"mappings": {
"_default_" : {
"properties" : {
"ip" :{
"type":"ip"
}
}
}
},
"aliases": {
"last_3_months": {} //添加这个索引至 last_3_months 别名中
}
}
查询mapping
GET /indexName/_mapping/indexType //查询指定索引的mapping
测试mapping
GET /indexName/_analyze
{
"field": "tweet",
"text": "Black-cats"
}
默认映射
当你索引一个包含新域的文档—之前未曾出现-- Elasticsearch 会使用动态映射
https://www.elastic.co/guide/cn/elasticsearch/guide/current/mapping-intro.html
生产建议
重要配置修改(elasticsearch.yml 文件中)
//指定集群及机器名称
cluster.name: elasticsearch_production
node.name: elasticsearch_005_data
//数据路径
path.data: /path/to/data1,/path/to/data2
path.logs: /path/to/logs
path.plugins: /path/to/plugins
//最小主节点数
discovery.zen.minimum_master_nodes: 2 //( master 候选节点个数 / 2) + 1
//集群恢复方面的配置,防止因节点重启等情况发生数据移动
gateway.expected_nodes: 10
gateway.recover_after_time: 5m
//使用单播代替组播(默认)???
discovery.zen.ping.unicast.hosts: ["host1", "host2:port"]
//禁用 swap
bootstrap.mlockall: true
线程池的线程数最多设置成核心数的2倍
堆大小默认是1G,偏小
./bin/elasticsearch -Xmx10g -Xms10g //启动时调整
export ES_HEAP_SIZE=10g //设置环境变量留下一半内存给Lucene,它占用的是堆外内存
Elasticsearch内存也不要超过32G设置好文件描述符的大小
可以在/etc/sysctl.conf 通过修改 vm.max_map_count 设置它
其他需要注意的配置(elasticsearch.yml 文件中)
//是否禁止自动创建索引
action.auto_create_index: false