elasticsearch基本操作

创建索引:

put http://192.168.102.100:9200/users?pretty
{
    "mappings":{
            "properties":{
                "userName":{
                    "type":"keyword"
                },
                "sex":{
                    "type":"keyword"
                },
                "age":{
                    "type":"integer"
                },
                "birthday":{
                    "type":"keyword"
                },
                "height":{
                    "type":"integer"
                }
        }
    }
    
}

查询索引基本信息:

get http://192.168.102.100:9200/users/_settings
{
    "users": {
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "users",
                "creation_date": "1646192811120",
                "number_of_replicas": "1",
                "uuid": "Lx8ZeIYBQribMGVkz91C7Q",
                "version": {
                    "created": "8000199"
                }
            }
        }
    }
}

查询索引mapping信息:

get http://192.168.102.100:9200/users/_mappings
{
    "users": {
        "mappings": {
            "properties": {
                "age": {
                    "type": "integer"
                },
                "birthday": {
                    "type": "keyword"
                },
                "height": {
                    "type": "integer"
                },
                "sex": {
                    "type": "keyword"
                },
                "userName": {
                    "type": "keyword"
                }
            }
        }
    }
}

插入数据

put http://192.168.102.100:9200/users/_doc/1
{
    "userName":"张三",
    "sex":"男",
    "age":25,
    "birthday":"1995-04-12",
    "height":175,
}

按ID查询数据:

get http://192.168.102.100:9200/users/_doc/1
{
    "_index": "users",
    "_id": "1",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "userName": "张三",
        "sex": "男",
        "age": 25,
        "birthday": "1995-04-12",
        "height": 175
    }
}

查询全部数据:

post http://192.168.102.100:9200/users/_search
{
    "query":{
        "match_all":{}
    },
    "from":0,
    "size":100
}
Response:
{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "users",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "userName": "张三",
                    "sex": "男",
                    "age": 25,
                    "birthday": "1995-04-12",
                    "height": 175
                }
            }
        ]
    }
}

按条件查询数据:

post http://192.168.102.100:9200/users/_search
{
    "query":{
        "match":{
            "userName":"张三"
        }
    },
    "from":0,
    "size":100
}
response:
{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "users",
                "_id": "1",
                "_score": 0.2876821,
                "_source": {
                    "userName": "张三",
                    "sex": "男",
                    "age": 25,
                    "birthday": "1995-04-12",
                    "height": 175
                }
            }
        ]
    }
}

`

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容