ElasticSearch is fast to use, basic index creation-increase type-mapping-insert data

ElasticSearch Quick Start (Basic Commands)

Example: A management system project based on a person introduces the process of ElasticSearch access in detail

1. Create index (database)

curl -X PUT http://10.210.40.59:9200/manage?pretty


-Let's not talk about the server ip port number --manage We need to create a project-based database
--pretty Let Elasticsearch beautify the output (pretty-print) JSON response for easier reading

View the created index (database) information

curl -X GET http://10.210.40.59:9200/manage?pretty

Delete the index (database)

curl -X DELETE http://10.210.40.59:9200/manage?pretty

2. Create type (table)

Create a user table in the database, of course, there are many table field attribute settings in addition to type, here is only a simple and quick example

curl -X PUT 10.210.40.59:9200/manage/_mapping/user?pretty -H 'Content-Type: application/json' -d '
{
    "properties": {
        "user_id": {
            "type": "long"
        },
        "user_name": {
            "type": "text"
        },
        "user_phone": {
            "type": "keyword"
        }
    }
}
'

It is necessary to mention the supported data types of table fields:

字符串类型:string(已过期)(5.x后改成了text类型 添加了keyword类型, 至于区别百度一下你就知道)
整数 : byte,short,integer,long
浮点数:float,double
布尔型: boolean
日期: date

View the created mapping information (table field details)

curl -X GET http://10.210.40.59:9200/manage/user/_mapping?pretty

Add mapping (add table field)

curl -X PUT 10.210.40.59:9200/manage/_mapping/user?pretty -d '{"properties":{"user_addr":{"type":"text"}}}'

3. Add document (insert data)

In order to facilitate the increase of data, there is no need to use the linux command, which is a bit troublesome. Add the
URL through postman : POST

# 指定id增加
 http://10.210.40.59:9200/manage/user/1?pretty
# 不指定id,es自动生成
http://10.210.40.59:9200/manage/user/?pretty

json parameter string

{
	"user_id":"10",
	"user_name":"Daniel",
	"user_phone":"13678909876",
	"user_addr":"北京"
}

Add specified id
Add without specifying id

4. Delete document (delete data)

URL: DELETE、POST

# 指定id删除 DELETE
http://10.210.40.59:9200/manage/user/10
# 查询式删除 POST
http://10.210.40.59:9200/manage/user/_delete_by_query?pretty

# json参数串
{
    "query": {
        "bool": {
            "filter": {
                "terms": {
                    "_id": ["1","AXGGuNaHdgsAZVXGg9_C"]
                }
            }
        }
    }
}

Query delete

If you want to know more about deletion, you can refer to the official documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html

5. Modify the document (modify the data)

doc file format modification

# POST
http://10.210.40.59:9200/manage/user/AXGGv5VOdgsAZVXGg-It/_update?pretty

# json
{
    "doc": {
        "user_name": "Claire",
        "user_phone": "13898765435"
    }
}

Document format modification

Script format modification

# POST
http://10.210.40.59:9200/manage/user/AXGGv5VOdgsAZVXGg-It/_update?pretty

# json
{
    "script": "ctx._source.user_addr = '成都'"
}

Script format modification

That's all for the quick start and use of ElasticSearch and the related operations of adding, deleting, and modifying database creation and data, and then write a separate record about the query.

Guess you like

Origin www.cnblogs.com/zcl-blog/p/12718974.html