ElasticSearch Learning Path -day11

Reprinted from: https://blog.csdn.net/chengyuqiang/column/info/18392 , ES version 6.3.0

New Document: index / of the type / the above mentioned id
(1) general format

PUT blog/csdn/1
{
  "id":1,
  "title":"Elasticsearch简介",
  "author":"chengyuqiang",
  "content":"Elasticsearch是一个基于Lucene的搜索引擎"
}

return

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

Continue to add a data

POST blog/csdn/2
{
  "id":2,
  "title":"Git简介",
  "author":"chengyuqiang",
  "content":"Git是一个版本控制软件"
}

return

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

(2) do not specify a document id

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

return

{
  "_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
}

Get the document:
(1) acquire an existing document

GET blog/csdn/1

return

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

(2) to obtain the document does not exist

GET blog/csdn/100

return

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

(3) Head command to check whether a document exists

HEAD blog/csdn/1

200 - OK

HEAD blog/csdn/100

404 - Not Found
(4) Batch for documentation

GET blog/csdn/_mget
{
  "ids":["1","2"]
}

Back to Results

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

Multiple-document search
here tell us about simple document retrieval operation, described in detail in later chapters
(1) retrieve all documents

GET blog/_search

return

{
  "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 query
term query is used to find files in the specified field contains the specified word, the word only when a query word and document exactly when the matches are retrieved

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

Since the word is not used IK Chinese, each character is seen as a separate word.

{
  "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面向对象程序设计"
        }
      }
    ]
  }
}

When the query "program", title field no such word, default characters are divided into single words

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

return

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

(3) terms query
query document summary documents that contain multiple words

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

Note that after word of English words into a lower case, such as "Java" lexical items into a "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 the query
and query different precise term for the match query, the query field as long as there is any lexical item is matched, it will search for the document
GET blog / _search

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

return

{
  "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面向对象程序设计"
        }
      }
    ]
  }
}

Update document
(1) update the data
file in Elasticsearch is immutable and can not be modified. If we need to modify the document, Elasticsearch actually rebuild a new document to replace the old documents.

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

return

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

note:

  • 1. version plus 1
  • 2.created identified as false, because the document type already exists under the same index of the same ID
  • 3. Within ES, _version file 1 has been marked for deletion and add a complete new document. Old documents will not disappear immediately, but can no longer access him.

Query again

GET blog/csdn/2

return

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

(2) update the field

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

return

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

View the updated document

GET blog/csdn/2

return

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

(3) add a new field

POST blog/csdn/1/_update
{
  "script": "ctx._source.posttime=\"2018-01-09\""
}

return

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

Query the updated document

GET blog/csdn/1

return

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

We found _version version parameter has been added 1
(4) update query

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

return

{
  "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 Document

DELETE blog/csdn/1

return

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

 

Guess you like

Origin blog.csdn.net/qq_23536449/article/details/91047842