Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exists query는 Field Value가 Null인 경우 어떻게 처리하나요? #4

Closed
higee opened this issue Nov 24, 2017 · 0 comments
Closed

Comments

@higee
Copy link
Owner

higee commented Nov 24, 2017

조회하려는 필드에 적어도 한 개의 null이 아닌 값이 있는 Documents를 검색한다


  • 예시
    • 다음과 같은 Documents가 있다고 하자
    { "user": "jane" }
    { "user": "" } 
    { "user": ["jane"] }
    { "user": ["jane", null ] } 
    { "user": null }
    { "user": [] } 
    { "user": [null] } 
    { "foo":  "bar" } 
    
    • 그리고 다음과 같은 query로 검색한다고 해보자
    GET /_search
    {
      "query": {
        "exists" : { 
          "field" : "user" 
        }
      }
    }
    
    • 이 때 결과는 다음과 같다
document exists missing 비고
{ "user": "jane" } o "jane"이라는 non-null value 존재
{ "user": "" } o 빈 string은 non-null value
{ "user": ["jane"] } o "jane"이라는 non-null value 존재
{ "user": ["jane", null] } o "jane"이라는 non-null value 존재
{ "user": null} o null만 존재
{ "user": [] } o 아무 값도 없으므로 null
{ "user": [null] } o null만 존재
{ "foo": "bar" } o "user" field 자체가 없음
  • 단, null-value mapping을 통해 위의 결과가 바뀔 수 있다
    • mapping 예시
    PUT test_index
    {
      "mappings": {
        "test_type": {
          "properties": {
            "user": {
              "type": "keyword",
              "null_value": "NULL"
            }
          }
        }
      }
    }
    
    • 의미 : 명시적으로 null 로 들어온 value를 NULL 이라는 string으로 변환
    • 위와 같은 query 결과
document exists missing 비고
{ "user": "jane" } o "jane"이라는 non-null value 존재
{ "user": "" } o 빈 string은 non-null value
{ "user": ["jane"] } o "jane"이라는 non-null value 존재
{ "user": ["jane", null] } o "jane"이라는 non-null value 존재
{ "user": null} o null -> NULL 변환되어 non-null value 존재
{ "user": [] } o 명시적으로 null으로 들어온 게 없으므로 null
{ "user": [null] } o null -> NULL 변환되어 non-null value 존재
{ "foo": "bar" } o "user" field 자체가 없음
  • 명시적으로 null으로 들어왔다는 건 아래 query로 확인 가능하다
    • 위에서 만든 test_index에 데이터를 넣어보자

      • 명시적 null
      PUT test_index/test_type/1
      {
      "user": null
      }
      
      • 명시적이 아닌 null
      PUT test_index/test_type/2
      {
        "user": []
      }
      
    • 전체 Documents 확인

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "2",
        "_score": 1,
        "_source": {
          "user": []
        }
      },
      {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user": null
        }
      }
    ]
  }
}
  • 검색
    • query

      GET test_index/_search
      {
        "query": {
          "term": {
            "user": "NULL" 
          }
        }
      }
      
    • 결과 : 위에서 _id가 1번인 document만 출력된다

 {
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "user": null
        }
      }
    ]
  }
}
@higee higee closed this as completed Nov 24, 2017
@higee higee self-assigned this Nov 24, 2017
@higee higee added the 5.6.4 label Jan 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant