Denormalization and Concurrency
Of course, data denormalization has downsides too. The first disadvantage is that the index will be bigger because the _source
document for every blog post is bigger, and there are more indexed fields. This usually isn’t a huge problem. The data written to disk is highly compressed, and disk space is cheap. Elasticsearch can happily cope with the extra data.
The more important issue is that, if the user were to change his name, all of his blog posts would need to be updated too. Fortunately, users don’t often change names. Even if they did, it is unlikely that a user would have written more than a few thousand blog posts, so updating blog posts with the scroll
and bulk
APIs would take less than a second.
However, let’s consider a more complex scenario in which changes are common, far reaching, and, most important, concurrent.
In this example, we are going to emulate a filesystem with directory trees in Elasticsearch, much like a filesystem on Linux: the root of the directory is /
, and each directory can contain files and subdirectories.
We want to be able to search for files that live in a particular directory, the equivalent of this:
grep "some text" /clinton/projects/elasticsearch/*
This requires us to index the path of the directory where the file lives:
PUT
/fs/file/
1
{
"name"
:
"README.txt"
,
"path"
:
"/clinton/projects/elasticsearch"
,
"contents"
:
"Starting a new Elasticsearch project is easy..."
}
- The filename
- The full path to the directory holding the file
Note
Really, we should also index directory
documents so we can list all files and subdirectories within a directory, but for brevity’s sake, we will ignore that requirement.
We also want to be able to search for files that live anywhere in the directory tree below a particular directory, the equivalent of this:
grep -r "some text" /clinton
To support this, we need to index the path hierarchy:
/clinton
/clinton/projects
/clinton/projects/elasticsearch
This hierarchy can be generated automatically from the path
field using the path_hierarchy
tokenizer:
PUT
/fs
{
"settings"
:
{
"analysis"
:
{
"analyzer"
:
{
"paths"
:
{
"tokenizer"
:
"path_hierarchy"
}
}
}
}
}
- The custom
paths
analyzer uses thepath_hierarchy
tokenizer with its default settings. Seepath_hierarchy
tokenizer.
The mapping for the file
type would look like this:
PUT
/fs/_mapping/file
{
"properties"
:
{
"name"
:
{
"type"
:
"string"
,
"index"
:
"not_analyzed"
},
"path"
:
{
"type"
:
"string"
,
"index"
:
"not_analyzed"
,
"fields"
:
{
"tree"
:
{
"type"
:
"string"
,
"analyzer"
:
"paths"
}
}
}
}
}
- The
name
field will contain the exact name. - The
path
field will contain the exact directory name, while thepath.tree
field will contain the path hierarchy.
Once the index is set up and the files have been indexed, we can perform a search for files containing elasticsearch
in just the /clinton/projects/elasticsearch
directory like this:
GET
/fs/file/_search
{
"query"
:
{
"filtered"
:
{
"query"
:
{
"match"
:
{
"contents"
:
"elasticsearch"
}
},
"filter"
:
{
"term"
:
{
"path"
:
"/clinton/projects/elasticsearch"
}
}
}
}
}
- Find files in this directory only.
Every file that lives in any subdirectory under /clinton
will include the term /clinton
in the path.tree
field. So we can search for all files in any subdirectory of /clinton
as follows:
GET
/fs/file/_search
{
"query"
:
{
"filtered"
:
{
"query"
:
{
"match"
:
{
"contents"
:
"elasticsearch"
}
},
"filter"
:
{
"term"
:
{
"path.tree"
:
"/clinton"
}
}
}
}
}
- Find files in this directory or in any of its subdirectories
Comments