python elasticsearch客户端在创建索引期间设置映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31635828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:20:24  来源:igfitidea点击:

python elasticsearch client set mappings during create index

pythonpyelasticsearchelasticsearch-py

提问by

I can set mappings of index being created in curl command like this:

我可以像这样设置在 curl 命令中创建的索引的映射:

{  
  "mappings":{  
    "logs_june":{  
      "_timestamp":{  
        "enabled":"true"
      },
      "properties":{  
        "logdate":{  
          "type":"date",
          "format":"dd/MM/yyy HH:mm:ss"
        }
      }
    }
  }
}

But I need to create that index with elasticsearch client in python and set mappings.. what is the way ? I tried somethings below but not work:

但是我需要在 python 中使用 elasticsearch 客户端创建该索引并设置映射..是什么方式?我在下面尝试了一些但不起作用:

self.elastic_con = Elasticsearch([host], verify_certs=True)
self.elastic_con.indices.create(index="accesslog", ignore=400)
params = "{\"mappings\":{\"logs_june\":{\"_timestamp\": {\"enabled\": \"true\"},\"properties\":{\"logdate\":{\"type\":\"date\",\"format\":\"dd/MM/yyy HH:mm:ss\"}}}}}"
self.elastic_con.indices.put_mapping(index="accesslog",body=params)

采纳答案by Val

You can simply add the mapping in the createcall like this:

您可以简单地在create调用中添加映射,如下所示:

from elasticsearch import Elasticsearch

self.elastic_con = Elasticsearch([host], verify_certs=True)
mapping = '''
{  
  "mappings":{  
    "logs_june":{  
      "_timestamp":{  
        "enabled":"true"
      },
      "properties":{  
        "logdate":{  
          "type":"date",
          "format":"dd/MM/yyy HH:mm:ss"
        }
      }
    }
  }
}'''
self.elastic_con.indices.create(index='test-index', ignore=400, body=mapping)

回答by Elzor

Well, there is easier way to do this with general python syntax:

好吧,有一种更简单的方法可以使用通用的 python 语法来做到这一点:

from elasticsearch import Elasticsearch
# conntect es
es = Elasticsearch([{'host': config.elastic_host, 'port': config.elastic_port}])
# delete index if exists
if es.indices.exists(config.elastic_urls_index):
    es.indices.delete(index=config.elastic_urls_index)
# index settings
settings = {
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
    },
    "mappings": {
        "urls": {
            "properties": {
                "url": {
                    "type": "string"
                }
            }
        }
     }
}
# create index
es.indices.create(index=config.elastic_urls_index, ignore=400, body=settings)

回答by James Murty

The Python API client can be a tricky to work with and it often requires you to provide the inner portions of JSON spec documentations to keyword arguments.

Python API 客户端使用起来可能很棘手,它通常需要您将 JSON 规范文档的内部部分提供给关键字参数。

For the put_mappingmethod, instead of providing it the full "mappings" JSON document you must give it the document_typeparameter and only the innerportion of the "mappings" document like so:

对于该put_mapping方法,您必须为其提供document_type参数和“映射”文档的内部部分,而不是为其提供完整的“映射”JSON 文档,如下所示:

self.client.indices.put_mapping(
    index="accesslog",
    doc_type="logs_june",
    body={
        "_timestamp": {  
            "enabled":"true"
        },
        "properties": {  
            "logdate": {  
                "type":"date",
                "format":"dd/MM/yyy HH:mm:ss"
            }
        }
    }
)

回答by Pari Rajaram

Another python client sample of how to raise field limit through create index

如何通过创建索引提高字段限制的另一个 python 客户端示例

from elasticsearch import Elasticsearch

es = Elasticsearch([{'host': config.elastic_host, 'port': 
config.elastic_port}])
settings = {
    'settings': {
        'index.mapping.total_fields.limit': 100000
    }
}

es.indices.create(index='myindex', body=settings)