๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Elasticsearch

Elasticsearch Exists ํ‚ค์›Œ๋“œ

by ์ฃผ๋ฐœ2 2024. 12. 22.
๋ฐ˜์‘ํ˜•

Elasticsearch์—์„œ ์ธ๋ฑ์Šค์— ํ•„๋“œ์˜ ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•œ ์ฟผ๋ฆฌ๋กœ exists๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋•Œ ์ธ๋ฑ์Šค์˜ ๋งคํ•‘ ์„ค์ •์— ๋”ฐ๋ผ ์ •์ƒ์ ์œผ๋กœ ์กฐํšŒ๊ฐ€ ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋Š”๋ฐ์š”, exists๋กœ ํ•„๋“œ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งคํ•‘์˜ index๊ฐ€ true๋กœ ์„ค์ •์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

PUT /exists-tet
{
  "mappings": {
    "properties": {
      "index_true": {
        "type": "keyword",
        "index": true,
        "doc_values": false
      },
      
      "index_false": {
        "type": "keyword",
        "index": false,
        "doc_values": false
      }
    }
  }
}

index_true๋Š”mappings์—์„œ index๋ฅผ true๋กœ ์„ค์ •ํ•˜์˜€๊ณ , index_false๋Š” false๋กœ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

(index_true์—์„œ index์˜ ๋งคํ•‘ ์ •๋ณด๊ฐ€ ๊ฒ€์ƒ‰์ด ์•ˆ๋˜๋Š”๊ฑด ๋””ํดํŠธ๊ฐ€ true์—ฌ์„œ ๋”ฐ๋กœ ๋ณด์—ฌ์ง€์ง€๊ฐ€ ์•Š๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.)

 

 

POST /exists-tet/_doc
{
  "index_true": "index_true1"
}

POST /exists-tet/_doc
{
  "index_false": "index_false1"
}

POST /exists-tet/_doc
{
  "index_false": "index_false2"
}

POST /exists-tet/_doc
{
  "index_false": "index_false3"
}

POST /exists-tet/_doc
{
  "index_true": "index_true2",
  "index_false": "index_false4"
}

์ถ”๊ฐ€์ ์œผ๋กœ index_true, index_false ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ๊ฑด ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

 

GET /exists-tet/_search
{
  "query": {
    "exists": {
      "field": "index_true"
    }
  }
}

์œ„ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด index_true ๋ฅผ exists๋กœ ์กฐํšŒํ•ด๋ณด๋ฉด, ํžˆํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ 2๊ฑด์œผ๋กœ ์ •์ƒ์ ์œผ๋กœ ์กฐํšŒ๊ฐ€ ๋˜๋Š” ๋ฐ˜๋ฉด์—,

 

 

GET /exists-tet/_search
{
  "query": {
    "exists": {
      "field": "index_false"
    }
  }
}

index_false ๋ฅผ exists๋กœ ์กฐํšŒํ•ด๋ณด๋ฉด ์•„๋ฌด๋Ÿฐ ๋ฐ์ดํ„ฐ๊ฐ€ ์กฐํšŒ๊ฐ€ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

 

GET /exists-tet/_search
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "index_true"
        }
      }
    }
  }
}

์ถ”๊ฐ€์ ์œผ๋กœ index_true๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”(must_not > exists) ํ•„๋“œ๋ฅผ ์กฐํšŒํ•˜๋ ค๋ฉด ์œ„ ์ฟผ๋ฆฌ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

์ƒ‰์ธ๋œ ๋ฐ์ดํ„ฐ์—์„œ ํŠน์ • ํ•„๋“œ๊ฐ€ null์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋ ค๊ณ  ํ•  ๋•Œ, exists ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ null์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋งŒ ์กฐํšŒํ•˜๋ ค๊ณ  ํ–ˆ๋”๋‹ˆ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์กฐํšŒ๋˜์ง€ ์•Š์•„ ์ฐพ์•„๋ณด๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์œผ๋กœ, ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿคฃ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€