elasticsearch常用的http操作

1、创建mapping

  PUT /my_index_name1/

{
  "settings": {
    "index": {
      "number_of_shards": "3",
      "number_of_replicas": "0",
      "max_result_window": 100000
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "id": {
          "type": "keyword",
          "index": "true"
        },
        "name": {
          "type": "text",
          "index": "true",
          "fields": {
            "keyword": {
              "ignore_above": 256,
              "type": "keyword"
            }
          }
        },
        "date_time": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
          "index": "true"
        },
        "children": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "children_ids": {
          "type": "keyword"
        }
      }
    }
  }
}

2、term查询、range查询、bool查询、分页、排序

POST http://localhost:9200/my_index_name2/   _search

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "is_delete": {
              "value": 0,
              "boost": 1
            }
          }
        },
        {
          "range": {
            "date_time": {
              "from": "2022-09-12 09:19:07",
              "to": null,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "version": true,
  "sort": [
    {
      "date_time": {
        "order": "desc"
      }
    }
  ]
}

3、聚合操作

{
  "from": 0,
  "size": 1,
  "query": {
    "range": {
      "date_time": {
        "from": "2022-09-12 09:48:59",
        "to": null,
        "include_lower": true,
        "include_upper": true,
        "boost": 1
      }
    }
  },
  "version": true,
  "aggregations": {
    "groupCount": {
      "terms": {
        "field": "group_col",
        "size": 10,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    }
  }
}

4、must存在查询

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "children_ids",
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "version": true,
  "sort": [
    {
      "date_time": {
        "order": "desc"
      }
    }
  ]
}

5、must、should与通配符查询

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "pub_time": {
              "from": null,
              "to": 1663035144435,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "should": [
        {
          "wildcard": {
            "name1.keyword": {
              "wildcard": "*Tax and digital*",
              "boost": 1
            }
          }
        },
        {
          "wildcard": {
            "name1.keyword": {
              "wildcard": "*Tax and digital*",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "minimum_should_match": "1",
      "boost": 1
    }
  },
  "version": true,
  "sort": [
    {
      "date_time": {
        "order": "desc"
      }
    }
  ]
}

6、date_histogram nested_group

{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date_time": {
              "from": 1627747200000,
              "to": null,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "range": {
            "date_time": {
              "from": null,
              "to": 1667145600000,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "version": true,
  "aggregations": {
    "nested_property_group": {
      "nested": {
        "path": "property"
      },
      "aggregations": {
        "property_group": {
          "terms": {
            "field": "property.id",
            "size": 1000,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_key": "asc"
              }
            ]
          },
          "aggregations": {
            "property_day_group": {
              "date_histogram": {
                "field": "date_time",
                "format": "MM-dd",
                "interval": "1d",
                "offset": 0,
                "order": {
                  "_key": "asc"
                },
                "keyed": false,
                "min_doc_count": 0
              }
            }
          }
        }
      }
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值