1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | ################## 집계(aggs) # SQL 의 group by 와 같이 데이터를 그룹화하고 통계를 얻는 기능. # ES는 검색과 동시에 집계 결과를 반환할 수 있다. # 집계만 표시하려면 size: 0 #################### Metrics Aggregations # 인덱스 생성 PUT order { "mappings": { "default": { "properties": { "lines": { "properties": { "amount": { "type": "double" }, "product_id": { "type": "integer" }, "quantity": { "type": "short" } } }, "purchased_at": { "type": "date" }, "sales_channel": { "type": "keyword" }, "salesman": { "properties": { "id": { "type": "integer" }, "name": { "type": "text" } } }, "status": { "type": "keyword" }, "total_amount": { "type": "double" } } } } } # _bulk API로 데이터 밀어넣기 POST order/default/_bulk # 첨부 json # 전체 판매금액과 판매량 집계 POST order/_search { "size": 0, "aggs": { "total_sales": { "stats": { "field": "total_amount" } }, "total_cnt": { "stats": { "field": "lines.quantity" } } } } # stats 를 개별적으로 가져오려면 stats 대신 value_count, min, max, avg, sum 을 넣는다 # 판매원의 숫자 # cardinality는 고유한 값의 개수를 계산한다. POST order/_search { "size": 0, "aggs": { "total_salesman": { "cardinality": { "field": "salesman.id" } } } } #################### Bucket Aggregations # default는 desc # 진행상태별 갯수 GET /order/_search { "size": 0, "aggs": { "status_terms": { "terms": { "field": "status", "order": { "_term": "asc" } } } } } # sql 로 표현하자면 아래와 같다. # SELECT status, COUNT(*) FROM order GROUP BY status ORDER BY COUNT(*) GET /order/_search { "size": 0, "aggs": { "status_terms": { "terms": { "field": "total_amount", "size": 10 } } } } # terms 는 갯수가 많은 숫자로 노출이 되지만 significant_terms 은 흥미롭거나 비정상적인 발생을 반환한다. 흥미로운 결과를 얻을 수 있다. #################### Nested Aggregations(중첩집계) # 상태별 금액 GET /order/_search { "size": 0, "aggs": { "status_terms": { "terms": { "field": "status" }, "aggs": { "status_stats": { "stats": { "field": "total_amount" } } } } } } # 판매원별 판매액 GET /order/_search { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "salesman.id" }, "aggs": { "id_total_amount": { "sum": { "field": "total_amount" } } } } } } # 기간 검색 추가 GET /order/_search { "size": 0, "query": { "range": { "purchased_at": { "gte": "2016-06-01T00:00:00Z", "lte": "2016-12-31T23:59:59" } } }, "aggs": { "status_terms": { "terms": { "field": "status" }, "aggs": { "status_stats": { "stats": { "field": "total_amount" } } } } } } #################### filter와 함께 사용하기 GET /order/_search { "size": 0, "aggs": { "high_value": { "filter": { "range": { "total_amount": { "gt": 100 } } }, "aggs": { "avg_amount": { "avg": { "field": "total_amount" } } } } } } #쿼리로 바꾸면 GET /order/_search { "size": 0, "query": { "range": { "total_amount": { "gt": 100 } } }, "aggs": { "avg_amount": { "avg": { "field": "total_amount" } } } } | cs |
orders-bulk.json
0.26MB
'Elasticsearch6' 카테고리의 다른 글
elasticsearch 6.* 기본기-4 한국어형태소분석기-nori (0) | 2018.10.26 |
---|---|
elasticsearch 6.* 기본기-2 Index (0) | 2018.10.22 |
elasticsearch 6.* 기본기-1 Search (0) | 2018.10.19 |
elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 4 (search) (0) | 2018.10.17 |
elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 3 (bulk insert) (0) | 2018.10.17 |