如何为Python Elasticsearch mSearch创建请求主体 [英] How to create request body for Python Elasticsearch mSearch

查看:111
本文介绍了如何为Python Elasticsearch mSearch创建请求主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行多搜索请求在Elasticsearch Python客户端上.我可以正确运行单数搜索,但无法弄清楚如何格式化对msearch的请求.根据文档,请求的主体需要格式化为:

请求定义(元数据搜索请求定义对),如下换行符分隔的字符串或要序列化的字典序列(每行一个).

创建此请求正文的最佳方法是什么?我一直在寻找示例,但似乎找不到任何示例.

解决方案

如果您遵循

如您所见,最终请求是由几个req_unit构造的.每个req_unit构造如下所示:

  request_header(关于索引名,可选映射类型,搜索类型等的搜索控件)\ nreqeust_body(涉及有关此请求的查询详细信息)\ n 

要序列化的命令的顺序与前一种方法几乎相同,只是不需要将其转换为字符串:

  def msearch():es = get_es_instance()请求= []req_head = {'index':'my_test_index','type':'doc_type_1'}req_body = {'query':{'term':{'text':'bag'}},'from':0,'size':2}request.extend([req_head,req_body])req_head = {'index':'my_test_index','type':'doc_type_2'}req_body = {'query':{'range':{'price':{'gte':100,'lt':300}}}},'from':0,'size':2}request.extend([req_head,req_body])resp = es.msearch(正文=请求) 

这里是它返回的结构.阅读有关 msearch 的更多信息..>

I'm trying to run a multi search request on the Elasticsearch Python client. I can run the singular search correctly but can't figure out how to format the request for a msearch. According to the documentation, the body of the request needs to be formatted as:

The request definitions (metadata-search request definition pairs), as either a newline separated string, or a sequence of dicts to serialize (one per row).

What's the best way to create this request body? I've been searching for examples but can't seem to find any.

解决方案

If you follow the demo of official doc(even thought it's for BulkAPI) , you will find how to construct your request in python with the Elasticsearch client:

Here is the newline separated string way:

def msearch():
    es = get_es_instance()

    search_arr = []
    # req_head
    search_arr.append({'index': 'my_test_index', 'type': 'doc_type_1'})
    # req_body
    search_arr.append({"query": {"term" : {"text" : "bag"}}, 'from': 0, 'size': 2})

    # req_head
    search_arr.append({'index': 'my_test_index', 'type': 'doc_type_2'})
    # req_body
    search_arr.append({"query": {"match_all" : {}}, 'from': 0, 'size': 2})

    request = ''
    for each in search_arr:
        request += '%s \n' %json.dumps(each)

    # as you can see, you just need to feed the <body> parameter,
    # and don't need to specify the <index> and <doc_type> as usual 
    resp = es.msearch(body = request)

As you can see, the final-request is constructed by several req_unit. Each req_unit construct shows below:

request_header(search control about index_name, optional mapping-types, search-types etc.)\n
reqeust_body(which involves query detail about this request)\n

The sequence of dicts to serialize way is almost same with the previous one, except that you don't need to convert it to string:

def msearch():
    es = get_es_instance()

    request = []

    req_head = {'index': 'my_test_index', 'type': 'doc_type_1'}
    req_body = {
        'query': {'term': {'text' : 'bag'}}, 
        'from' : 0, 'size': 2  }
    request.extend([req_head, req_body])

    req_head = {'index': 'my_test_index', 'type': 'doc_type_2'}
    req_body = {
        'query': {'range': {'price': {'gte': 100, 'lt': 300}}},
        'from' : 0, 'size': 2  }
    request.extend([req_head, req_body])

    resp = es.msearch(body = request)

Here is the structure it returns. Read more about msearch.

这篇关于如何为Python Elasticsearch mSearch创建请求主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆