Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Elasticsearch 8.x Cookbook

You're reading from   Elasticsearch 8.x Cookbook Over 180 recipes to perform fast, scalable, and reliable searches for your enterprise

Arrow left icon
Product type Paperback
Published in May 2022
Publisher Packt
ISBN-13 9781801079815
Length 750 pages
Edition 5th Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Alberto Paro Alberto Paro
Author Profile Icon Alberto Paro
Alberto Paro
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Chapter 1: Getting Started 2. Chapter 2: Managing Mappings FREE CHAPTER 3. Chapter 3: Basic Operations 4. Chapter 4: Exploring Search Capabilities 5. Chapter 5: Text and Numeric Queries 6. Chapter 6: Relationships and Geo Queries 7. Chapter 7: Aggregations 8. Chapter 8: Scripting in Elasticsearch 9. Chapter 9: Managing Clusters 10. Chapter 10: Backups and Restoring Data 11. Chapter 11: User Interfaces 12. Chapter 12: Using the Ingest Module 13. Chapter 13: Java Integration 14. Chapter 14: Scala Integration 15. Chapter 15: Python Integration 16. Chapter 16: Plugin Development 17. Chapter 17: Big Data Integration 18. Chapter 18: X-Pack 19. Other Books You May Enjoy

Adding a field with multiple mappings

Often, a field must be processed with several core types or in different ways. For example, a string field must be processed as tokenized for search and not-tokenized for sorting. To do this, we need to define a fields multifield special property.

The fields property is a very powerful feature of mappings because it allows you to use the same field in different ways.

Getting ready

You will need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe of Chapter 1Getting Started.

To execute the commands in this recipe, you can use any HTTP client, such as curl (https://curl.haxx.se/), Postman (https://www.getpostman.com/), or similar. I suggest using the Kibana console, which provides code completion and better character escaping for Elasticsearch.

How to do it…

To define a multifield property, we need to define a dictionary containing the fields subfield. The subfield with the same name as a parent field is the default one.

If we consider the item from our order example, we can index the name like so:

{ "name": {
    "type": "keyword",
    "fields": {
      "name": {"type": "keyword"},
      "tk": {"type": "text"},
      "code": {"type": "text","analyzer": "code_analyzer"}
} },

If we already have a mapping stored in Elasticsearch and we want to migrate the fields in a multi-field property, it's enough to save a new mapping with a different type, and Elasticsearch provides the merge automatically. New subfields in the fields property can be added without problems at any moment, but the new subfields will only be available while you're searching/aggregating newly indexed documents.

When you add a new subfield to already indexed data, you need to reindex your record to ensure you have it correctly indexed for all your records.

How it works…

During indexing, when Elasticsearch processes a fields property of the multifield type, it reprocesses the same field for every subfield defined in the mapping.

To access the subfields of a multifield, we must build a new path on the base field, plus use the subfield's name. In the preceding example, we have the following:

  • name: This points to the default multifield subfield-field (the keyword one).
  • name.tk: This points to the standard analyzed (tokenized) text field.
  • name.code: This points to a field that was analyzed with a code extractor analyzer.

As you may have noticed in the preceding example, we changed the analyzer to introduce a code extractor analyzer that allows you to extract the item code from a string.

By using the multifield, if we index a string such as Good Item to buy - ABC1234, we'll have the following:

  • name = Good Item to buy - ABC1234 (useful for sorting)
  • name.tk= ["good", "item", "to", "buy", "abc1234"] (useful for searching)
  • name.code = ["ABC1234"] (useful for searching and aggregations)

In the case of the code analyzer, if the code is not found in the string, no tokens are generated. This makes it possible to develop solutions that carry out information retrieval tasks at index time and uses these at search time.

There's more...

The fields property is very useful in data processing because it allows you to define several ways to process field data.

For example, if we are working on documental content (such as articles, word documents, and so on), we can define fields as subfield analyzers to extract names, places, date/time, geolocation, and so on.

The subfields of a multifield are standard core type fields – we can perform every process we want on them, such as search, filter, aggregation, and scripting.

See also

To find out more about what Elasticsearch analyzers you can use, please refer to the Specifying different analyzers recipe.

You have been reading a chapter from
Elasticsearch 8.x Cookbook - Fifth Edition
Published in: May 2022
Publisher: Packt
ISBN-13: 9781801079815
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime