elasticsearch-py 7.16.0新特征

elasticsearch-py 7.16.0新特征,第1张

elasticsearch-py 7.16.0新特征 es.indices.create():
  1. 先创建一个配置

    config=
    {
      "settings": {
        "index": {
          "number_of_shards": 1,
          "number_of_replicas": 0
        }
      },
      "mappings": {
        "dynamic": "strict",
        "properties": {
          "id": {
            "ignore_above": 1024,
            "type": "keyword"
          },
          "body": {
            "type": "text",
            "analyzer": "english"
          }
        }
      }
    }
    
  2. 旧版参数

    es.indices.create(index=index, body=config)
    
  3. 新版参数

    es.indices.create(index=index, settings=config['settings'], mappings=config["mappings"])
    
  4. 作者原话
    The case of “make a JSON body an API request” is likely to be applicable elsewhere too (ingest.put_pipeline, transform.put_transform, search?) A raw JSON HTTP body is a transport-layer concept compared to “this is the index’s settings” and “this is the index’s mapping” being API-layer concepts.

    The goal of moving away from params and body is for the client to become more than “named URLs”: being able to catch issues with your request before it’s sent to Elasticsearch (required parameters, parameter types, using API structures and enums) which means encoding Elasticsearch API information into function signatures.

    I know usage of body is pervasive so I’m keeping the option to use body and params for almost all APIs in 8.x (search_template uses the params keyword) and when using these options the individual values will be picked out and expanded so they go through the same processing as if you’d passed the parameters directly:

es.create():
  1. 作用:在索引中增加文档,

  2. 基本参数:

    """
    index:				type:str 索引
    doc_type:			type(由于一个index只能有1个type,可不写)
    document:			type:dict增加的字段
    id:					type:str id
    """
    

由document替换了body,语义明确。

es.index():
  1. 作用:在索引中增加或者更新文档

  2. 基本参数:

    """
    index:				type:str 索引
    doc_type:			type(由于一个index只能有1个type,可不写)
    document:			type:dict增加的字段
    id:					type:str id
    """
    

由document替换了body,语义明确。

es.search():
  1. 作用:Returns results matching a query.(直接抄文档了)

  2. 基本参数:

    """
    index:				type:str 索引
    doc_type:			type(由于一个index只能有1个type,可不写)
    query:				type:dict增加的字段
    """
    

由query替换了body,语义明确。

es.update():
  1. 作用:部分更新文档

  2. 基本参数:

    index:				type:str 索引
    doc_type:			type(由于一个index只能有1个type,可不写)
    doc:				type:dict修改的字段
    

由doc替代了body,语义明确。

helper.bulk():
  1. 作用:批量增删改

  2. 基本参数:

    """
    client:			# es_obj
    actions:		#  *** 作
    """
    
  3. actions构造:
    1.删除
    {
    ‘_op_type’: ‘delete’,
    ‘_index’: ‘index-name’,
    ‘_id’: 42, # 这里id又可以传int类型了
    }
    2.更新
    {
    ‘_op_type’: ‘update’,
    ‘_index’: ‘index-name’,
    ‘_id’: 42,
    ‘doc’: {‘question’: ‘The life, universe and everything.’}
    }
    3.增加(默认)
    {
    ‘_index’: ‘index-name’,
    ‘_id’: 42,
    ‘_source’: {
    “title”: “Hello World!”,
    “body”: “…”
    }
    }
    如果你没写_source字段,那么会将所有字段pop出去作为插入的数据!
    也可以这么写:
    {
    “_index”: “index-name”,
    “_id”:43,
    “title”: “Hello World”,
    “body”:"…"
    }
    或者
    {
    “_index”: “index-name”,
    “title”: “Hello World”,
    “body”:"…"
    }

未完待续

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5695971.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存