栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticsearch-py 7.16.0新特征

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”:"…"
    }

未完待续
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/695826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号