天天看點

es 多條件查詢統計文法

添加映射

PUT /washing_process/_mapping
{
  "properties": {
      "distribution": {
        "properties": {
             "id":{
               "type":"long"
             },
             "distributionOrderId": {
                 "type":"long"
             },
             "washingCategoryId":{
               "type":"long"
             },
             "sendUserId":{
               "type":"long"
             },
             "sendUserName":{
               "type":"text"
             },
             "sendStatus":{
               "type":"integer"
             },
             "sendLogisticCode":{
               "type":"keyword"
             },
             "sendLogisticCodeThird":{
               "type":"keyword"
             }
         }
      }
  }
}
           

給已經存在的mapping 添加新的字段

PUT /washing_process/_mapping
{
  "properties": {
    "distribution":{
      "properties":{
        "createTime":{
            "type":"date"
         }
      }
    }
  }
}
           

多條件查詢,範圍查詢,分頁查詢文法

備注:term 和 match的差別 match 會進行評分,不精準,而term不會評分,精準查詢

GET washing_process/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "term": {
            "orderItem.storeId":0
          }
        },
        {
          "term": {
            "task.status":10
          }
        }
      ],
      "filter": {
          "range": {
              "task.startTime": {
                   "lte": "1571389204000"
                     }
                   }
        }
    }
  },
  "from":1,
  "size":10
}
           

多條件查詢後 進行分組統計文法

GET washing_process/_search
{
  "query":{
    "bool":{
      "must": [
        {
          "term": {
              "orderItem.storeId": "903"
          }
        }
      ], 
      "filter": {
        "range": {
          "task.createTime": {
            "gte": "1570261420000",
            "lte": "1572939820000"
          }
        }
      }
    }
  },
  "aggs": {
    "group_storeName": {
      "terms": {
        "field": "orderItem.storeName"
      },
      "aggs":{
        "group_type":{
          "terms": {
             "field": "task.type"
          },
          "aggs":{
            "group_status":{
              "terms": {
                 "field": "distribution.sendStatus"
               }
            }
          }
        }
      }
    }
  }
}
           

多條件查詢分頁,排序文法

GET trade_order/_search
{"query":
  {
    "bool":
     {"must":
       {"match_all":{}
       }
    }
  },
  "from":0,
  "size":10,
  "sort":[{"id":{"order":"desc"}}]
  
}