elasticsearch简单增删改查

创建索引

put http://localhost:9200/luoye/   
Content-Type  application/json
{
	"settings": {
		"index": {
			"number_of_shards": "2",   --分片数
			"number_of_replicas": "0"  --副本
		}
	}
}
-----------------------------------------------------------------------
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "indsex": "luoye"
}

删除索引

Delete  http://localhost:9200/luoye/   
-----------------------------------------------------------------------
{
    "acknowledged": true
}

插入数据

1.指定了id,方法用put和post都可以

put http://localhost:9200/luoye/people/01    

{
	 "name”":"kettle",
     "age":25,
     "wetchat":"1732650",
     "sex":"男",
      "Height":175.5

}
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "01",   
    "_version": 1,        --版本
    "result": "created",  --创建,如果再执行会变成updated, vsesion 会变成2 
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

2.未指定id 方法必须用post

post  http://localhost:9200/luoye/people 

{
	 "name”":"落叶",
     "age":25,
     "wetchat":"17326504*",
     "sex":"男",
      "Height":175.5

}
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "cMQ1kX4BOCtS3EjrIwsn",     --随机生成的
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

3.批量插入数据

post http://localhost:9200/luoye/people/_bulk

 {"create" :{ "_index":"luoye","_type":"people","_id":"2022"}}
 {"id":"2022","name”":"大哥哥","age": 25 ,"address":"深圳","wetchat":"17326","sex":"男"}
  {"create" :{ "_index":"luoye","_type":"people","_id":"2023"}}
 {"id":"2023","name”":"Henry","age":25  ,"address":"汕头","wetchat":"1732650","sex":"男"}
  {"create" :{ "_index":"luoye","_type":"people","_id":"2024"}}
 {"id":"2024","name”":"说好的幸福","age":25  ,"address":"深圳","wetchat":"17326504","sex":"男"}
  {"create" :{ "_index":"luoye","_type":"people","_id":"2025"}}
 {"id":"2025","name”":"痛怎么伪装","age":25  ,"address":"珠海","wetchat":"173265","sex":"男"}    --这个后面要加\n
                                                                                              --的换行符
                                                                                              
 -----------------------------------------------------------------------
 {
    "took": 69,
    "errors": false,
    "items": [
        {
            "create": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2022",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 5,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "create": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2023",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 19,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "create": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2024",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 20,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "create": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2025",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 6,
                "_primary_term": 1,
                "status": 201
            }
        }
    ]
}
 


更新数据

1.全局更新,会直接覆盖之前的,方法可用put或者post

 post/put http://localhost:9200/luoye/people/01
 
 {
	 "name”":"kettle",
     "age":25,
     "wetchat":"1732650",
     "sex":"男",
     "address":"深圳"    -Height换成了address,后面再查询查不到Height,相当于删掉之前的,再换成现在的,只是id不变
}
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "01",
    "_version": 3,            --版本变化
    "result": "updated",      ---更新
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

2.部分更新,方法 post/put都可以

post/put http://localhost:9200/luoye/people/01
{
  "doc":{
    "name":"落叶",
      "age":25
  } 
}
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "01",
    "_version": 5,              --版本
    "result": "updated",        --更新
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 1
}

删除数据

1.单条数据删除

Delete  http://localhost:9200/luoye/people/02
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "02",
    "_version": 2,
    "result": "deleted",         --成功为deleted,不成功为not_found
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

2.批量删除

 post http://localhost:9200/luoye/people/_bulk

{"delete" :{ "_index":"luoye","_type":"people","_id":"2022"}}
{"delete" :{ "_index":"luoye","_type":"people","_id":"2023"}}
{"delete" :{ "_index":"luoye","_type":"people","_id":"2024"}}
{"delete" :{ "_index":"luoye","_type":"people","_id":"2025"}}      --这里同样要有个换行符\n
-----------------------------------------------------------------------

{
    "took": 5,
    "errors": false,
    "items": [
        {
            "delete": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2022",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 7,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "delete": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2023",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 21,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "delete": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2024",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 22,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "delete": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2025",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 8,
                "_primary_term": 1,
                "status": 200
            }
        }
    ]
}
 

查询数据

1.直接查询


Get   http://localhost:9200/luoye/people/01
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "01",
    "_version": 1,
    "_seq_no": 8,
    "_primary_term": 1,
    "found": true,              ---存在
    "_source": {
        "name”": "落叶",
        "age": 25,
        "wetchat": "17326504*",
        "sex": "男",
        "Height": 175.5
    }
}
-----------------------------------------------------------------------
{
    "_index": "luoye",       
    "_type": "people",
    "_id": "011",               
    "found": false              --- 不存在
}

2.获取全部数据

  Get http://localhost:9200/luoye/people/_search
-----------------------------------------------------------------------
  {
    "took": 831,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "b8QykX4BOCtS3EjrcwuQ",
                "_score": 1.0,
                "_source": {
                    "name”": "kettle",
                    "age": 25,
                    "wetchat": "1732650",
                    "sex": "男",
                    "Height": 175.5
                }
            },
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "cMQ1kX4BOCtS3EjrIwsn",
                "_score": 1.0,
                "_source": {
                    "name”": "kettle",
                    "age": 25,
                    "wetchat": "1732650",
                    "sex": "男",
                    "Height": 175.5
                }
            },
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "01",
                "_score": 1.0,
                "_source": {
                    "name”": "落叶",
                    "age": 25,
                    "wetchat": "17326504*",
                    "sex": "男",
                    "Height": 175.5
                }
            }
        ]
    }
}

3.瘦身查询,有时我们不需要那么多字段,那么就可以指出只展现哪些字段就行

Get  http://localhost:9200/luoye/people/_search?_source=name”,age,wetchat
-----------------------------------------------------------------------
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "b8QykX4BOCtS3EjrcwuQ",
                "_score": 1.0,
                "_source": {
                    "name”": "kettle",
                    "age": 25,
                    "wetchat": "1732650"
                }
            },
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "cMQ1kX4BOCtS3EjrIwsn",
                "_score": 1.0,
                "_source": {
                    "name”": "kettle",
                    "age": 25,
                    "wetchat": "1732650"
                }
            },
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "01",
                "_score": 1.0,
                "_source": {
                    "name”": "落叶",
                    "age": 25,
                    "wetchat": "17326504*"
                }
            },
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "_search ",
                "_score": 1.0,
                "_source": {}
            }
        ]
    }
}

4.多个数据进行查询,某个数据不存在,不影响查询

post  http://localhost:9200/luoye/people/_mget

{
  "ids":["01","cMQ1kX4BOCtS3EjrIwsn","02"]    --02数据就不存在,不影响

}
-----------------------------------------------------------------------
{
    "docs": [
        {
            "_index": "luoye",
            "_type": "people",
            "_id": "01",
            "_version": 1,
            "_seq_no": 8,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name”": "落叶",
                "age": 25,
                "wetchat": "17326504*",
                "sex": "男",
                "Height": 175.5
            }
        },
        {
            "_index": "luoye",
            "_type": "people",
            "_id": "cMQ1kX4BOCtS3EjrIwsn",
            "_version": 1,
            "_seq_no": 1,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name”": "kettle",
                "age": 25,
                "wetchat": "1732650",
                "sex": "男",
                "Height": 175.5
            }
        },
        {
            "_index": "luoye",
            "_type": "people",
            "_id": "02",
            "found": false
        }
    ]
}

3.关键字搜索

方法1:

  get  http://localhost:9200/luoye/people/_search?q=name\”:落叶     --name“字段里面打多了一个中文的“所以加转义
  -----------------------------------------------------------------------
  {
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.2199391,
        "hits": [
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "01",
                "_score": 1.2199391,
                "_source": {
                    "name”": "落叶",
                    "age": 25,
                    "wetchat": "17326504*",
                    "sex": "男",
                    "Height": 175.5
                }
            }
        ]
    }
}

上一篇:单例模式之懒汉式


下一篇:JavaBean实体类