ElasticSearch CRUD实例

# 给索引添加别名
POST http://127.0.0.1:9200/_aliases
{
   "actions":[{"add":{"index":"twitter","alias":"alias1"}}]
}

# 删除索引的别名
POST http://127.0.0.1:9200/_aliases
{
   "actions":[{"remove":{"index":"twitter","alias":"alias1"}}]
}

#注意:别名没有修改的语法,当修改别名时先删除别名,在添加别名。
POST http://127.0.0.1:9200/_aliases
{
 "actions":[
   {
     "remove":{ 
       "index" : "secisland",
       "alias" : "alias1"
     }
   },
   {
       "add":{ 
       "index" : "secisland",
       "alias" : "alias2"
     }
  }
  ]
}

# 一个别名可以关联多个索引
POST http://127.0.0.1:9200/_aliases
{
    "actions":[
        {"add":{"index":"lancer_test","alias":"test1"}},
        {"add":{"index":"curry_test","alias":"test1"}},
        {"add":{"index":"albert_test","alias":"test1"}}
    ]
}
# 或者用 通配符 设置
POST http://127.0.0.1:9200/_aliases
{
    "actions":[
        {"add":{"index":"*test","alias":"test2"}}
     ]
}

# 创建一个带过滤的别名,注意确保映射中已经存在要过滤的字段。该别名索引中就只保留指定的过滤字段的文档了
POST http://127.0.0.1:9200/_aliases
{
  "actions":[
      { 
        "add":{
           "index":"secisland",
           "alias":"alias1",
           "filter":{ "term":{"name": "lancer"} }
        }
      }
   ]
}

# 创建一个关联路由的别名。
POST http://127.0.0.1:9200/_aliases
{
  "actions":[
      { 
        "add":{
           "index" : "secisland",
           "alias" : "alias1",
           "filter" : { "term":{"name": "lancer"} },
           "routing" : "1"
        }
      }
   ]
}

# 创建一个关联不同路由的别名, 其中搜索路由可以指定多个值,索引路由只能指定一个值。
POST http://127.0.0.1:9200/_aliases
{
  "actions":[
      { 
        "add":{
           "index" : "secisland",
           "alias" : "alias1",
           "filter" : { "term":{"name": "lancer"} },
           "search_routing" : "1,2",
           "index_routing" : "1" 
        }
      }
   ]
}


#删除一个别名
DELETE http://127.0.0.1:9200/secisland/_alias/alias1

#查找一个索引的别名
GET http://127.0.0.1:/secisland/_alias/*

# 请求所有别名中包含 test1的索引。
GET http://127.0.0.1:/_alias/test1

# 请求所有别名中包含 test开头的的索引。
GET http://127.0.0.1:/_alias/test*


#插入索引 twitter 里 "_id"随机生成的一条记录。
POST twitter/doc/
{
  "user":"lancer",
  "uid":1,
  "city":"shanghai",
  "country":"china"
}

#获取索引 twitter 里 "_id" 为 1的记录
GET twitter/doc/1

#更新索引 twitter 里 "_id" 为 1的记录,如果不存在则插入该条记录
POST twitter/doc/1
{
  "user":"curry",
  "uid":1,
  "age":35,
  "city":"shanghai",
  "message": "上海空气好,天气好,Hello",
  "country":"china",
  "province": "shanghai",
  "address": "上海市普陀区",
  "geo_location":{
    "lat":"29.0844661",
    "lon":"111.335210"
  }
}
 
# 直接更新文档信息。更新某个字段
POST twitter/doc/1/_update
{
  "doc":{
    "age": 30
  }

}



#删除索引 twitter 里 "_id" 为 1的记录
DELETE twitter/doc/1

#删除整个 twitter 索引。
DELETE twitter

# 设置分片数为 1
PUT twitter
{
  "settings": {"number_of_shards": 1}
}
#查找整个 twitter 索引的数据。
GET twitter/_search

#使用 _bulk 批量插入数据,如果省略"_id" 则会自动生成一个id
POST _bulk
{"index":{"_index" : "twitter", "_type" : "doc"}}
{"user":"lancer","message":"今天天气不错啊,出去转转去 hello","uid":2,"age":20, "city":"beijing","province":"beijing","country":"china","address":"中国北京市海淀区","location":{"lat":39.970718,"lon":116.325747}}
{"index":{"_index" : "twitter", "_type" : "doc"}}
{"user":"roman","message":"出发,下一站云南 hEllO","uid":3,"age":30,"city":"beijing","province":"beijing","country":"china","address":"中国北京市东城区","location":{"lat":39.904313,"lon":116.412754}}

# 使用 _bluk 批量操作。
POST /lib2/books/_bluk
{"delete":{"_index":"lib2","_type":"books","_id":4}}
{"create":{"_index":"tt","_type":"ttt","_id":"100"}}
{"name":"lisi"}
{"index":{"_index":"tt","_type":"ttt"}}
{"name":"zhaosi"}
{"update":{"_index":"lib2","_type":"books","_id":4}}
{"doc":{"price":58}}

# bluk 一次性最大处理多少数据量:
  bluk 会把将要处理的数据载入内存中,所以数据量是有限的,最佳的数据量不是一个确定的数值,它取决于你的硬件,你的文档大小,以及复杂性,你的索引以及搜索的负载。
一般建议是 1000-5000个文档,大小建议是 【5-15M】默认不能超过 100M,可以在 es 的配置文件【$ES_HOME/config/elasticsearch.yml】中设置。


# 批量获取多个文档
GET /_mget
{
  "docs":[
      { "_index":"lib",
        "_type": "user",
        "_id":1
      },
      { "_index":"lib",
        "_type": "user",
        "_id":2
      },
      { "_index":"lib",
        "_type": "user",
        "_id":3
      }
  ]
}

# 批量获取多个文档,每个文档获取不同的字段
GET /_mget
{
  "docs":[
      { "_index":"lib",
        "_type": "user",
        "_id":1,
        "_source":"interests"
      },
      { "_index":"lib",
        "_type": "user",
        "_id":2
        "_source":["age","interests"]
      }
  ]
}

# 如果获取的文档的索引相同,类型相同,则可以简写
GET /lib/user/_mget
{
  "docs":[
      { 
        "_id":1,
        "_source":"interests"
      },
      { 
        "_id":2
        "_source":["age","interests"]
      }
  ]
}
 
#批量获取文档最简单的形式。
GET /lib/user/_mget
{
  "ids":["1","2","3"]
}

#按字段查询
GET twitter/_search
{
  "query":{
    "match": {
      "user": "roman"
    }
  }
}

#组合查询: user 为 lancer, 并且 uid 为 2 的记录
GET twitter/_search
{
  "query":{
    "bool":{
      "must":[
        {
          "match": {
            "user": "lancer"
          }
        },
        {
          "match": {
            "uid": "2"
          }
        }
      ]
    }
    
  }
}

#查询条件取反
GET twitter/_search
{
  "query":{
    "bool":{
      "must_not":[
        {
          "match": {
            "user": "lancer"
          }
        }
      ]
    }
    
  }
}

# 或查询,满足任意一个条件就行。
GET twitter/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "match": {
            "user": "lancer"
          }
        },
        {
          "match": {
            "user": "roman"
          }
        }
      ]
    }
    
  }
}

#count 查询
GET twitter/_count
{
  "query":{
    "bool":{
      "should":[
        {
          "match": {
            "user": "lancer"
          }
        },
        {
          "match": {
            "user": "roman"
          }
        }
      ]
    }
    
  }
}

# 获取 mapping
GET twitter/_mapping

#设置 mapping 
PUT twitter/doc/_mapping
{
      "properties": {
        "address": {
          "type": "text",
          "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
          }
        },
        "city":{
          "type": "keyword"
        },
        "country":{
          "type": "keyword"
        },
        "location":{
          "type": "geo_point"
        },
        "province":{
          "type": "keyword"
        },
        "uid":{
          "type": "long"
        },
        "user":{
          "type": "text",
          "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
          }
        }
      }
}

#地理位置相关查询: 朝外 soho 3 公里范围内的记录
GET /twitter/_search
{
  "query":{
    "bool":{
      "must":[
        {
          "match": {
            "city": "beijing"
          }
        }
      ]
    }
    
  },
  "post_filter": {
    "geo_distance": {
      "distance": "3km",
      "location": {
        "lat": 39.970718,
        "lon": 116.325747
      }
    }
  }
}
#按照距离的远近来排序
GET /twitter/_search
{
  "query":{
    "bool":{
      "must":[
        {
          "match": {
            "city": "beijing"
          }
        }
      ]
    }
    
  },
  "post_filter": {
    "geo_distance": {
      "distance": "100km",
      "location": {
        "lat": 39.970718,
        "lon": 116.325747
      }
    }
  },
  "sort":[ 
    {
      "_geo_distance": {
        "location": "39.970718,116.325747",
        "order": "asc",
        "unit": "km"
      }
    }
  ]
}


#按照一定的范围来对数据检索
GET /twitter/_search
{
  "query":{
    "range":{
      "age":{
        "gte": "30",
        "lte": "40"
      }
    }
    
  }
}

#按照一定的范围检索数据,并且按照年龄降序。
GET /twitter/_search
{
  "query":{
    "range":{
      "age":{
        "gte": "30",
        "lte": "40"
      }
    }
  },
   "sort":[ 
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

#全文检索,匹配给定的字符的记录。
GET /twitter/_search
{
  "query":{
    "match":{
      "message":"天气"
    }
  }
}

#全文检索,匹配给定的字符的记录,不忽略大小写。
GET /twitter/_search
{
  "query":{
    "match_phrase":{
      "message":"Hello"
    }
  }
}

#关键字高亮。
GET /twitter/_search
{
  "query":{
    "match_phrase":{
      "message":"Hello"
    }
  }
  , "highlight": {
    "fields": {
      "message": {}
    }
  }
}

#统计结果做聚合。
#如果size 设置为 0 ,只会返回统计结果,不会返回搜索结果。
GET /twitter/_search
{
  "size": 0,
  "aggs":{
    "age":{
      "range":{
        "field": "age",
        "ranges": [
          {
            "from": 10,
            "to":20
          },          {
            "from": 20,
            "to":30
          },          {
            "from": 30,
            "to":40
          }
        ]
      }
    }
  }

}

#字段值的统计。
GET /twitter/_search
{
  "query":{
    "match_phrase":{
      "message":"Hello"
    }
  },
  "size": 0,
  "aggs":{
    "city":{
      "terms": {
        "field": "city",
        "size": 10
      }
    }
  }
  }
}

#分析器
GET twitter/_analyze
{
  "text": ["hello world.this is a test"],
  "analyzer": "standard"
}

#  "analyzer": "sample"
GET twitter/_analyze
{
  "text": ["hello world.this is a test"],
  "analyzer": "simple"
}

#  "analyzer": "sample"
GET twitter/_analyze
{
  "text": ["hello world.this is a test"],
  "tokenizer": "standard"
}
GET twitter/_analyze
{
  "text": ["hello world.this is a test"],
  "tokenizer": "keyword"
}

GET twitter/_analyze
{
  "text": ["Hello World.this is a test"],
  "tokenizer": "keyword",
  "filter":["lowercase"]
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值