一、插入文档
-
PUT /<target>/_doc/<_id>
在插入文档的时候,就指定文档的
Id
。如果插入的文档id
已经存在,则会更新文档,并且_version
字段的值会加一@Test public void addDocumentTest() { String baseUrl = "http://192.168.1.51:9200"; Person person = new Person(); String id = UUID.randomUUID().toString(); person.setId(id); person.setName("xiaoxiao"); person.setAge(18); person.setSalary(1.23); // http://192.168.1.51:9200/test-index/_doc/abcd String url = baseUrl + "/" + index + "/_doc/" + id; String insertResult = RestClient.put(url, JSON.toJSONString(person), headerMap); System.out.println(insertResult); String queryResult = EsUtilsWithAuth.queryAll(index); System.out.println(queryResult); }
-
POST /<target>/_doc/
在插入文档的时候,不指定文档
Id
,由ES生成文档的Id@Test public void addDocumentTest() { String baseUrl = "http://192.168.1.51:9200"; Person person = new Person(); String id = UUID.randomUUID().toString(); person.setId(id); person.setName("xiaoxiao"); person.setAge(18); person.setSalary(1.23); // http://192.168.1.51:9200/test-index/_doc String url = baseUrl + "/" + index + "/_doc"; String insertResult = RestClient.post(url, JSON.toJSONString(person), headerMap); System.out.println(insertResult); String queryResult = EsUtilsWithAuth.queryAll(index); System.out.println(queryResult); }
-
PUT /<target>/_create/<_id>
如果待插入的文档Id已经存在,则会出现version冲突
@Test public void addDocumentTest() { String baseUrl = "http://192.168.1.51:9200"; Person person = new Person(); String id = UUID.randomUUID().toString(); person.setId(id); person.setName("xiaoxiao"); person.setAge(18); person.setSalary(1.23); // http://192.168.1.51:9200/test-index/_create/abcd String url = baseUrl + "/" + index + "/_create/" + id; String insertResult = RestClient.put(url, JSON.toJSONString(person), headerMap); System.out.println(insertResult); String queryResult = EsUtilsWithAuth.queryAll(index); System.out.println(queryResult); }
-
POST /<target>/_create/<_id>
如果待插入的文档Id已经存在,则会出现version冲突
@Test public void addDocumentTest() { String baseUrl = "http://192.168.1.51:9200"; Person person = new Person(); String id = UUID.randomUUID().toString(); person.setId(id); person.setName("xiaoxiao"); person.setAge(18); person.setSalary(1.23); // http://192.168.1.51:9200/test-index/_create/abcd String url = baseUrl + "/" + index + "/_create/" + id; String insertResult = RestClient.post(url, JSON.toJSONString(person), headerMap); System.out.println(insertResult); String queryResult = EsUtilsWithAuth.queryAll(index); System.out.println(queryResult); }
二、简单查询文档
官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html
-
GET <index>/_doc/<_id>
根据id查询文档信息
GET /test-index-02/_doc/1
{ "_index" : "test-index-02", "_type" : "_doc", "_id" : "1", "_version" : 1, "_seq_no" : 25, "_primary_term" : 5, "found" : true, "_source" : { "id" : 3, "name" : "xiao3", "age" : 18 } }
-
HEAD <index>/_doc/<_id>
判断文档是否存在
HEAD /test-index-02/_doc/1
-
GET <index>/_source/<_id>
只查询具体数据
GET /test-index-02/_source/1
{ "id" : 3, "name" : "xiao3", "age" : 18 }
-
HEAD <index>/_source/<_id>
判断文档是否存在
HEAD /test-index-02/_source/1
-
_mget
官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html
在多个索引下查询数据
GET /_mget { "docs": [ { "_index": "test-index-01", "_id": 1 }, { "_index": "test-index-02", "_id": 2 } ] }
在一个索引下查询数据
GET /test-index-02/_mget { "docs": [ { "_id": 1 }, { "_id": 2 } ] }
三、修改文档
-
更新执行的字段
POST /test-index-02/_update/560405c8-a4c9-4059-8424-e5ba05cff44f { "doc": { "name":"xiao1" } }
@Test public void updateDocumentTest() { String id = "560405c8-a4c9-4059-8424-e5ba05cff44f"; String url = baseUrl + "/" + index + "/_update/" + id; Map<String, String> paramMap = new HashMap<>(); paramMap.put("name", "xiao2"); Map<String, Object> docMap = new HashMap<>(); docMap.put("doc", paramMap); String updateResult = RestClient.post(url, JSON.toJSONString(docMap), headerMap); System.out.println(updateResult); }
-
upsert
更新-
如果需要更新的文档存在:
- 如果
doc
中的字段存在,则更新该字段 - 如果
doc
中的字段不存在,但是开启了动态mapping
,则新增该字段;
- 如果
-
如果文档不存在,则插入
upsert
中的字段
POST /test-index-02/_update/1 { "doc":{ "address": "chengdu" }, "upsert": { "sex":"woman" } }
使用
upsert
更新数据update test-index-02._doc set address = 'beijing' where address = 'chengdu'
POST /test-index-02/_update_by_query { "query": { "term": { "address":"chengdu" } }, "script": { "source": "ctx._source.address = 'beijing'", "lang": "painless" } }
-
四、删除文档
-
通过id删除文档
DELETE /test-index-02/_doc/3
-
delete_by_query
POST /test-index-02/_delete_by_query { "query":{ "term":{ "address": "beijing" } } }
五、批量操作
POST _bulk
{"create": {"_index": "test", "_id":5}}
{"field5": "value5"}
{"update": {"_index": "test", "_id": 5}}
{"doc": {"field5": "value6"}}
{"index": {"_index": "test", "_id": 5}}
{"field5": "value7"}
{"delete": {"_index": "test", "_id": 5}}
六、使用sql查询ES数据
format
可以选择json
或者txt
POST /_sql?format=txt
{
"query": """
SELECT speaker, count(1) as speaker_count FROM "shakespeare" group by speaker order by speaker_count desc
"""
}
@Test
public void sqlTest() {
String url = baseUrl + "/_sql?format=txt";
Map<String, String> queryBody = new HashMap<>();
queryBody.put("query", "SELECT speaker, count(1) as speaker_count FROM shakespeare group by speaker order by"
+ " speaker_count desc");
String result = RestClient.post(url, JSON.toJSONString(queryBody), headerMap);
System.out.println(result);
}
将sql
转换成DSL
:
POST /_sql/translate
{
"query": """
SELECT speaker, count(1) as speaker_count FROM "shakespeare" group by speaker order by speaker_count desc
"""
}
{
"size" : 0,
"_source" : false,
"aggregations" : {
"groupby" : {
"composite" : {
"size" : 1000,
"sources" : [
{
"7cd992d7" : {
"terms" : {
"field" : "speaker.keyword",
"missing_bucket" : true,
"order" : "asc"
}
}
}
]
}
}
}
}