Elasticsearch 기본 동작

CHEESE·2024년 2월 21일

Elasticsearch

목록 보기
2/5
post-thumbnail

1️⃣ 문서 색인

_id를 지정하여 색인하기

  • _id: 문서를 인덱스 내에서 고유하게 식별하기 위한 값. 모든 문서가 _id값을 갖는다.
PUT [인덱스 이름]/_doc/[_id값]
{
	[문서 내용]
}

예시

request

PUT my_index/_doc/1
{
	"title": "hello cheese",
    "views": 0,
    "public": true,
    "created": "2024-02-21"
}

response

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

result 값이 created면 색인에 성공한 것이다.

_id 없이 색인하기

_id 값을 지정하지 않으면 엘라스틱 서치가 자동으로 _id값을 생성한다.

POST [인덱스 이름]/_doc
{
	[문서 내용]
}

예시

request

POST my_index/_doc
{
	"title": "hello cheese",
    "views": 0,
    "public": true,
    "created": "2024-02-21"
}

response

{
	"_index": "my_index",
  	"_type": "_doc",
  	"_id": "203Md1DS3J4zwaDD38f0",
  	"_version": 1,
  	"result": "created",
  	"_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
    },
  	"_seq_no": 1,
  	"_primary_term": 1
}

_id 값이 자동으로 생성된다.

2️⃣ 문서 조회

GET [인덱스 이름]/_doc/[_id값]

예시

request

GET my_index/_doc/1

response

{
	"_index": "my_index",
  	"_type": "_doc",
  	"_id": "1",
  	"_version": 1,
  	"_seq_no": 0,
  	"_primary_term": 1,
  	"found": true,
  	"_source" : {
      	"title": "hello cheese",
    	"views": 0,
    	"public": true,
    	"created": "2024-02-21"
    }
}

_source 필드에서 색인한 문서의 내용을 확인할 수 있다.

3️⃣ 문서 변경

POST [인덱스 이름]/_update/[_id값]
{
	"doc": {
		[문서 내용]
    }
}

예시

request

POST my_index/_update/1
{
	"doc": {
		"title": "bye cheese"
    }
}

response

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

4️⃣ 문서 검색

엘라스틱서치는 다양한 검색 쿼리를 위한 쿼리 DSL을 제공한다.
GET 메서드에 request body를 사용한다는 점이 특징이다. 일반적이지 않은 방식이기 때문에 POST 메서드를 사용해도 동일하게 동작한다.

GET [인덱스 이름]/_search
POST [인덱스 이름]/_search

예시

request

GET my_index/_search
{
	"query": {
    	"match": {
        	"title": "hello cheese"
        }
    }
}

response

{
	"took": 21,
    "timed_out": false,
    "_shards": {
    	"total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
    	"total": {
        	"value": 2,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
        	{
                "_index": "my_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 1,
                "_source" : {
                    "title": "hello cheese",
                    "views": 0,
                    "public": true,
                    "created": "2024-02-21"
                }
          	},
            {
                "_index": "my_index",
                "_type": "_doc",
                "_id": "203Md1DS3J4zwaDD38f0",
                "_score": 0.8754687,
                "_source" : {
                    "title": "bye cheese",
                    "views": 0,
                    "public": true,
                    "created": "2024-02-21"
                }
          	}
        ]
    }
}

_score에서 유사도 점수를 확인할 수 있다.

5️⃣ 문서 삭제

DELETE [인덱스 이름]/_doc/[_id값]

예시

request

DELETE my_index/_doc/1

response

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

0개의 댓글