ElasticSearch 學習筆記 - 5. 文檔

一、新建文檔

(1)通常格式java

PUT blog/segmentfault/1
{
  "id":1,
  "title":"Elasticsearch簡介",
  "author":"kyle",
  "content":"Elasticsearch是一個基於Lucene的搜索引擎"
}
名稱 參數 說明
blog _index 索引名
segmentfault _type 類型名
1 _id 文檔ID
1 _version 版本號

(2)未指定文檔IDgit

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
}

二、獲取文檔

GET /blog/segmentfault/1

{
  "_index": "blog",
  "_type": "segmentfault",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "id": 1,
    "title": "Elasticsearch簡介",
    "author": "kyle",
    "content": "Elasticsearch是一個基於Lucene的搜索引擎"
  }
}

三、文檔搜索

(1)、檢索所有segmentfault

GET /blog/_search

(2)、term查詢
term查詢用於查找指定字段中包含指定分詞的文件,只有當查詢分詞和文檔中的分詞精確匹配時才被檢索到。app

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字段中找不到這樣的分詞,默認漢字被分爲單字詞less

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查詢,只要被查詢字段中存在任何一個詞項被匹配,就會搜索到該文檔。post

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面向對象程序設計"
        }
      }
    ]
  }
}

四、更新文檔

(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"}
  }
}

五、刪除文檔

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
}
相關文章
相關標籤/搜索