2.X/1. Getting Started

1-03-04. Retrieving a Document

drscg 2017. 10. 1. 11:37

To get the document out of Elasticsearch, we use the same _index_type, and _id, but the HTTP verb changes to GET:

Elasticsearch에서 document를 가져오기 위해서는, 동일한 _index_type_id 를 사용해야 한다. 그러나 HTTP verb는 GET 으로 바꾸어야 한다.

GET /website/blog/123?pretty

The response includes the by-now-familiar metadata elements, plus the _source field, which contains the original JSON document that we sent to Elasticsearch when we indexed it:

response는 이제는 익숙한 metadata에, 추가로 _source field를 포함하고 있다. _source field는 색인 시에 Elasticsearch에 전송했던, 원래의 JSON document를 포함하고 있다.

{
  "_index" :   "website",
  "_type" :    "blog",
  "_id" :      "123",
  "_version" : 1,
  "found" :    true,
  "_source" :  {
      "title": "My first blog entry",
      "text":  "Just trying this out...",
      "date":  "2014/01/01"
  }
}
Note

Adding pretty to the query-string parameters for any request, as in the preceding example, causes Elasticsearch to pretty-print the JSON response to make it more readable. The _source field, however, isn’t pretty-printed. Instead we get back exactly the same JSON string that we passed in.

위의 예처럼, 어떤 request이든 query-string 매개변수에 pretty 를 더하면, Elasticsearch는 JSON response를 좀 더 읽기 쉽게 만들어, 보기 좋게 출력(pretty-print) 한다. 그러나 _source field는 보기 좋게 출력되지 않고, 색인 시에 입력한 JSON 문자열을 정확히 똑같이 돌려준다.

The response to the GET request includes {"found": true}. This confirms that the document was found. If we were to request a document that doesn’t exist, we would still get a JSON response, but found would be set to false.

GET request에 대한 response는 {"found": true} 를 포함하고 있다. 이것은 document가 발견되었다는 것을 확인해 준다. 만약 존재하지 않는 document를 request하면, found 가 false 로 설정된 JSON response가 나올 것이다.

Also, the HTTP response code would be 404 Not Found instead of 200 OK. We can see this by passing the -i argument to curl, which causes it to display the response headers:

또한, HTTP response code는 200 OK 가 아닌, 404 Not Found 가 될 것이다. curl 에 -i 매개변수를 전달하면, response header를 볼 수 있다.

curl -i -XGET http://localhost:9200/website/blog/124?pretty

The response now looks like this:

response는 아래와 같다.

HTTP/1.1 404 Not Found
Content-Type: application/json; charset=UTF-8
Content-Length: 83

{
  "_index" : "website",
  "_type" :  "blog",
  "_id" :    "124",
  "found" :  false
}

Retrieving Part of a Documentedit

By default, a GET request will return the whole document, as stored in the _source field. But perhaps all you are interested in are the title and text fields. Individual fields can be requested by using the _source parameter. Multiple fields can be specified in a comma-separated list:

기본적으로, GET request는 _source field에 저장되어 있는, 전체 document를 반환한다. 그런데, title과 text field에만 관심이 있다면, _source 매개변수를 사용하여, 개별 field를 request할 수 있다. 다중 field는 ,(comma)로 구분된 목록으로 지정할 수 있다.

GET /website/blog/123?_source=title,text

The _source field now contains just the fields that we requested and has filtered out the date field:

이제 _source field는 request한 field만을 포함하고 date field는 걸러졌다.

{
  "_index" :   "website",
  "_type" :    "blog",
  "_id" :      "123",
  "_version" : 1,
  "found" :   true,
  "_source" : {
      "title": "My first blog entry" ,
      "text":  "Just trying this out..."
  }
}

Or if you want just the _source field without any metadata, you can use the _source endpoint:

metadata 없이 _source field  원한다면, 마지막에 _source 를 쓰면 된다.

GET /website/blog/123/_source

which returns just the following:

아래와 같이 반환한다.

{
   "title": "My first blog entry",
   "text":  "Just trying this out...",
   "date":  "2014/01/01"
}


'2.X > 1. Getting Started' 카테고리의 다른 글

1-03-02. Document Metadata  (0) 2017.10.01
1-03-03. Indexing a Document  (0) 2017.10.01
1-03-05. Checking Whether a Document Exists  (0) 2017.10.01
1-03-06. Updating a Whole Document  (0) 2017.10.01
1-03-07. Creating a New Document  (0) 2017.10.01