ElasticSearch学习之路-day11

转载自:https://blog.csdn.net/chengyuqiang/column/info/18392,ES版本号6.3.0

新建文档:index/type/id
(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
  },
  "_seq_no": 0,
  "_primary_term": 3
}

继续添加一条数据

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
  },
  "_seq_no": 0,
  "_primary_term": 3
}

(2)未指定文档id

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

返回

{
  "_index": "blog",
  "_type": "csdn",
  "_id": "i9DSO2gBBk_Lv-BZu9bh",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 3
}

获取文档:
(1)获取已存在文档

GET blog/csdn/1

返回

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

(2)获取不存在文档

GET blog/csdn/100

返回

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

(3)Head命令查看文档是否存在

HEAD blog/csdn/1

200 - OK

HEAD blog/csdn/100

404 - Not Found
(4)批量获取文档

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是一个版本控制软件"
      }
    }
  ]
}

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

GET blog/_search

返回

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "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": "i9DSO2gBBk_Lv-BZu9bh",
        "_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": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.6931472,
    "hits": [
      {
        "_index": "blog",
        "_type": "csdn",
        "_id": "i9DSO2gBBk_Lv-BZu9bh",
        "_score": 0.6931472,
        "_source": {
          "id": 3,
          "title": "Java编程",
          "author": "chengyuqiang",
          "content": "Java面向对象程序设计"
        }
      }
    ]
  }
}

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

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

返回

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "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": 3,
    "successful": 3,
    "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": "i9DSO2gBBk_Lv-BZu9bh",
        "_score": 1,
        "_source": {
          "id": 3,
          "title": "Java编程",
          "author": "chengyuqiang",
          "content": "Java面向对象程序设计"
        }
      }
    ]
  }
}

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

{
  "query": {
    "match": {
      "title": {
        "query": "程序"
      }
    }
  }
}

返回

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

更新文档
(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
  },
  "_seq_no": 1,
  "_primary_term": 3
}

注意:

  • 1.版本加1
  • 2.created标识为false,因为同索引类型下已经存在同ID的文档
  • 3.在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": 2,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 4
}

查看更新后的文档

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
  },
  "_seq_no": 2,
  "_primary_term": 4
}

查询更新后的文档

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
(4)查询更新

POST blog/_update_by_query
{
   "script": {
    "source": "ctx._source.category=params.category",
    "lang":"painless",
    "params":{"category":"git"}
  },
  "query":{
    "term": {"title":"git"}
  }
}

返回

{
  "took": 470,
  "timed_out": false,
  "total": 1,
  "updated": 1,
  "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": []
}

删除文档

DELETE blog/csdn/1

返回

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

猜你喜欢

转载自blog.csdn.net/qq_23536449/article/details/91047842