如何在Elasticsearch中创建索引时定义默认值



我需要通过为字段分配默认值来在elasticsearch中创建索引。Ex,在蟒蛇3中,

request_body = {
"settings":{
"number_of_shards":1,
"number_of_replicas":1
},
"mappings":{
"properties":{
"name":{
"type":"keyword"
},
"school":{
"type":"keyword"
},
"pass":{
"type":"keyword"
}
}
}
}
from elasticsearch import Elasticsearch
es = Elasticsearch(['https://....'])
es.indices.create(index="test-index", ignore=400, body= request_body)

在上面的场景中,将使用这些字段创建索引。但我需要将默认值设置为";通过";如真。我能在这里做吗?

弹性搜索是无模式的。它允许任何数量的字段和字段中的任何内容,而不受任何逻辑约束。

在分布式系统中,完整性检查可能很昂贵,因此像RDBMS这样的检查在弹性搜索中不可用。

最好的方法是在客户端进行验证。

另一种方法是使用摄取

Ingest管道允许您在索引之前对数据执行常见转换。例如,您可以使用管道删除字段、从文本中提取值以及丰富数据。

**For testing**
POST _ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"script": {
"lang": "painless",
"source": "if (ctx.pass ===null) { ctx.pass='true' }"
}
}
]
},
"docs": [    
{
"_index": "index",
"_type": "type",
"_id": "2",
"_source": {
"name": "a",
"school":"aa"
}
}
]
}
PUT _ingest/pipeline/default-value_pipeline
{
"description": "Set default value",
"processors": [
{
"script": {
"lang": "painless",
"source": "if (ctx.pass ===null) { ctx.pass='true' }"
}
}
]
}
**Indexing document**
POST my-index-000001/_doc?pipeline=default-value_pipeline
{
"name":"sss",
"school":"sss"
}

**Result**
{
"_index" : "my-index-000001",
"_type" : "_doc",
"_id" : "hlQDGXoB5tcHqHDtaEQb",
"_score" : 1.0,
"_source" : {
"school" : "sss",
"pass" : "true",
"name" : "sss"
}
},

最新更新