如何在弹性搜索中强制执行必填字段? [英] How to enforce a required field in elastic search?

查看:84
本文介绍了如何在弹性搜索中强制执行必填字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在后端使用弹性搜索构建cms,我的团队决定使用弹性搜索.我是新来的.我主要将Mongoose与以前项目中的mongodb一起使用.在mongodb中,如果我错误地分配了一个字段或完全跳过了必填字段,则mongodb会引发错误.

I am building a cms using elastic search on the back end and my team has decided to use elastic search. I am new to it. I mostly use mongoose with mongodb from previous projects. In mongodb if I wrong assign a field or completely skip a required field mongodb throws an error.

有没有一种方法可以在Elasticsearch中强制执行必填字段?

Is there a way to enforce required fields in elasticsearch?

推荐答案

没有内置功能,可让您在映射中定义必填字段.许多人会建议您在客户端进行检查.

There is not built in functionality, that will allow you to define required/mandatory fields in the mappings. Many will recommend you to do checks on the client side.

但是,在Elasticsearch 5.x中,您可以通过使用Ingest节点来完成技巧.

However, in Elasticsearch 5.x you have the possibility to do the trick by using Ingest node.

您可以使用摄取节点在实际文档之前对文档进行预处理进行索引.此预处理由摄取节点进行拦截批量和索引请求,应用转换,然后将文档传递回索引API或批量API.

You can use ingest node to pre-process documents before the actual indexing takes place. This pre-processing happens by an ingest node that intercepts bulk and index requests, applies the transformations, and then passes the documents back to the index or bulk APIs.

要在索引之前对文档进行预处理,您可以定义一个管道指定一系列处理器.每个处理器都会转换文档.

To pre-process documents before indexing, you define a pipeline that specifies a series of processors. Each processor transforms the document in some way.

一个示例,显示了使用此方法的可能性.

An example, which shows the possibility of using this approach.

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "lang": "painless",
          "inline": "if (ctx.title == null) { throw new Exception('Document does not have the *title* field') }"
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_type": "type",
      "_id": "1",
      "_source": {
        "title": "Elasticsearch 101"
      }
    },
    {
      "_index": "index",
      "_type": "type",
      "_id": "2",
      "_source": {
        "company": "Elastic"
      }
    }
  ]
}

有关更多信息,请在这里查看- https://www.elastic.co/guide/zh-CN/elasticsearch/reference/5.2/ingest.html

For more information please take a look here - https://www.elastic.co/guide/en/elasticsearch/reference/5.2/ingest.html

这篇关于如何在弹性搜索中强制执行必填字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆