IT/Elasticsearch

Elastic Search DSL search query 기초

준나이 2021. 4. 19. 13:17

index 정보 호출

# es 내에 설치된 모든 index(=table) 정보 호출
GET _cat/indices

mapping 정보 호출

# index의 mapping(=schema) 정보 호출
GET dev_recommend_storage_cst_mart/_search

search query - match_all

# index 내 모든 documents return
GET dev_recommend_storage_prd_mart/_search
{
  "query": {
    "match_all": {}
  }
}
# GET dev_recommend_storage_prd_mart/_search 와 동일

search query - term (query vs. filter)

# score가 반환되는 일반 query
# gendr_cd가 "F" documents return
GET dev_recommend_storage_cst_mart/_search
{
  "query": {
    "term": {"gendr_cd": "F"}
  }
}

# score가 반환 안되는 filter query
# gendr_cd가 "F" documents return
GET dev_recommend_storage_cst_mart/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {"gendr_cd": "F"}
      } 
    }
  }
}

search query - terms

# score가 반환되는 일반 query
# gendr_cd가 "F", "M"인 documents return
GET dev_recommend_storage_cst_mart/_search
{
  "query": {
    "terms": {"gendr_cd": ["M", "F"]}
  }
}

search query - ids

# search query : index 내 _id값이 일치하는 document 검색 
GET dev_recommend_storage_cst_mart/_search
{
  "query": {
    "ids": {"values": "1203925"} # list로 arguments를 넘기면 복수개 검색
  }
}

search query - _source

# search query : 원하는 field("cust_no", "gendr_cd", "age5_unit_cd")만  리턴
GET dev_recommend_storage_cst_mart/_search
{
  "_source": ["cust_no", "gendr_cd", "age5_unit_cd"], 
  "query": {
    "ids": {"values": "1203925"} # list로 arguments를 넘기면 복수개 검색
  }
}

search query - compound 1 (query vs. filter)

GET dev_recommend_storage_prd_mart/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": {"age_rank1": "32"}},
        {"term": {"man_prd": 0}},
        {"term": {"prop": "100%"}},
        {"range": {"num": {"gte": 50}}}
      ]
    }
  }
}

GET dev_recommend_storage_prd_mart/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {"term": {"age_rank1": "32"}},
            {"term": {"man_prd": 0}},
            {"term": {"prop": "100%"}},
            {"range": {"num": {"gte": 50}}}
          ]
        }
      }
    }
  }
}

# https://www.elastic.co/guide/en/elasticsearch/reference/6.8/compound-queries.html

search query - compound2 (query vs. filter)

GET dev_recommend_storage_prd_mart/_search
{
  "query": {
    "bool": {
      "must": [
          {"terms": {"age_rank1": ["32", "31"]}},
          {"range": {"num": {"gte": 50, "lt": 100}}}
        ],
        "must_not": [
          {"term": {"man_prd": 1}}
        ],
        "should": [
          {"term": {"prop": "100%"}},
          {"terms": {"age_rank2": ["32", "31"]}}
        ]
    }
  }
}

GET dev_recommend_storage_prd_mart/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
              {"terms": {"age_rank1": ["32", "31"]}},
              {"range": {"num": {"gte": 50, "lt": 100}}}
            ],
            "must_not": [
              {"term": {"man_prd": 1}}
            ],
            "should": [
              {"term": {"prop": "100%"}},
              {"terms": {"age_rank2": ["32", "31"]}}
            ]
        }  
      }
    }
  }
}

# https://www.elastic.co/guide/en/elasticsearch/reference/6.8/compound-queries.html

search query - script

# prop : %제거 → int 변환 → 0.01 곱셈 → 0.5보다 작은 경우 리턴
GET dev_recommend_storage_prd_mart/_search
{
  "query": {
    "script": {
      "script": "Integer.parseInt(doc['prop'].value.replace('%', '')) * 0.01 < 0.5"
    }
  }
}

참고 : search query - function_score

GET dev_recommend_storage_cst_mart/_search
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "gauss": {
            "avg_prc": {
              "origin": 156465,
              "scale": 2000
            }
          } 
        }  
      ]
    }
  }
}
# functions : 다수의 function을 다수의 field에 적용할 수 있음 
# function 종류 : gauss, exp, linear 등이 존재
# 각 field에 대한 score를 어떻게 종합해서 보여줄지 정할 수 있음 (min, max, avg, sum)
# https://www.elastic.co/guide/en/elasticsearch/reference/6.8/query-dsl-function-score-query.html

참고 : search query - script score

GET dev_recommend_storage_prd_mart/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": {
            "bool": {
              "must": [
                {"term": {"age_rank1": "61"}},
                {"term": {"man_prd": 0}},
                {"term": {"prop": "100%"}},
                {"range": {"num": {"gte": 50}}}
              ]
            }
          }
        }
      },
      "functions": [{
          "script_score" : {
            "script" : { 
              "source": """
                if (doc['age_rank2'].value != "0" ) {
                  71 - Math.abs(Integer.parseInt(doc['age_rank2'].value) - Integer.parseInt(doc['age_rank1'].value))}
                else { 0  }"""}},
          "weight": 0.5
        },{
          "script_score" : {
            "script" : { 
              "source": """
                if (doc['age_rank3'].value != "0" ) {
                  71 - Math.abs(Integer.parseInt(doc['age_rank3'].value) - Integer.parseInt(doc['age_rank1'].value))}
                else {0}"""}},
          "weight": 0.3
        }
      ], 
      "score_mode": "sum", 
      "boost_mode":"sum"
    }
  }
}