ElasticSearch 6.x 学习笔记:7.文档

原文: https://blog.csdn.net/chengyuqiang/article/details/79009805

7.1 新建文档

(1)一般格式

PUT blog/csdn/1
{
  "id":1,
  "title":"Elasticsearch简介",
  "author":"chengyuqiang",
  "content":"Elasticsearch是一个基于Lucene的搜索引擎"
}
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": true
}

在这里插入图片描述

名称参数说明
blog_index索引名
csdn_type类型名
1_id文档ID
1_version版本号

继续添加一条数据

POST blog/csdn/2
{
  "id":2,
  "title":"Git简介",
  "author":"chengyuqiang",
  "content":"Git是一个版本控制软件"
}
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "2",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": true
}

(2)未指定文档ID

POST blog/csdn
{
  "id":3,
  "title":"Java编程",
  "author":"chengyuqiang",
  "content":"Java面向对象程序设计"
}
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "wkv472AB5R2olyYk97rN",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

在这里插入图片描述

7.2 获取文档

GET blog/csdn/1
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "id": 1,
    "title": "Elasticsearch简介",
    "author": "chengyuqiang",
    "content": "Elasticsearch是一个基于Lucene的搜索引擎"
  }
}

在这里插入图片描述

GET blog/csdn/100
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "100",
  "found": false
}

在这里插入图片描述

HEAD blog/csdn/1
200 - OK
HEAD blog/csdn/100
404 - Not Found
GET blog/csdn/_mget
{
   "ids":["1","2"]
}
{
  "docs": [
    {
      "_index": "blog",
      "_type": "csdn",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "id": 1,
        "title": "Elasticsearch简介",
        "author": "chengyuqiang",
        "content": "Elasticsearch是一个基于Lucene的搜索引擎"
      }
    },
    {
      "_index": "blog",
      "_type": "csdn",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "id": 2,
        "title": "Git简介",
        "author": "chengyuqiang",
        "content": "Git是一个版本控制软件"
      }
    }
  ]
}

7.3 文档搜索

这里介绍一下简单的文档检索操作,后面章节会详细介绍。
(1)检索全部文档

GET blog/_search
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "blog",
        "_type": "csdn",
        "_id": "2",
        "_score": 1,
        "_source": {
          "id": 2,
          "title": "Git简介",
          "author": "chengyuqiang",
          "content": "Git是一个版本控制软件"
        }
      },
      {
        "_index": "blog",
        "_type": "csdn",
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": 1,
          "title": "Elasticsearch简介",
          "author": "chengyuqiang",
          "content": "Elasticsearch是一个基于Lucene的搜索引擎"
        }
      },
      {
        "_index": "blog",
        "_type": "csdn",
        "_id": "wkv472AB5R2olyYk97rN",
        "_score": 1,
        "_source": {
          "id": 3,
          "title": "Java编程",
          "author": "chengyuqiang",
          "content": "Java面向对象程序设计"
        }
      }
    ]
  }
}

(2)term 查询
term 查询用于查找指定字段中包含指定分词的文档,只有当查询分词和文档中的分词精确匹配时才被检索到。

GET blog/_search
{
  "query": {
    "term": {
      "title": "程"
    }
  }
}

由于未使用IK中文分词,每个汉字被看做独立的一个词。

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "blog",
        "_type": "csdn",
        "_id": "wkv472AB5R2olyYk97rN",
        "_score": 0.2876821,
        "_source": {
          "id": 3,
          "title": "Java编程",
          "author": "chengyuqiang",
          "content": "Java面向对象程序设计"
        }
      }
    ]
  }
}

当查询”程序”时,title字段中找不到这样的分词,默认汉字被分为单字词。

GET blog/_search
{
  "query": {
    "term": {
      "title": "程序"
    }
  }
}
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

(3)terms查询
查询文档汇总包含多个词的文档

GET blog/_search
{
  "query": {
    "terms": {
      "title": ["java","git"]
    }
  }
}

注意,经过分词后英文单词变成了小写,比如”Java”词项变成了”java”

{
  "took": 17,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "blog",
        "_type": "csdn",
        "_id": "2",
        "_score": 1,
        "_source": {
          "id": 2,
          "title": "Git简介",
          "author": "chengyuqiang",
          "content": "Git是一个版本控制软件"
        }
      },
      {
        "_index": "blog",
        "_type": "csdn",
        "_id": "wkv472AB5R2olyYk97rN",
        "_score": 1,
        "_source": {
          "id": 3,
          "title": "Java编程",
          "author": "chengyuqiang",
          "content": "Java面向对象程序设计"
        }
      }
    ]
  }
}

(4)match查询
与term精确查询不同,对于match查询,只要被查询字段中存在任何一个词项被匹配,就会搜索到该文档。

GET blog/_search
{
  "query": {
    "match": {
      "title": {
        "query": "程序"
      }
    }
  }
}
{
  "took": 37,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "blog",
        "_type": "csdn",
        "_id": "wkv472AB5R2olyYk97rN",
        "_score": 0.2876821,
        "_source": {
          "id": 3,
          "title": "Java编程",
          "author": "chengyuqiang",
          "content": "Java面向对象程序设计"
        }
      }
    ]
  }
}

7.4 更新文档

(1)更新数据
文档在ElasticSearch 中是不可改变的,不能修改。如果我们需要修改文档,ElasticSearch 实际上重建新文档替换掉旧文档。

POST blog/csdn/2
{
  "id":2,
  "title":"Git简介",
  "author":"hadron",
  "content":"Git是一个分布式版本控制软件"
}
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "2",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": false
}

注意

  • 注意版本加一
  • created 标识为false,因为用索引同类型下已经存在通ID的文档
  • 在ES 内部,_version为1的文件已经被标识“删除”,并添加了一个完整的新文档。旧文档不会立即消失,但是不能再访问它。

查询

GET blog/csdn/2
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "2",
  "_version": 2,
  "found": true,
  "_source": {
    "id": 2,
    "title": "Git简介",
    "author": "hadron",
    "content": "Git是一个分布式版本控制软件"
  }
}

(2)更新字段

POST blog/csdn/2/_update
{
  "script": {
    "source": "ctx._source.content=\"Git是一个开源的分布式版本控制软件\""  
  } 
}
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "2",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 7,
  "_primary_term": 1
}

查看更新后的文档

GET blog/csdn/2
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "2",
  "_version": 3,
  "found": true,
  "_source": {
    "id": 2,
    "title": "Git简介",
    "author": "hadron",
    "content": "Git是一个开源的分布式版本控制软件"
  }
}

(3)添加新字段

POST blog/csdn/1/_update
{
  "script": "ctx._source.posttime=\"2018-01-09\""
}
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  }
}

查询更新后的文档

GET blog/csdn/1
{
  "_index": "blog",
  "_type": "csdn",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "id": 1,
    "title": "Elasticsearch简介",
    "author": "chengyuqiang",
    "content": "Elasticsearch是一个基于Lucene的搜索引擎",
    "posttime": "2018-01-09"
  }

发现版本参数_version 已经加1

(2)查询更新

POST blog/_update_by_query
{
  "script": {
    "source": "ctx._source.category=params.category",
    "lang":"painless",
    "params":{"category":"git"}
  },
  "query":{
    "term": {"title":"git"}
  }
}
{
  "took": 72,
  "timed_out": false,
  "total": 2,
  "updated": 2,
  "deleted": 0,
  "batches": 1,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1,
  "throttled_until_millis": 0,
  "failures": []
}

7.5 删除文档

DELETE blog/csdn/1 
{
  "found": true,
  "_index": "blog",
  "_type": "csdn",
  "_id": "1",
  "_version": 4,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值