StoryCode

검색 실행

Elastic Search
반응형

[ 참조 ] https://www.elastic.co/guide/kr/elasticsearch/reference/current/gs-executing-searches.html

 

1. 검색명령구조

{

    "query":{} # WHERE 조건절

    "size": 10, # 가져올 결과건수

    "_source":["column1", "column2"] # SELECT 컬럼 목록

    "sort":[{"column1":"asc"}] # 정렬

}

 

2. 결과 구조

{

    "took": 걸린시간,

    "timed_out":false,

    "_shards": {},

    "hits":

        "total": 35,

        "max_score" : 

        "hits": [

                    {

                        "_score": # 결과가 얼마나 QUERY 와 매칭되는지

                    },

                    {

                    }

                ]

}

 

 

3. 검색명령.query 조건절 구조

 

{"match":{"account_number":20}}

{"match":{"address":"maill"}} # address에 mill 이라는 용어가 있는 모든 계정 반환

{"match":{"address":"maill lane"}} # address에 mill 혹은 lane 이라는 용어가 있는 모든 계정 반환

{"match_phrase":{"address":"maill lane"}} # address에 "mill lane" 이라는 문구가 있는 모든 계정 반환

{"bool":{"must":[{"match":{"address":"mill"}},{"match":{"address":"lane"}}]}} # address 에 mill 과 lane 이 있는 모든 계정 반환

{"bool":{"must_not":[{"match":{"address":"mill"}},{"match":{"address":"lane"}}]}} # address 에 mill 과 lane 이 없는 모든 계정 반환

{"bool":{"must":[{"match":{"age":"40"}}],{"must_not":[{"match":{"state":"ID"}}]}} # age 가 40 이고, state 가 ID 가 아닌 모든 계정 반환

{"bool":{"should":[{"match":{"address":"mill"}},{"match":{"address":"lane"}}]}} # address 에 mill 혹은 lane 이 있는 모든 계정 반환

 

4. 검색명령.query.filter

{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

 

5.검색명령.집계

 

- STATE 별로 doc_count = COUNT 결과만 보기.

 size:0 를 설정하여, 검색 적중은 제외하고, 집계만 결과로 본다.

GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      }
    }
  }
}

 

위 집계는 개념상 아래 SQL 과 비슷하다.

SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC

 

{
  "took": 29,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_state" : {
      "doc_count_error_upper_bound": 20,
      "sum_other_doc_count": 770,
      "buckets" : [ {
        "key" : "ID",
        "doc_count" : 27
      }, {
        "key" : "TX",
        "doc_count" : 27
      }]
    }
  }
}

 

- STATE 별로 평균balance 얻기

GET /bank/_search

{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}

 

결과

{
    "took": 22,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1000,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "group_by_state": {
            "doc_count_error_upper_bound": 20,
            "sum_other_doc_count": 770,
            "buckets": [
                {
                    "key": "ID",
                    "doc_count": 27,
                    "average_balance": {
                        "value": 24368.777777777777
                    }
                },
                {
                    "key": "TX",
                    "doc_count": 27,
                    "average_balance": {
                        "value": 27462.925925925927
                    }
                },
                {
                    "key": "AL",
                    "doc_count": 25,
                    "average_balance": {
                        "value": 25739.56
                    }
                },
                {
                    "key": "MD",
                    "doc_count": 25,
                    "average_balance": {
                        "value": 24963.52
                    }
                },
                {
                    "key": "TN",
                    "doc_count": 23,
                    "average_balance": {
                        "value": 29796.782608695652
                    }
                },
                {
                    "key": "MA",
                    "doc_count": 21,
                    "average_balance": {
                        "value": 29726.47619047619
                    }
                },
                {
                    "key": "NC",
                    "doc_count": 21,
                    "average_balance": {
                        "value": 26785.428571428572
                    }
                },
                {
                    "key": "ND",
                    "doc_count": 21,
                    "average_balance": {
                        "value": 26303.333333333332
                    }
                },
                {
                    "key": "ME",
                    "doc_count": 20,
                    "average_balance": {
                        "value": 19575.05
                    }
                },
                {
                    "key": "MO",
                    "doc_count": 20,
                    "average_balance": {
                        "value": 24151.8
                    }
                }
            ]
        }
    }
}

 

 

 

 

 

 

- 연령대별 성별 평균balance 얻기

GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_age": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "to": 50
          }
        ]
      },
      "aggs": {
        "group_by_gender": {
          "terms": {
            "field": "gender.keyword"
          },
          "aggs": {
            "average_balance": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  }
}

 

결과

{
    "took": 63,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1000,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "group_by_age": {
            "buckets": [
                {
                    "key": "20.0-30.0",
                    "from": 20,
                    "to": 30,
                    "doc_count": 451,
                    "group_by_gender": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "M",
                                "doc_count": 232,
                                "average_balance": {
                                    "value": 27374.05172413793
                                }
                            },
                            {
                                "key": "F",
                                "doc_count": 219,
                                "average_balance": {
                                    "value": 25341.260273972603
                                }
                            }
                        ]
                    }
                },
                {
                    "key": "30.0-40.0",
                    "from": 30,
                    "to": 40,
                    "doc_count": 504,
                    "group_by_gender": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "F",
                                "doc_count": 253,
                                "average_balance": {
                                    "value": 25670.869565217392
                                }
                            },
                            {
                                "key": "M",
                                "doc_count": 251,
                                "average_balance": {
                                    "value": 24288.239043824702
                                }
                            }
                        ]
                    }
                },
                {
                    "key": "40.0-50.0",
                    "from": 40,
                    "to": 50,
                    "doc_count": 45,
                    "group_by_gender": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "M",
                                "doc_count": 24,
                                "average_balance": {
                                    "value": 26474.958333333332
                                }
                            },
                            {
                                "key": "F",
                                "doc_count": 21,
                                "average_balance": {
                                    "value": 27992.571428571428
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

반응형