elasticsearch에 terminate_after 라는 매개변수가 있다. 리눅스 커맨드로 따지면, 'head/tail -n 1 file.txt | wc -l '정도 될 것 같다. 


데이터가 있는지 먼저 확인한다.


// http://localhost:9200/_search?q=name:Andrew


{

  "took": 29,

  "timed_out": false,

  "_shards": {

    "total": 1,

    "successful": 1,

    "skipped": 0,

    "failed": 0

  },

  "hits": {

    "total": {

      "value": 1,

      "relation": "eq"

    },

    "max_score": 0.2876821,

    "hits": [

      {

        "_index": "company",

        "_type": "_doc",

        "_id": "AOmqCWwB3Ptn8KUa8r_2",

        "_score": 0.2876821,

        "_source": {

          "name": "Andrew",

          "age": 45,

          "experienceInYears": 10

        }

      }

    ]

  }

}




terminate_after는 size 매개변수와 함께 사용된다.



못찾으면 다음과 같은 결과가 나온다.

// http://localhost:9200/_search?q=name:kimchi&size=0&terminate_after=1


{

  "took": 841,

  "timed_out": false,

  "terminated_early": false,

  "_shards": {

    "total": 1,

    "successful": 1,

    "skipped": 0,

    "failed": 0

  },

  "hits": {

    "total": {

      "value": 0,

      "relation": "eq"

    },

    "max_score": null,

    "hits": [

      

    ]

  }

}

hits.total.value의 값은 0이다. 



찾으면 아래와 같은 결과가 나온다. 


// http://localhost:9200/_search?q=name:Andrew&size=0&terminate_after=1


{

  "took": 13,

  "timed_out": false,

  "terminated_early": false,

  "_shards": {

    "total": 1,

    "successful": 1,

    "skipped": 0,

    "failed": 0

  },

  "hits": {

    "total": {

      "value": 1,

      "relation": "eq"

    },

    "max_score": null,

    "hits": [

      

    ]

  }

}


hits.total.value의 값이 1이다. 



size=0의 의미는 검색 결과에 관심이 없음을 의미한다. terminate_after=1은 검색 결과에 맞는 결과가 1개가 나타나면 더 이상 검색하지 않고 종료된다.




Posted by '김용환'
,