from db.mod_db import EsBase
index = "treasure_name"
import datetime,time,os,sys

class NameEs:
    def __init__(self, es=None):
        if es is None:
            self.esBase = EsBase()
        else:
            self.esBase = es
    # def select_name(self,name_type,name_size,city,cate,size):
    #     query = {
    #           "from": 0,
    #           "size": size,
    #           "query": {
    #             "bool": {
    #               "must": [
    #                   {
    #                       "term": {
    #                           "name_type": name_type
    #                       }
    #                   },
    #                   {
    #                       "term": {
    #                           "name_size": name_size
    #                       }
    #                   },
    #                   {
    #                       "term": {
    #                           "gsb_city": city
    #                       }
    #                   },
    #                   {
    #                       "term": {
    #                           "gsb_company_cate": cate
    #                       }
    #                   },
    #                   {
    #                       "term": {
    #                           "state": 1
    #                       }
    #                   },
    #
    #               ]
    #             }
    #           },
    #           "sort": {
    #             "_script": {
    #               "script": "Math.random()",
    #               "type": "number",
    #               "order": "asc"
    #             }
    #           }
    #         }
    #     return self.esBase.select(index=index,body=query)
    def select_name(self,name_type,name_size,size):
        query = {
              "from": 0,
              "size": size,
              "query": {
                "bool": {
                  "must": [
                      {
                          "term": {
                              "name_type": name_type
                          }
                      },
                      {
                          "term": {
                              "name_size": name_size
                          }
                      },
                      {
                          "term": {
                              "state": 1
                          }
                      },

                  ]
                }
              },
              "sort": {
                "_script": {
                  "script": "Math.random()",
                  "type": "number",
                  "order": "asc"
                }
              }
            }
        return self.esBase.select(index=index,body=query)
    def select_null(self,size):
        query = {
            "query": {
                "bool": {
                    "must_not": {
                        "exists": {
                            "field": "name_type"
                        }
                    }
                }
            },
            "size":size
        }
        return self.esBase.select(index=index, body=query)


