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
              }
            }
          }
        }
      }
    }
  }
}

你可能感兴趣的:(es,elasticsearch)