es6.8 curd

这篇博客演示了如何使用Elasticsearch进行索引创建、删除、文档插入、更新和搜索操作。通过示例展示了如何设置索引设置、映射、动态模式以及日期格式。还涵盖了批量操作、过滤查询、范围查询、聚合查询等高级功能。内容包括错误消息处理、ID字段、不同类型的字段(如text、keyword、long、date等)以及时间戳的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PUT /docname/_mapping/_doc
{
  "properties": {
    "xxx_type": {
      "type": "keyword"
    }
  }
}

DELETE /docname

PUT /docname
{
  "settings": {
    "number_of_shards": "5",
    "number_of_replicas": "0"
  },
  "mappings": {
    "_doc": {
      "dynamic": "strict",
      "properties": {
        "xname": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        },
        "aname": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        },
        "cname": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        },
        "err_msg": {
          "type": "text"
        },
        "hid": {
          "type": "long"
        },
        "oid": {
          "type": "long"
        },
        "oaid": {
          "type": "long"
        },
        "result": {
          "type": "byte"
        },
        "run_time": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "sname": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        },
        "create_time": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "update_time": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        }
      }
    }
  }
}

POST /docname/_doc
{
  "xname": "计划名称1",
  "run_time": "0000-01-01 00:00:00"
}

POST /docname/_doc/EippUYEBfjYkm2Z7kWAI/_update
{
  "doc":{
    "run_time" : "0000-01-01 00:00:00"
  }
}

POST _bulk
{"index":{"_index":"docname","_type":"_doc"}}
{"a":"计划名称2","b":"广告主名称2","c":"广告组名称2","e":"","h":2,"oa":2,"oc":2,"od":2,"response":"","result":2,"run_time":"0000-01-01 00:00:00","sn":"策略名称2"}
{"index":{"_index":"docname","_type":"_doc"}}
{"a":"计划名称3","b":"广告主名称3","c":"广告组名称3","e":"","h":3,"ocean_ad_id":3,"oa":3,"oc":3,"response":"","result":3,"run_time":"0000-01-03 00:00:00","sn":"策略名称3"}


GET /docname/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "hid": 2
        }
      }
    }
  }
}

GET /docname/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "hid": 2
          }
        },
        {
          "range": {
            "run_time": {
              "gte": "0000-01-01 00:00:00"
            }
          }
        }
      ]
    }
  }
}

GET /docname/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "run_time": {
            "gte": "0000-01-01 00:00:00"
          }
        }
      }
    }
  },
  "aggs": {
    "results": {
      "terms": {
        "field": "run_time"
      },
      "aggs": {
        "hids": {
          "terms": {
            "field": "hid"
          },
          "aggs": {
            "hits": {
              "top_hits": {}
            }
          }
        }
      }
    }
  }
}

GET /docname/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "hid": [
            3,
            2
          ]
        }
      }
    }
  },
  "aggs": {
    "response": {
      "composite": {
        "sources": [
          {
            "run_time": {
              "date_histogram": {
                "field": "run_time",
                "interval": "1s",
                "format": "yyyy-MM-dd HH:mm:ss",
                "order": "desc"
              }
            }
          },
          {
            "hid": {
              "terms": {
                "field": "hid",
                "order": "asc"
              }
            }
          }
        ]
      },
      "aggs": {
        "oaids": {
          "terms": {
            "field": "oaid",
            "order": {
              "_key": "asc"
            }
          },
          "aggs": {
            "success_num": {
              "filter": {
                "term": {
                  "result": 1
                }
              }
            },
            "data": {
              "top_hits": {
                "size": 10
              }
            }
          }
        },
        "paginate": {
          "bucket_sort": {
            "from": 0,
            "size": 10
          }
        }
      }
    },
    "total": {
      "cardinality": {
        "script": "doc['run_time'] +'-'+ doc['hid']"
      }
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值