elasticsearch - 弹性搜索:将值限制为枚举列表
问题描述
寻找一种将 ElasticSearch 的索引映射中的字段限制为已知单词列表的方法。
/myIndex
{ "mapping": {
"demo1": {
"type": "keyword",
"allowed_values": [ "a", "b", "c" ]
}
}
好像没有type: enum
可用的字段。有谁知道如何实现这一目标?
解决方案
弹性搜索是无模式的。它允许任意数量的字段和字段中的任何内容,而没有任何逻辑约束。
在分布式系统中,完整性检查可能很昂贵,因此像 RDBMS 这样的检查在弹性搜索中不可用。
最好的方法是在客户端进行验证。
另一种方法是使用摄取
摄取管道可让您在索引之前对数据执行常见转换。例如,您可以使用管道删除字段、从文本中提取值并丰富您的数据。
For testing
POST _ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"script": {
"lang": "painless",
"source": "if (ctx.demo1 !='a' && ctx.demo1 !='b' && ctx.demo1 !='c') { throw new Exception('Add valid values') }"
}
}
]
},
"docs": [
{
"_index": "index",
"_id": "1",
"_source": {
"demo1": "a"
}
},
{
"_index": "index",
"_id": "2",
"_source": {
"demo1": "d"
}
}
]
}
Ingest pipeline
PUT _ingest/pipeline/check-value_pipeline
{
"description": "Check if demo field has valid values",
"processors": [
{
"script": {
"lang": "painless",
"source": "if (ctx.demo1 !='a' && ctx.demo1 !='b' && ctx.demo1 !='c') { throw new Exception('Add valid values') }"
}
}
]
}
This will throw error
POST index115/_doc?pipeline=check-value_pipeline
{
"demo1":"d"
}
推荐阅读
- r - Aggregate rows with conditions, sum quantitative values, and keep character values of the max
- android - APK分析使用MobSF(Mobile-Security-Framework-MobSF)框架,显示代码分析为空
- python - 用反斜杠 pandas 分隔列值
- android - Android Q 中的内部应用程序音频捕获不适用于某些 android 设备
- powershell - 在 Windows Server 2008 R2 中使用 New-WebVirtualDirectory
- javascript - 滚动或单击菜单外时关闭导航
- pandas - 具有重叠坐标的 Pandas double(?) groupby
- php - 在 PHP/mysql 中强匹配街道地址的算法
- node.js - NPM:在带有 nvm 的 Windows 10 上运行 npm -v 时找不到 npm-cli.js
- javascript - 需要查看从成功数据到html表格的整个列表元素。(需要查看列表中的所有元素)