일치하는 Document가 필요한 게 아니라 집계할 특정 field만 필요하므로
query - size : 0 - aggs 으로 구성
GET <인덱스명>/_search
{
"query": {
… <쿼리 구문> …
},
"size" : 0,
"aggs": {
"<임의의 aggregation 1>": {
"<aggregation 종류>": {
… <aggreagation 구문> …
}
},
"<임의의 aggregation 2>": {
"<aggregation 종류>": {
… <aggreagation 구문> …
}
}
}
}
GET <인덱스명>*/_search?filter_path=aggregations.userIds.buckets.key
"_source": false
"query": {
"bool": {
"filter": [
{
"range": {
}
},
{
"bool": {
"should": [
{
// 생략
}
]
}
}
]
}
}
"aggs": {
"aggIds": {
"terms": {
"field": "aggField",
"size": 100000,
"include": {
"partition": 6,
"num_partitions": 8
}
}
}
}
문자열 검색 시 “일치” 조건과 “포함할 때” 조건을 구분해서 구현
문자열 검색이 필요한 field의 Type은 text 혹은 keyword인데 상황에 따라 선택
terms + keyword 타입
GET /user*/_search
{
"query": {
"bool": {
"filter": [
{
"terms": {
"id.keyword": ["grb1" , "grb2"]
}
}
]
}
}
}
GET /_analyze
{
"analyzer" : "standard",
"text" : "https://www.pxg.co.kr/main/main.asp"
}
{
"tokens" : [
{
"token" : "https",
"start_offset" : 0,
"end_offset" : 5,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "www.pxg.co.kr",
"start_offset" : 8,
"end_offset" : 21,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "main",
"start_offset" : 22,
"end_offset" : 26,
"type" : "<ALPHANUM>",
"position" : 2
},
{
"token" : "main.asp",
"start_offset" : 27,
"end_offset" : 35,
"type" : "<ALPHANUM>",
"position" : 3
}
]
}
GET /index_name*/_search
{
"query": {
"bool": {
"should": [
{
"wildcard": {
"memberId": "*user*"
}
},
{
"wildcard": {
"memberId": "*test*"
}
},
{
"query_string": {
"query": "user",
"default_field": "memberId",
"default_operator": "or"
}
}
]
}
}
}
ES 에서도 일부 sql문이 사용 가능합니다. 쿼리를 sql로 작성할 경우 팀원들이 이해 하기에 쉽겠지만 where in 같이 조금만 쿼리가 복잡해져도 사용이 불가능 하므로 본래 ES 쿼리로 작성하였습니다.
Query
POST _sql?format=txt
{
"query": """
SELECT count(distinct cookieId) FROM index
}
------------------------------------------------
Result
count(distinct cookieId)
4617305.0
Query
POST _sql?format=txt
{
"query": SELECT count(distinct cookieId) FROM index
where actionCd = 'PU' and id in
(
SELECT id FROM "userdata-8a4605e5c5d641b7869d9104222a6d38-*"
where actionCd = 'VC'
)
}
------------------------------------------------
Result
{
"error" : {
"root_cause" : [
{
"type" : "parsing_exception",
"reason" : "line 3:30: IN query not supported yet"
}
],
"type" : "parsing_exception",
"reason" : "line 3:30: IN query not supported yet"
},
"status" : 400
}
RDBMS의 cursor를 사용하는 것과 유사하다. scrollID는 고정되어있고 scrollId로 Search요청을 보낼 때마다 다른 데이터를 받을 수 있습니다.
기존에는 10,000개 이상의 document들에 대해 페이징을 적용할 때 scroll api를 사용하는 것이 권장되었지만 7 버전이 되면서부터 더이상 권장하지 않습니다. (We no longer recommend using the scroll API for deep pagination. If you need to preserve the index state while paging through more than 10,000 hits, use the search_after parameter with a point in time)
aggregation을 사용하는 것보다 빠르지도 않고 공식적으로 사용을 권장하지 않는다고 하니 사용 중단.
GET /user*/_search?scroll=1m
{
"query": {
"bool": {
"filter": [
{
"range": {
"regDtm": {
"gte": "2021-01-02T00:00:00.000Z",
"lte": "2021-12-03T23:59:59.000Z"
}
}
}
]
}
},
"size": 100
}
// 첫 request에서 받은 scroll_id 로 계속 받아올 수 있다.
GET _search/scroll?scroll_id=DnF1ZXJ5VGhlbkZldGNoAwAAAAAAl4uhFmJ2VGN1ekZqUUtTeFk1bXdLQTZFTEEAAAAAAJKm9BZ4VWo3azBsdVRXQ01JOWpJSjJ1OXhRAAAAAAAKk3AWcUxpVHFFVXZUQUdaMEZLMWhwQUtuUQ==
{
}