We are working on updating this book for the latest version. Some content might be out of date.
About 5% of all queries are phrase queries (see Phrase Matching), but they often account for the majority of slow queries. Phrase queries can perform poorly, especially if the phrase includes very common words; a phrase like “To be, or not to be” could be considered pathological. The reason for this has to do with the amount of data that is necessary to support proximity matching.
In Pros and Cons of Stopwords, we said that removing stopwords saves only a small amount of space in the inverted index. That was only partially true. A typical index may contain, among other data, some or all of the following:
- Terms dictionary
- A sorted list of all terms that appear in the documents in the index, and a count of the number of documents that contain each term.
- Postings list
- A list of which documents contain each term.
- Term frequency
- How often each term appears in each document.
- The position of each term within each document, for phrase and proximity queries.
- The start and end character offsets of each term in each document, for snippet highlighting. Disabled by default.
- A factor used to normalize fields of different lengths, to give shorter fields more weight.
Removing stopwords from the index may save a small amount of space in the terms dictionary and the postings list, but positions and offsets are another matter. Positions and offsets data can easily double, triple, or quadruple index size.
Positions are enabled on
analyzed string fields by default,
so that phrase
queries will work out of the box. The more often that a term appears, the more
space is needed to store its position data. Very common words, by
definition, appear very commonly, and their positions data can run to megabytes
or gigabytes on large collections.
Running a phrase query on a high-frequency word like
the might result in
gigabytes of data being read from disk. That data will be stored in the kernel
filesystem cache to speed up later access, which seems like a good thing, but
it might cause other data to be evicted from the cache, which will slow
This is clearly a problem that needs solving.
Often, the answer is no. For many use cases, such as logging, you need to
know whether a term appears in a document — information that is provided
by the postings list—but not where it appears. Or perhaps you need to use
phrase queries on one or two fields, but you can disable positions data on all
of the other analyzed
Only store which documents contain which terms. This is the default for
docsinformation, plus how often each term appears in each document. Term frequencies are needed for complete TF/IDF relevance calculations, but they are not required if you just need to know whether a document contains a particular term.
freqs, plus the position of each term in each document. This is the default for
analyzedstring fields, but can be disabled if phrase/proximity matching is not needed.
positions, and the start and end character offsets of each term in the original string. This information is used by the
postingshighlighter but is disabled by default.
Removing stopwords is one way of reducing the size of the positions data quite dramatically. An index with stopwords removed can still be used for phrase queries because the original positions of the remaining terms are maintained, as we saw in Maintaining Positions. But of course, excluding terms from the index reduces searchability. We wouldn’t be able to differentiate between the two phrases Man in the moon and Man on the moon.
Fortunately, there is a way to have our cake and eat it: the
common_grams token filter.