Elasticsearch 入门(二)

Felix 2019年11月04日 196次浏览

插入数据模型

Request
URL: http://localhost:9200/accounts
Method: PUT
Body:
{
  "mappings": {
    "person": {
      "properties": {
        "user": {
          "type": "text",
          "analyzer": "smartcn",
          "search_analyzer": "smartcn"
        },
        "title": {
          "type": "text",
          "analyzer": "smartcn",
          "search_analyzer": "smartcn"
        },
        "desc": {
          "type": "text",
          "analyzer": "smartcn",
          "search_analyzer": "smartcn"
        }
      }
    }
  }
}

Response
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "accounts"
}

保存数据

Request
URL: http://localhost:9200/accounts/person
Method: POST
Body:
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}

{
    "_index": "accounts",
    "_type": "person",
    "_id": "bX-KNm4BqlwhIt9xmp5f",	#随机生成id
    "_version": 1,			#当前版本
    "result": "created",		#当前状态
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

数据查询

查询所有数据

URL: http://localhost:9200/accounts/person/_search
Method: GET

{
  "took": 7,				#花费时间
  "timed_out": false,			#是否超时
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,				#数据个数
    "max_score": 1.0,
    "hits": [
      {
        "_index": "accounts",
        "_type": "person",
        "_id": "bX-KNm4BqlwhIt9xmp5f",
        "_score": 1.0,
        "_source": {
          "user": "张三",
          "title": "工程师",
          "desc": "数据库管理"
        }
      },
      {
        "_index": "accounts",
        "_type": "person",
        "_id": "bn-QNm4BqlwhIt9xp54L",
        "_score": 1.0,
        "_source": {
          "user": "李四",
          "title": "工程师",
          "desc": "数据库管理,软件开发"
        }
      }
    ]
  }
}

条件查询数据

URL: http://localhost:9200/accounts/person/_search
Method: GET
Body:
#匹配单个条件
{
  "query": {
    "match": {
      "desc": "数据库"		
    }
  },
  "size": 2,			#返回个数
  "from": 0			#返回开始下标
}

#匹配多个or条件
{
  "query": {
    "match": {
      "desc": "数据库 开发"		
    }
  },
  "size": 2,
  "from": 0
}

#匹配多个and条件,需要用到bool查询
{
  "query": {
    "bool": {
      "must": [
        { "match": { "desc": "数据库" } },
        { "match": { "desc": "开发" } }
      ]
    }
  },
  "size": 2,
  "from": 0
}

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "accounts",
        "_type": "person",
        "_id": "bX-KNm4BqlwhIt9xmp5f",
        "_score": 0.2876821,
        "_source": {
          "user": "张三",
          "title": "工程师",
          "desc": "数据库管理"
        }
      },
      {
        "_index": "accounts",
        "_type": "person",
        "_id": "bn-QNm4BqlwhIt9xp54L",
        "_score": 0.2876821,
        "_source": {
          "user": "李四",
          "title": "工程师",
          "desc": "数据库管理,软件开发"
        }
      }
    ]
  }
}

通过Id查询

URL: http://localhost:9200/accounts/person/bX-KNm4BqlwhIt9xmp5f?pretty=true 		#pretty=true 表示以易读的格式返回
Method: GET

{
  "_index": "accounts",
  "_type": "person",
  "_id": "bX-KNm4BqlwhIt9xmp5f",
  "_version": 1,
  "found": true,
  "_source": {
    "user": "张三",
    "title": "工程师",
    "desc": "数据库管理"
  }
}

数据更新

URL: http://localhost:9200/accounts/person/bX-KNm4BqlwhIt9xmp5f
Method: PUT
Boby: 
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理2"
}

{
    "_index": "accounts",
    "_type": "person",
    "_id": "bX-KNm4BqlwhIt9xmp5f",
    "_version": 2,			# version增加
    "result": "updated",		# 状态改为updated
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

数据删除

URL: http://localhost:9200/accounts/person/bX-KNm4BqlwhIt9xmp5f
Method: DELETE

{
    "_index": "accounts",
    "_type": "person",
    "_id": "bX-KNm4BqlwhIt9xmp5f",
    "_version": 3,			#version增加
    "result": "deleted",		#状态改改deleted
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}