说明:elasticsearch是目前最流行的搜索引擎,功能强大,非常受欢迎。特点是倒排索引,在海量数据的模糊查找领域,效率非常高。elasticsearch的安装和使用参考:http://t.csdn.cn/yJdZb。
本文介绍在es的索引库(index)操作和文档(document)操作,相当于数据库的表操作和数据操作。
索引库(index)操作
创建索引库
格式:PUT /索引库名
PUT /student
{
  # 映射,索引中文档的约束
  "mappings": {
    # 字段
    "properties": {
      # 姓名
      "name":{
        # 类型为text,表示此字段参与分词
        "type":"text",
        # 分词器为ik_smart
        "analyzer": "ik_smart"
      },
      # 年龄
      "age":{
        "type": "integer"
      },
      # 个人信息
      "info":{
        "type":"keyword"
      }
    }
  }
}
创建成功
 
查找索引库
格式:GET /索引库名
GET /student

删除索引库
格式:DELETE /索引库名
DELETE /student

修改索引库
格式:PUT /索引库名/_mapping
修改索引库只能添加字段,不能修改原有的索引库结构,如添加一个hobby字段,类型为keyword,不参与分词;
PUT /student/_mapping
{
  "properties":{
    "hobby":{
      "type":"keyword"
    }
  }
}

文档(document)操作
添加文档
格式:POST /索引库名/_doc/ID
添加文档,相当于添加一条数据,如在student索引库中添加一条文档;
POST /student/_doc/1
{
  "name":"zhangsan",
  "age":"35",
  "info":"中国人",
  "hobby":"reading"
}

查找文档
格式:GET /索引库名/_doc/ID
GET /student/_doc/1

删除文档
格式:DELETE /索引库名/_doc/ID
DELETE /student/_doc/1

修改文档
格式:PUT /索引库名/_doc/ID 或 POST /索引库名/_update/ID
# 方式一:全量修改,会删除旧文档,添加新文档
PUT /student/_doc/1
{
  "name":"lisi",
  "age":"60",
  "info":"中国人",
  "hobby":"walk"
}
# 方式二:增量修改,修改指定字段
POST /student/_update/1
{
  "doc":{
    "name":"wangwu"
  }
}

总结
添加、创建、修改使用POST、PUT,查找用GET,删除用DELETE



















