# Multi-Tenant custom pre-defined fields and unknown filters

**URL:** <https://discuss.elastic.co/t/multi-tenant-custom-pre-defined-fields-and-unknown-filters/168761>\
**Category:** Elasticsearch\
**Created:** [February 18, 2019, 1:39am UTC](https://discuss.elastic.co/t/multi-tenant-custom-pre-defined-fields-and-unknown-filters/168761 "2019-02-18T01:39:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mehrdd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mehrdd/32/41029_2.png) [@mehrdd](https://discuss.elastic.co/u/mehrdd)\
**Post date:** [February 18, 2019, 1:39am UTC](https://discuss.elastic.co/t/multi-tenant-custom-pre-defined-fields-and-unknown-filters/168761/1 "2019-02-18T01:39:29Z")

</div>

Hi,

My index:

```auto
"mappings": {
	"properties": { 
		"namespace": { "type": "text" },
		"uid": { "type": "long" },
		"id": { "type": "long" },
		"flags": { "type": "text" },
		"s01": { "type": "text" },
		"s02": { "type": "text" },
		"s03": { "type": "text" },
		"n01": { "type": "long" },
		"n02": { "type": "long" },
		"n03": { "type": "long" }
	}
}

```

And my very simple query:

```auto
GET /d3/_doc/_search
{
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "namespace": "1_2_3"
                }
            }
        }
    }
}

```

## Questions:

- My simple query will took about 70ms to 120ms  
But when i try URI query it is much faster (about 2-3ms):  
[http://localhost:9200/d3/\_doc/\_search?q=namespace:1\_2\_3](http://localhost:9200/d3/_doc/_search?q=namespace:1_2_3)  
Is it because of caching or something?

- I'm building a SaaS service and every tenants (which is isolated) can have their modules and their custom fields. i thought the best approach is having a set of pre-defined fields like s01, s02, s(n) for strings and n01, n02, n(n) for numbers. with a middleware i can map the target field of a module to it's specific data. And `namespace` is a 3 part codename for "TenantID\_ApplicationID\_ModuleID".  
So let's say, if we want to get latest news (ApplicationID: 1 , ModuleID: 4) from a tenant (TenantID: 20) we would do a query like this:  
`SELECT * FROM d3 WHERE namespace = '20_1_4' ORDER BY id DESC LIMIT 10`  
We can also filter the module with all fields.  
Is it a good approach todo with Elasticsearch? We may have 200 Requests Per Second.

- Due to fact i told here, filters and queries will be very different and completely unknown. is there any suggestion for this model?

- Results of `namespace: 1_2_3` are the slowest. I think that's because it has millions of results, but don't know why. On the other hand `namespace:1_2_2` is super fast.

* * *

Some information:  
Records with `namespace:1_2_1` = 3 docs  
Records with `namespace:1_2_2` = 17 docs  
Records with `namespace:1_2_3` = 3000000 docs

I'm using Elasticsearch 6.6 with Docker for macOS.  
1 Node, 1 Shard, 1 Replica

Thank you so much  
Mehrdad

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 18, 2019, 1:39am UTC](https://discuss.elastic.co/t/multi-tenant-custom-pre-defined-fields-and-unknown-filters/168761/2 "2019-03-18T01:39:38Z")

</div>

Closed on March 18, 2019, 1:39am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
