Back-end development of Elasticsearch depth articles ---- queries and batch query

Depth queries and batch query

Paging depth

In paging query, use and size query and keywords from the same level. But when you start from 9999 and from size 10 is the time certainly will complain.
Because there is a max_result_window controls the parameters, the default value is 10000. If your company have to be ultra-deep those queries, you can modify this value

GET     /shop/_settings         #  用于查询相关的设置

PUT     /shop/_settings         #  设置相关参数
{ 
    "index.max_result_window": "20000"
}

Of course, you can also use the scroll scroll search, saving time and set the anchor

  • scroll = 1m, the equivalent of the session is a session time, holding time search context is 1 minute.
POST    /shop/_search?scroll=1m
{
    "query": { 
    	"match_all": {
    	}
    },  
    "sort" : ["_doc"], 
    "size":  5
}
# 上面的返回值会有一个_scroll_id的属性,将其copy

POST    /_search/scroll
{
    "scroll": "1m", 
    "scroll_id" : "your last scroll_id"  # 贴在这个点发
}

Batch operations

_mget batch query

# 之前的多id查询是这样的,这种是检索类的查询将返回相关的max_score和hint等元数据
POST /shop/_doc/_search
{
	"query":{
		"ids":{
			"type":"_doc",
			"values":["1001","1002","1003"]
		}
	}
}

# 但是_mget这个就是返回一个个doc,并且如果没有存在要查询的id时,也会显示一个json对象,里面有个属性是found会显示false,而有的id的found就是true
POST /shop/_doc/_mget
{
	"ids":["1001","1002","1003"]
}

bulk batch operation
and conventional input style is a little different. Not to json format, the car will have to cut each line (including the last line should be car)

{ action: { metadata }}\n
{ request body        }\n
{ action: { metadata }}\n
{ request body        }\n
...
  • { action: { metadata }}Type represents a batch operation, can be added, deleted or modified
  • \nIs the end of each line must fill out a specification, each row including the last line should write for the analytical es
  • { request body }Is requesting body, additions and modifications to operational needs, you do not need to delete the operation

action must be one of the following options:

  • create: If the document does not exist, create it. There is error. Abnormal error does not affect other operations.
  • index: create a new document or replace an existing document.
  • update: updates a part of the document.
  • delete: delete a document.
create new documents
POST    /_bulk
{"create": {"_index": "shop2", "_type": "_doc", "_id": "2001"}}
{"id": "2001", "nickname": "name2001"}
{"create": {"_index": "shop2", "_type": "_doc", "_id": "2002"}}
{"id": "2002", "nickname": "name2002"}
{"create": {"_index": "shop2", "_type": "_doc", "_id": "2003"}}
{"id": "2003", "nickname": "name2003"}

# 也可以将index和type写在url上面
POST    /shop/_doc/_bulk
{"create": {"_id": "2003"}}
{"id": "2003", "nickname": "name2003"}
{"create": {"_id": "2004"}}
{"id": "2004", "nickname": "name2004"}
{"create": {"_id": "2005"}}
{"id": "2005", "nickname": "name2005"}

index create or overwrite the original document
# 有就更新,没有就创建。而create的话,有就报错
POST    /shop/_doc/_bulk
{"index": {"_id": "2004"}}
{"id": "2004", "nickname": "index2004"}
{"index": {"_id": "2007"}}
{"id": "2007", "nickname": "name2007"}
{"index": {"_id": "2008"}}
{"id": "2008", "nickname": "name2008"}

update part of the document fields
POST    /shop/_doc/_bulk
{"update": {"_id": "2004"}}
{"doc":{ "id": "3004"}}
{"update": {"_id": "2007"}}
{"doc":{ "nickname": "nameupdate"}}

delete delete
# 不用跟request body
POST    /shop/_doc/_bulk
{"delete": {"_id": "2004"}}
{"delete": {"_id": "2007"}}

Comprehensive batch various operations
POST    /shop/_doc/_bulk
{"create": {"_id": "8001"}}
{"id": "8001", "nickname": "name8001"}
{"update": {"_id": "2001"}}
{"doc":{ "id": "20010"}}
{"delete": {"_id": "2003"}}
{"delete": {"_id": "2005"}}

Official Tip: Each cluster with a batch of their own peak performance threshold, when it reaches this threshold (number of operations batch), performance will drop

Published 118 original articles · won praise 16 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_39702831/article/details/105000309