5AMSUNG

[python] kibana 에서 쓰던 쿼리 조회 하기 본문

카테고리 없음

[python] kibana 에서 쓰던 쿼리 조회 하기

짝구이 2023. 4. 21. 11:33
반응형

키바나에서 쓰던 쿼리를 바로 조회 해보자

high level client 를 사용를 사용하고 있는 api 에서 쿼리를 추출해보면 아래과 같은 

형태가 나오는데 .. 

 

이걸 데이터를 뽑아서 증거품을 만들어야 하는 상황..

근데  python 소스에 string 으로 발라서 넣으려고 하니까

true 라든지.. 숫자라든지.. 

암튼 걸리는 것들이 있어

json 파일로 만들어서 불러오는 방법 사용 

 

json 파일 셈플

{
  "from": 0,
  "size": 20,
  "timeout": "60s",
  "query": {
    "bool": {
      "filter": [{
        "range": {
          "saleStartDt": {
            "from": null,
            "to": "now/m",
            "include_lower": true,
            "include_upper": true,
            "boost": 1.0
          }
        }
      }, {
        "range": {
          "saleEndDt": {
            "from": "now/m",
            "to": null,
            "include_lower": true,
            "include_upper": true,
            "boost": 1.0
          }
        }
      }, {
        "multi_match": {
          "query": "Y",
          "fields": ["docDispYn^1.0", "rsvDocDispYn^1.0"],
          "type": "best_fields",
          "operator": "OR",
          "slop": 0,
          "prefix_length": 0,
          "max_expansions": 50,
          "zero_terms_query": "NONE",
          "auto_generate_synonyms_phrase_query": true,
          "fuzzy_transpositions": true,
          "boost": 1.0
        }
      }, {
        "bool": {
          "should": [{
            "terms": {
              "itemStoreInfo.storeId": ["37", "20163"],
              "boost": 1.0
            }
          }, {
            "term": {
              "shipMethod": {
                "value": "TD_DLV",
                "boost": 1.0
              }
            }
          }, {
            "term": {
              "storeType": {
                "value": "DS",
                "boost": 1.0
              }
            }
          }],
          "adjust_pure_negative": true,
          "boost": 1.0
        }
      }, {
        "term": {
          "category.mcateCd": {
            "value": 200390,
            "boost": 1.0
          }
        }
      }, {
        "range": {
          "itemStoreInfo.weight": {
            "from": null,
            "to": 0.0,
            "include_lower": true,
            "include_upper": true,
            "boost": 1.0
          }
        }
      }],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  },
  "sort": [{
    "adultSort": {
      "order": "asc"
    }
  }, {
    "itemStoreInfo.weight": {
      "order": "desc"
    }
  }, {
    "itemStoreInfo.sortWeight": {
      "order": "desc"
    }
  }, {
    "itemStoreInfo.buyerCnt": {
      "order": "desc"
    }
  }, {
    "itemNo": {
      "order": "desc"
    }
  }]
}

code 

# -*- coding: utf-8 -*-

import time

from elasticsearch import Elasticsearch


def ingi():
    with open(QUERY_INGI) as index_file:
        source = index_file.read().strip()
        response = client.search(index=INDEX_NAME, body=source)

    print("== 인기와인 == ")
    for hit in response["hits"]["hits"]:
        print(hit["_source"]["itemNm"] +" ( " + str(hit["_source"]["itemNo"]) + "sortWeight - 가중치값 : "+ str(hit["_source"]["itemStoreInfo"]["sortWeight"] )+")")


def chuchun():
    with open(CHU_CHUN) as index_file:
        source = index_file.read().strip()
        response = client.search(index=INDEX_NAME, body=source)

    print("== 추천와인 == ")
    for hit in response["hits"]["hits"]:
        print(hit["_source"]["itemNm"] +" ( " + str(hit["_source"]["itemNo"]) + "weight - 가중치값 : " + str(hit["_source"]["itemStoreInfo"]["weight"] ) + "sortWeight - 가중치값 : "+ str(hit["_source"]["itemStoreInfo"]["sortWeight"] )+")")
    print()


def ipmun():
    with open(CHU_CHUN) as index_file:
        source = index_file.read().strip()
        response = client.search(index=INDEX_NAME, body=source)

    print("== 입문 == ")
    for hit in response["hits"]["hits"]:
        print(hit["_source"]["itemNm"] +" ( " + str(hit["_source"]["itemNo"]) + "sortWeight - 가중치값 : "+ str(hit["_source"]["itemStoreInfo"]["sortWeight"] )+")")
    print()


def ingi_wi():
    with open(INGI_WI) as index_file:
        source = index_file.read().strip()
        response = client.search(index=INDEX_NAME, body=source)

    print("== 인기위스키 == ")
    for hit in response["hits"]["hits"]:
        print(hit["_source"]["itemNm"] +" ( " + str(hit["_source"]["itemNo"]) + "sortWeight - 가중치값 : "+ str(hit["_source"]["itemStoreInfo"]["sortWeight"] )+")")
    print()


def chuchun_wi():
    with open(CHU_CHUN_WI) as index_file:
        source = index_file.read().strip()
        response = client.search(index=INDEX_NAME, body=source)

    print("== 추천위스키 == ")
    for hit in response["hits"]["hits"]:
        print(hit["_source"]["itemNm"] +" ( " + str(hit["_source"]["itemNo"]) + "sortWeight - 가중치값 : "+ str(hit["_source"]["itemStoreInfo"]["sortWeight"] )+")")
    print()


##### MAIN SCRIPT #####

if __name__ == '__main__':
    INDEX_NAME = "hyper-item"
    QUERY_INGI = "easypickup/sql/ingi.json"
    CHU_CHUN = "easypickup/sql/chuchun.json"
    IPMUN = "easypickup/sql/ipmun.json"
    INGI_WI = "easypickup/sql/ingi_wi.json"
    CHU_CHUN_WI = "easypickup/sql/chuchun_wi.json"

    client = Elasticsearch("https://elastic:elastic1!@category-es-qa.homeplus.kr:443/", ca_certs=False,
                           verify_certs=False)

    ingi()
    print()
    chuchun()
    print()
    ipmun()
    print()
    ingi_wi()
    print()
    chuchun_wi()
    print()

    print("Done.")

 

결과 값

 

반응형