(5) ElasticSearch data types and document CRUD operations

1. ES data type

Official document address: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html#_complex_datatypes

Core Data Types (Core Data Types):

The core data type is the most basic and commonly used data type of Elasticsearch, which is used to store most of the data. These core data types include:

  • Text (text): used to store long text data for full-text search and analysis.
  • Keyword: used to store exact values, used for filtering, aggregation and exact matching.
  • Numeric: Used to store numeric data, including integers, floating point numbers, etc.
  • Date (date): used to store date and time data. Since Json does not have a date type, es judges whether it is a date type by identifying whether the string conforms to the format defined by format.
  • Boolean (Boolean): used to store Boolean values.
  • Binary (binary): used to store binary data.
  • Range: The Range type is used to store numeric ranges, date ranges, IP ranges, etc. It allows you to perform range queries and range aggregation operations on fields.

Complex Data Types:

Complex data types allow storing structured data such as objects, arrays, and nested fields. These complex data types include:

  • Object: Used to store data of nested objects or complex structures.
  • Array (array): A list used to store multiple values. Just use [ ] to define it. All the values ​​in the array must be of the same data type. Arrays of mixed data types are not supported:

Use complex data types to create more flexible and complex data structures that support nested queries and aggregation operations.

Specialized Data Types:

Specialized data types are special-purpose data types provided by Elasticsearch to solve specific domain needs. These specialized data types include:

  • GeoPoint (geographic point): used to store geographic coordinate points, support geospatial search and distance calculation.
  • GeoShape (geographic shape): used to store complex geographic shape data, such as polygons, lines and points.
  • IP (IP address): used to store IP addresses, and supports IP address range query and aggregation operations. Essentially a long field

Specialized data types allow Elasticsearch to better handle data related to geolocation and network addresses

2. ES document CRUD operation

new document

1) New way to specify id:

PUT /my_index/_doc/1

{
    
    
  "title": "Elasticsearch",
  "content": "Elasticsearch is a distributed, RESTful search and analytics engine."
}

2) Add without specifying the id:

PUT /my_index/_doc

{
    
    
  "title": "Elasticsearch",
  "content": "Elasticsearch is a distributed, RESTful search and analytics engine."
}

3) The method of specifying the id is added to prevent mistaken modification due to the same id, and the operation type can be specified:

PUT /my_index/_doc/1?op_type=create

{
    
    
  "title": "Elasticsearch",
  "content": "Elasticsearch is a distributed, RESTful search and analytics engine."
}

4) Turn on automatic index creation:
Check the status of the auto_create_index switch:

GET /_cluster/settings

insert image description here
As shown in the figure above, there is no auto_create_index field, or false means it is not enabled. Enable
automatic index creation:

PUT _cluster/settings

{
    
    
 "persistent": {
    
    
 "action.auto_create_index": "true"
 }
}

After automatic index creation is enabled, the data format will automatically match the mapping.

view document

1) View by id

GET /my_index/_doc/1

2) View multiple

POST /my_index/_doc/mget

{
    
    
 "ids" : ["1", "2"]
}

modify document

1) Update document data

POST /my_index/_doc/1/_update

{
    
    
  "doc": {
    
    
    "content": "Elasticsearch is a distributed, RESTful search and analytics platform."
  }
}

2) Add a field to the _source field

POST my_index/_update/1

{
    
    
 "script": "ctx._source.lable = es"
}

3) To the _source field, delete a field

POST my_index/_update/1

{
    
    
 "script": "ctx._source.remove(\"lable \")"
}

4) Update the fields of the specified document according to the condition parameters

upsert When the specified document does not exist, the content contained in the upsert parameter will be inserted into the index as a new document; if the specified document exists, the ElasticSearch engine will execute the specified update logic.

POST my_index/_update/1

{
    
    
	 "script": {
    
    
		 "source": "ctx._source.lable+= params.lable",
		 "params": {
    
    
		 "lable": "good"
		 }
	 },
	 "upsert": {
    
    
		 "lable": "just so so"
		 }
}

delete document

DELETE /my_index/_doc/1

Guess you like

Origin blog.csdn.net/csdn570566705/article/details/131204208