Home > database >  Is there any built-in function from Elasticsearch to prevent indexing document on specified conditio
Is there any built-in function from Elasticsearch to prevent indexing document on specified conditio

Time:03-25

I'm implementing the multi-tenancy system that use Elasticsearch. A single index keeps all tenants documents. Because it has the same fields for all tenants. I have a field to identify the tenant like this.

{
  tenantId: "cba1714d-0062-4fc6-a11d-88e062b1fe88"
}

The condition is the users in this tenant id should be able to index documents which tenant id == "cba1714d-0062-4fc6-a11d-88e062b1fe88" only.

Is there any built-in function to prevent the crossed tenant indexing document?

CodePudding user response:

Yes, you can achive using ingest pipeline in elasticsearch. Ingest pipeline have drop processor which will drop document based on specific condition.

You can create Ingest pipeline using below command or from Kibana.

PUT _ingest/pipeline/my-pipeline
{
  "description": "My optional pipeline description",
  "processors": [
    {
      "drop": {
        "if": "ctx.tenantId != 'cba1714d-0062-4fc6-a11d-88e062b1fe88'"
      }
    }
  ]
}

After creating pipeline, you can provide pipeline name in index request:

POST my-index/_doc?pipeline=my-pipeline
{
  "@timestamp": "2099-03-07T11:04:05.000Z",
  "tenantId": "cba1714d-0062-4fc6-a11d-88e062b1fe88"
}

If you want to index document based on tenantId in diffrent index then you can use Set Processor and change value of _index based on condition.

  • Related