Configuring Encoding Detectors
Tika determines the character encoding of plain text and HTML with a chain of encoding
detectors. DefaultEncodingDetector discovers them through the Java service-provider interface
(META-INF/services).
The chain runs in one of two modes, and which one you get depends on whether a
MetaEncodingDetector is in it:
-
collect-all — with a
MetaEncodingDetectorpresent (the default chain has one), every base detector runs and contributes candidate encodings, and the meta detector picks the best by decode quality. Registration order does not matter. -
first-match-wins — otherwise, detectors run in registration order and the first non-null result wins.
Because the default chain’s junk-filter-encoding-detector is a MetaEncodingDetector, the stock
distribution runs collect-all: order is irrelevant, and a declaration — a BOM, a <meta charset>
tag — does not automatically win. The junk filter overrides either when the byte evidence
strongly contradicts it.
Available Detectors
All implement org.apache.tika.detect.EncodingDetector and are referenced in JSON config by the
name below.
| Name | Module | Default chain | Role |
|---|---|---|---|
|
|
Yes |
Emits a candidate from a leading byte-order mark. |
|
|
Yes |
Emits a candidate from declarative hints ( |
|
|
No |
Always returns the configured charset, ignoring the document. Takes |
|
|
Yes |
Emits a candidate from an HTML |
|
|
Yes |
Byte-bigram Naive Bayes classifier, plus structural detectors for UTF-32 and UTF-16 and a UTF-8 grammar gate. |
|
|
Yes, last |
|
|
|
No |
Spec-strict WHATWG prescan. Opt in when you need strict WHATWG tokenisation — for example, ignoring charset declarations inside HTML comments. |
|
|
No |
State-machine structural prober (juniversalchardet fork). Not bundled and not auto-discovered: add the jar and configure it explicitly. |
|
|
No |
Wraps ICU4J’s |
Configuration Examples
Exclude a detector from the default chain
default-encoding-detector takes an exclude list of auto-registered detectors to drop:
{
"encoding-detectors": [
{
"default-encoding-detector": {
"exclude": ["html-encoding-detector"]
}
}
]
}
Do not combine default-encoding-detector with explicit detector entries in the same
list. The loader then wraps everything in an outer composite with no MetaEncodingDetector at its
top level, so collect-all arbitration is silently lost and the explicit detectors are never
reached. To configure individual detectors, specify the whole chain explicitly instead.
|
Specify the chain explicitly
An explicit ordered list replaces the SPI-discovered chain. End it with
junk-filter-encoding-detector to keep collect-all arbitration; omit it for first-match-wins.
{
"encoding-detectors": [
{"html-encoding-detector": {}},
{"mojibuster-encoding-detector": {}},
{"junk-filter-encoding-detector": {}}
]
}
Configure the HTML detector’s read limit
html-encoding-detector scans the first markLimit bytes for the <meta charset> tag, 8192 by
default. Raise it if your documents put large <script> blocks before the meta tag (TIKA-2485).
mojibuster-encoding-detector reads a larger content probe, so in the default chain this limit
matters mainly for very large preambles.
Configuring it means writing the chain out explicitly; the configured detector then participates as a base detector alongside Mojibuster, and the junk filter arbitrates as usual:
{
"encoding-detectors": [
{"html-encoding-detector": {"markLimit": 131072}},
{"mojibuster-encoding-detector": {}},
{"junk-filter-encoding-detector": {}}
]
}
Use the spec-strict WHATWG HTML detector
Swap in standard-html-encoding-detector when the lenient regex would false-match — charset
declarations inside comments, for instance:
{
"encoding-detectors": [
{"standard-html-encoding-detector": {}},
{"mojibuster-encoding-detector": {}},
{"junk-filter-encoding-detector": {}}
]
}
Restore the 3.x detection chain (universal + icu4j)
universal-encoding-detector and icu4j-encoding-detector are neither bundled nor
auto-registered. To get the 3.x chain (html / universal / icu4j, first-match-wins) you must
do both:
-
Add the jars to the classpath. They are not in the
tika-apportika-server-standardpackages, so supplytika-encoding-detector-universalandtika-encoding-detector-icu4jyourself — for example via-Dtika.extras.dir, see the configuration overview. -
Configure the chain explicitly. Dropping the jars on the classpath is not enough: unlike the other detectors, these two are config-only. An explicit chain with no
MetaEncodingDetectorruns first-match-wins.
{
"encoding-detectors": [
{"html-encoding-detector": {}},
{"universal-encoding-detector": {}},
{"icu4j-encoding-detector": {}}
]
}