DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Building a Cost-Effective ELK Stack for Centralized Logging
  • Doris vs Elasticsearch: A Comparison and Practical Cost Case Study
  • How to Scale Elasticsearch to Solve Your Scalability Issues

Trending

  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  • A Guide to Auto-Tagging and Lineage Tracking With OpenMetadata
  • Traditional Testing and RAGAS: A Hybrid Strategy for Evaluating AI Chatbots
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Custom Field Handling in Elasticsearch: Managing Total Number of Fields Limit

Custom Field Handling in Elasticsearch: Managing Total Number of Fields Limit

By 
Dinesh Agrawal user avatar
Dinesh Agrawal
·
Dec. 26, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
22.5K Views

Join the DZone community and get the full member experience.

Join For Free

While using Elasticsearch to handle custom fields in your product, you soon hit the limit of the total number of fields in an index. Now, there are two ways to go.

1. Increase this limit just by updating index.mapping.total_fields.limit with a higher number. This will require additional memory for your Elasticsearch cluster.

Java
 




x


 
1
PUT my_index_name/_settings
2
{
3
  "index.mapping.total_fields.limit": 5000
4
}



2. Better manage your custom fields so that you use this limit in an optimum way and your Elasticsearch cluster requires no additional memory.

Let's assume that you have 10 clients for which you're using the same Elasticsearch index, and they have their own custom fields. So, client 1 has "model name," "Driver name," "vehicle brand," "vehicle number," "date of joining," etc. custom fields, while client two has "shade," "light," "texture," "brightness," etc. custom fields in their account.

If we count, each one of them has 200 of these unique custom fields. To support 10 clients, we need 10 * 200 = 2000 fields in Elasticsearch. Now, to handle a cluster with 2,000 fields of support in an index is costly. How can we manage it better? Let's see.

Let's assume we secured some custom fields of each type. For example:

  • 50 custom fields of type date

    • cf_1_date => custom field 1 of type date.

    • cf_2_date.

    • cf_3_date.

    • cf_4_date up to cf_50_date.

  • Similarly, we secured other types like text, boolean, long, etc. (and any data type that you support).

    • cf_1_text up to cf_50_text.

    • cf_1_boolean up to cf_50_boolean.

    • cf_1_long up to cf_50_long.

Now, we have a list of secured fields with us, so let's map client 1's custom fields with it.

  • "model name" is of type text, so assign cf_1_text to it.
  • "Driver name"is of type text. So let's pick the next field for text from the secured list. cf_1_text  is taken, so assign cf_2_text to it
  • "vehicle brand" is of type text, so assign cf_3_text to it.
  • "vehicle number" is of type long, so assign cf_1_long to it.
  • "date of joining" is of type date, so assign cf_1_date to it.

And, we keep the mapping like:

JSON
 




xxxxxxxxxx
1


 
1
{
2
    "cf_1_text": "model name",
3
    "cf_2_text": "Driver name",
4
    "cf_3_text": "vehicle brand",
5
    "cf_1_long": "vehicle number",
6
    "cf_1_date": "date of joining"
7
}



Let's map client 2's custom fields.

  • "shade" is of type text, so assign cf_1_text to it.

  • "light" is of type text, so assign cf_2_text to it.

  • "texture" is of type text, so assign cf_3_text to it.

  • "brightness" is of type text so assign cf_4_text to it.

So our final mapping looks like this...

JSON
 




xxxxxxxxxx
1
17


 
1
{
2
    "client_1":
3
    {
4
        "cf_1_text": "model name",
5
        "cf_2_text": "Driver name",
6
        "cf_3_text": "vehicle brand",
7
        "cf_1_long": "vehicle number",
8
        "cf_1_date": "date of joining"
9
    },
10
    "client_2":
11
    {
12
        "cf_1_text": "shade",
13
        "cf_2_text": "light",
14
        "cf_3_text": "texture",
15
        "cf_4_text": "brightness"
16
    }
17
}



Now, let's talk about how to index the data:

As soon as you're indexing the data for client 1, replace the client's field with secured fields using the above mapping. Ex:

JSON
 




xxxxxxxxxx
1


 
1
{
2
    "model name": "BT 383",
3
    "Driver name": "Arik Ykam",
4
    "vehicle brand": "Bitol",
5
    "vehicle number": "IUA 74 29744",
6
    "date of joining": "2017-02-10"
7
}



  • Replace it with:
JSON
 




xxxxxxxxxx
1


 
1
{
2
    "cf_1_text": "BT 383",
3
    "cf_2_text": "Arik Ykam",
4
    "cf_3_text": "Bitol",
5
    "cf_1_long": "IUA 74 29744",
6
    "cf_1_date": "2017-02-10"
7
}



This is about indexing it. To ensure its data types, dynamic templates can be used with Elaticsearch. This is an example mapping:

JSON
 




x
48


 
1
{
2
    "your_type_name":
3
    {
4
        "dynamic_templates": [
5
        {
6
            "dt_text":
7
            {
8
                "match": "*_text",
9
                "mapping":
10
                {
11
                    "type": "text",
12
                }
13
            }
14
        },
15
        {
16
            "dt_long":
17
            {
18
                "match": "*_long",
19
                "mapping":
20
                {
21
                    "type": "double"
22
                }
23
            }
24
        },
25
        {
26
            "dt_date":
27
            {
28
                "match": "*_date",
29
                "mapping":
30
                {
31
                    "type": "date"
32
                }
33
            }
34
        },
35
        {
36
            "dt_boolean":
37
            {
38
                "match": "*_boolean",
39
                "mapping":
40
                {
41
                    "type": "boolean"
42
                }
43
            }
44
        }],
45
        "properties":
46
        {}
47
    }
48
}



Using a dynamic template and match like *_text, you can ensure that all field names matching this pattern will be of type text. (You can customize this.)

  • As soon as you index the data, any field name matching to *_text will be indexed as text.

  • Field name matching to *_long will be indexed as double.

  • Field name matching to *_date will be indexed as date data type.

  • Field name matching to *_boolean will be indexed as boolean type.

Now, let's come to the presentation part where you fetch the data from index and show the actual fields to the client

  • You can simply add a wrapper to the data fetch class, which does this conversion:

JSON
 




xxxxxxxxxx
1


 
1
{
2
    "cf_1_text": "BT 383",
3
    "cf_2_text": "Arik Ykam",
4
    "cf_3_text": "Bitol",
5
    "cf_1_long": "IUA 74 29744",
6
    "cf_1_date": "2017-02-10"
7
}



This is what Elasticseach will return. You'll check the mapping available for this client (client 1 in this case) which is:

JSON
 




xxxxxxxxxx
1


 
1
{
2
    "cf_1_text": "model name",
3
    "cf_2_text": "Driver name",
4
    "cf_3_text": "vehicle brand",
5
    "cf_1_long": "vehicle number",
6
    "cf_1_date": "date of joining"
7
}



And then, simply replace the cf_1_text with "model name" and  cf_2_text with "Driver name."

So, the final data will look like: 

JSON
 




xxxxxxxxxx
1


 
1
{
2
    "model name": "BT 383",
3
    "Driver name": "Arik Ykam",
4
    "vehicle brand": "Bitol",
5
    "vehicle number": "IUA 74 29744",
6
    "date of joining": "2017-02-10"
7
}



And that's it. Now, you can onboard any number of clients without having a need to update the field limit on the Elastiseach side.

JSON
 




xxxxxxxxxx
1
48


 
1
{
2
    "your_type_name":
3
    {
4
        "dynamic_templates": [
5
        {
6
            "dt_text":
7
            {
8
                "match": "*_text",
9
                "mapping":
10
                {
11
                    "type": "text",
12
                }
13
            }
14
        },
15
        {
16
            "dt_long":
17
            {
18
                "match": "*_long",
19
                "mapping":
20
                {
21
                    "type": "double"
22
                }
23
            }
24
        },
25
        {
26
            "dt_date":
27
            {
28
                "match": "*_date",
29
                "mapping":
30
                {
31
                    "type": "date"
32
                }
33
            }
34
        },
35
        {
36
            "dt_boolean":
37
            {
38
                "match": "*_boolean",
39
                "mapping":
40
                {
41
                    "type": "boolean"
42
                }
43
            }
44
        }],
45
        "properties":
46
        {}
47
    }
48
}


JSON
 




xxxxxxxxxx
1
48


 
1
{
2
    "your_type_name":
3
    {
4
        "dynamic_templates": [
5
        {
6
            "dt_text":
7
            {
8
                "match": "*_text",
9
                "mapping":
10
                {
11
                    "type": "text",
12
                }
13
            }
14
        },
15
        {
16
            "dt_long":
17
            {
18
                "match": "*_long",
19
                "mapping":
20
                {
21
                    "type": "double"
22
                }
23
            }
24
        },
25
        {
26
            "dt_date":
27
            {
28
                "match": "*_date",
29
                "mapping":
30
                {
31
                    "type": "date"
32
                }
33
            }
34
        },
35
        {
36
            "dt_boolean":
37
            {
38
                "match": "*_boolean",
39
                "mapping":
40
                {
41
                    "type": "boolean"
42
                }
43
            }
44
        }],
45
        "properties":
46
        {}
47
    }
48
}


JSON
 




xxxxxxxxxx
1
48


 
1
{
2
    "your_type_name":
3
    {
4
        "dynamic_templates": [
5
        {
6
            "dt_text":
7
            {
8
                "match": "*_text",
9
                "mapping":
10
                {
11
                    "type": "text",
12
                }
13
            }
14
        },
15
        {
16
            "dt_long":
17
            {
18
                "match": "*_long",
19
                "mapping":
20
                {
21
                    "type": "double"
22
                }
23
            }
24
        },
25
        {
26
            "dt_date":
27
            {
28
                "match": "*_date",
29
                "mapping":
30
                {
31
                    "type": "date"
32
                }
33
            }
34
        },
35
        {
36
            "dt_boolean":
37
            {
38
                "match": "*_boolean",
39
                "mapping":
40
                {
41
                    "type": "boolean"
42
                }
43
            }
44
        }],
45
        "properties":
46
        {}
47
    }
48
}



Further Reading

  • 23 Useful Elasticsearch Example Queries.
  • Introduction to Elasticsearch and the ELK Stack, Part 1.
  • Reporting and Analysis With Elasticsearch.
Elasticsearch

Opinions expressed by DZone contributors are their own.

Related

  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Building a Cost-Effective ELK Stack for Centralized Logging
  • Doris vs Elasticsearch: A Comparison and Practical Cost Case Study
  • How to Scale Elasticsearch to Solve Your Scalability Issues

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!