Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Getting started with ElasticSearch-basic example (1)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Based on ElasticSearch-6.1.2

About document metadata

Refer to the official authoritative guide documentation metadata

A document has three required metadata elements:

_ index: indicates the index in which the document is stored

_ type: the type of object represented by the document

_ id: unique identification of the document

1. Index new documents

By using index API, documents can be indexed-that is, documents are stored, and documents can be searched.

1.1 use custom ID

The following is an example of indexing a blog, where index is website, type is blog, and custom ID is 123

PUT / website/blog/123 HTTP/1.1 {"date": "2014-01-01", "text": "Still trying this out...", "title": "My second blog entry"}

The response body of ES is as follows:

HTTP/1.1 201 CreatedLocation: / website/blog/123content-encoding: gzipcontent-length: 143content-type: application/json Charset=UTF-8 {"_ id": "123"," _ index ":" website "," _ primary_term ": 1," _ seq_no ": 0," _ shards ": {" failed ": 0," successful ": 1," total ": 2}," _ type ":" blog "," _ version ": 1 "result": "created"}

In ES, every document has a version number (the _ version field in the response), and _ version increments each time it is modified or deleted.

1.2 use ID automatically generated by ES

Submit an index request using POST:

POST / website/blog/ HTTP/1.1 {"date": "2014-01-01", "text": "Still trying this out...", "title": "My second blog entry"}

The following is the response from ES:

HTTP/1.1 201 CreatedLocation: / website/blog/UALcG2EBr-dnzPFB0zH1content-encoding: gzipcontent-length: 165content-type: application/json Charset=UTF-8 {"_ id": "UALcG2EBr-dnzPFB0zH1", "_ index": "website", "_ primary_term": 1, "_ seq_no": 0, "_ shards": {"failed": 0, "successful": 1, "total": 2}, "_ type": "blog", "_ version": 1 "result": "created"}

The other response fields are similar to the above except that _ id is automatically generated by ES.

The automatically generated ID is an URL-safe, Base64-based, 20-character-long GUID string. These GUID strings are generated by a modifiable FlakeID pattern that allows multiple nodes to generate unique ID in parallel with almost zero probability of collision with each other.

two。 Retrieve documents

2.1 retrieve the document for the specified ID

Query the request for blog with ID 123:

GET / website/blog/123?pretty HTTP/1.1

The pretty parameter after the request causes the ES to invoke the prety-print function on output, making the JSON response body more readable.

The ES response is as follows:

HTTP/1.1 200 OKcontent-encoding: gzipcontent-length: 173content-type: application/json Charset=UTF-8 {"_ id": "123"," _ index ":" website "," _ source ": {" date ":" 2014-01-01 "," text ":" Still trying this out... "," title ":" My second blog entry "}," _ type ":" blog "," _ version ": 1," found ": true}

The {"found": true} in the response body indicates that the document has been retrieved. If there is no specified document, found = false will be returned, as follows:

GET / website/blog/124?pretty HTTP/1.1HTTP/1.1 404 Not Foundcontent-encoding: gzipcontent-length: 87content-type: application/json; charset=UTF-8 {"_ id": "_ index": "website", "_ type": "blog", "found": false}

2.2 return part of the document

Only the title field of blog is returned as follows, instead of all fields by default:

GET / website/blog/123?pretty&_source=title HTTP/1.1HTTP/1.1 200 OKcontent-encoding: gzipcontent-length: 136content-type: application/json; charset=UTF-8 {"_ id": "_ index": "website", "_ source": {"title": "My second blog entry"}, "_ type": "blog", "_ version": 1, "found": true}

2.3 only the contents of the document are returned, and meta information is not required.

GET / website/blog/123/_source?pretty HTTP/1.1HTTP/1.1 200 OKcontent-encoding: gzipcontent-length: 113content-type: application/json; charset=UTF-8 {"date": "2014-01-01", "text": "Still trying this out...", "title": "My second blog entry"}

2.3 check whether the document exists

Using a HEAD request, only one HTTP request header is returned:

HEAD / website/blog/123 HTTP/1.1

If the document exists, a 200 OK status code is returned:

HTTP/1.1 200 OK

Content-length: 186

Content-type: application/json; charset=UTF-8

If it does not exist, a 404 Not Found status code is returned:

HTTP/1.1 404 Not Found

Content-length: 61

Content-type: application/json; charset=UTF-8

3, update the document

Documents in ES are immutable and cannot be modified. If you need to update existing documents, you need to rebuild the index or replace them.

PUT / website/blog/123 HTTP/1.1

Accept: application/json, * / *

Accept-Encoding: gzip, deflate

Connection: keep-alive

Content-Length: 117

Content-Type: application/json

Host: localhost:9200

{

"date": "2014-01-02"

"text": "I am starting to get the hang of this..."

"title": "My first blog entry"

}

The following is the response body of ES:

HTTP/1.1 200 OK

Content-encoding: gzip

Content-length: 143

Content-type: application/json; charset=UTF-8

{

"_ id": "123"

"_ index": "website"

"_ primary_term": 2

"_ seq_no": 1

"_ shards": {

"failed": 0

"successful": 1

"total": 2

}

"_ type": "blog"

"_ version": 2

"result": "updated"

}

As you can see above, the _ version field increases itself. Internally, Elasticsearch has marked the old document as deleted and added a completely new one. Although you can no longer access the old version of the document, it will not disappear immediately. As more data continues to be indexed, Elasticsearch cleans up the deleted documents in the background.

4, delete the document

Use the DELETE method to delete the document.

DELETE / website/blog/123 HTTP/1.1

Accept: * / *

Accept-Encoding: gzip, deflate

Connection: keep-alive

Content-Length: 0

Host: localhost:9200

The response body returned by ES is as follows:

HTTP/1.1 200 OK

Content-encoding: gzip

Content-length: 143

Content-type: application/json; charset=UTF-8

{

"_ id": "123"

"_ index": "website"

"_ primary_term": 2

"_ seq_no": 2

"_ shards": {

"failed": 0

"successful": 1

"total": 2

}

"_ type": "blog"

"_ version": 3

"result": "deleted"

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report