资讯专栏INFORMATION COLUMN

ElasticSearch 学习笔记 - 5. 文档

Batkid / 369人阅读

摘要:程序编程面向对象程序设计更新文档更新数据文档在中是不可变的,不能修改。更新字段是一个开源的分布式版本控制软件添加新字段查询更新删除文档

1、新建文档

(1)一般格式

PUT blog/segmentfault/1
{
  "id":1,
  "title":"Elasticsearch简介",
  "author":"kyle",
  "content":"Elasticsearch是一个基于Lucene的搜索引擎"
}
名称 参数 说明
blog _index 索引名
segmentfault _type 类型名
1 _id 文档ID
1 _version 版本号

(2)未指定文档ID

POST blog/segmentfault
{
  "id":3,
  "title":"Java编程",
  "author":"kyle",
  "content":"Java面向对象程序设计"
}

(3)添加多个type
6.x之后不允许添加多个type

POST blog/csdn
{
  "id":3,
  "title":"Java编程",
  "author":"chengyuqiang",
  "content":"Java面向对象程序设计"
}

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Rejecting mapping update to [blog] as the final mapping would have more than 1 type: [csdn, segmentfault]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Rejecting mapping update to [blog] as the final mapping would have more than 1 type: [csdn, segmentfault]"
  },
  "status": 400
}
2、获取文档
GET /blog/segmentfault/1

{
  "_index": "blog",
  "_type": "segmentfault",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "id": 1,
    "title": "Elasticsearch简介",
    "author": "kyle",
    "content": "Elasticsearch是一个基于Lucene的搜索引擎"
  }
}
3、文档搜索

(1)、检索全部

GET /blog/_search

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

GET /blog/_search
{
  "query": {
    "term": {
      "content": {
        "value": "程"
      }
    }
  }
}

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

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

GET /blog/_search
{
  "query": {
    "term": {
      "content": {
        "value": "程序"
      }
    }
  }
}

{
  "took": 1,
  "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": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "blog",
        "_type": "segmentfault",
        "_id": "V0JLZmYBnUSb-hp-GzV6",
        "_score": 1,
        "_source": {
          "id": 3,
          "title": "Java编程",
          "author": "kyle",
          "content": "Java面向对象程序设计"
        }
      },
      {
        "_index": "blog",
        "_type": "segmentfault",
        "_id": "2",
        "_score": 1,
        "_source": {
          "id": 2,
          "title": "Git简介",
          "author": "lalala",
          "content": "Git是一个版本控制软件"
        }
      }
    ]
  }
}

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

GET /blog/_search
{
  "query": {
    "match": {
      "content": "程序"
    }
  }
}

{
  "took": 18,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "blog",
        "_type": "segmentfault",
        "_id": "V0JLZmYBnUSb-hp-GzV6",
        "_score": 0.5753642,
        "_source": {
          "id": 3,
          "title": "Java编程",
          "author": "kyle",
          "content": "Java面向对象程序设计"
        }
      }
    ]
  }
}
4、更新文档

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

POST /blog/segmentfault/2
{
  "id":2,
  "title":"Git简介",
  "author":"hahaha",
  "content":"Git是一个分布式版本控制软件"
}

{
  "_index": "blog",
  "_type": "segmentfault",
  "_id": "2",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}

NOTE:

版本加1。

"result": "updated",因为同索引同类型下已经存在同ID的文档。

在ES内部,_version为1的文件已经被标记“删除”,并添加了一个完整的新文档。旧文档不会立即消失,但是不能再访问它。

(2)更新字段

POST /blog/segmentfault/2/_update
{
  "script": {
    "source": "ctx._source.content="GIT是一个开源的分布式版本控制软件""  
  }
}

{
  "_index": "blog",
  "_type": "segmentfault",
  "_id": "2",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}

(3)添加新字段

POST /blog/segmentfault/2/_update
{
  "script": "ctx._source.posttime="2018-10-09""
}

{
  "_index": "blog",
  "_type": "segmentfault",
  "_id": "2",
  "_version": 4,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 1
}

(4)查询更新

POST blog/_update_by_query
{
  "script": {
    "source": "ctx._source.category=params.category",
    "lang":"painless",
    "params":{"category":"git"}
  },
  "query":{
    "term": {"title":"git"}
  }
}
5、删除文档
DELETE /blog/segmentfault/2

{
  "_index": "blog",
  "_type": "segmentfault",
  "_id": "2",
  "_version": 7,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 6,
  "_primary_term": 1
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/34308.html

相关文章

  • ElasticSearch 学习笔记 - 3.核心概念

    摘要:中索引的概念具有不同意思,这里的索引相当于关系数据库中的一个数据库实例。的类型概念相当于关系数据库的数据表。的文档具有格式,由多个字段组成,字段相当于关系数据库中列的概念。副本其实,分片全称是主分片,简称为分片。 1 索引 索引(index)是ElasticSearch存放具体数据的地方,是一类具有相似特征的文档的集合。ElasticSearch中索引的概念具有不同意思,这里的索引相当...

    niuxiaowei111 评论0 收藏0
  • elastic学习笔记

    摘要:至于其他的算一般我习惯说树形模型,这里说的概率模型可能是差不多的意思。 要点 不同工具之间版本匹配很重要由点及面,先实践起来再学细节的原理和使用 技术栈 laravel5.5框架+scout组件+elasticsearch6.3.0搜索引擎 辅助 elasticsearch-head 查看集群数据可视化 中文分词插件Ik 介绍 laravel是一款现代化的php框架es是搜索引擎e...

    heartFollower 评论0 收藏0
  • elastic学习笔记

    摘要:至于其他的算一般我习惯说树形模型,这里说的概率模型可能是差不多的意思。 要点 不同工具之间版本匹配很重要由点及面,先实践起来再学细节的原理和使用 技术栈 laravel5.5框架+scout组件+elasticsearch6.3.0搜索引擎 辅助 elasticsearch-head 查看集群数据可视化 中文分词插件Ik 介绍 laravel是一款现代化的php框架es是搜索引擎e...

    wemallshop 评论0 收藏0
  • elasticsearch学习笔记(三)——Elasticsearch的核心概念

    摘要:下面来通过引出的核心概念和的前世今生是最先进功能强大的搜索库。任何一个服务器随时可能故障或宕机,此时可能就会丢失,因此可以为每个创建多个副本。默认每个索引个个个,最小的高可用配置是台服务器核心概念和数据库核心概念数据库行表 下面来通过lucene引出Elasticsearch的核心概念 1、lucene和elasticsearch的前世今生 lucene是最先进、功能强大的搜索库。但是...

    wow_worktile 评论0 收藏0
  • ElasticSearch 学习笔记 - 7. 字段类型

    摘要:字符串类型类型在旧版本中使用较多,从开始不再支持,由和类型替代。当一个字段是要被全文搜索的,比如内容产品描述,应该使用类型。如果字段需要进行过滤比如查找已发布博客中属性为的文章排序聚合。默认情况下,该类型的字段只存储不索引。 1、字符串类型 (1)string string类型在ElasticSearch 旧版本中使用较多,从ElasticSearch 5.x开始不再支持string,...

    reclay 评论0 收藏0

发表评论

0条评论

Batkid

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<