Elasticsearch 5+模糊查询max_expansions参数解析及与fuzziness配合示例
Hey there! Since you already have a solid grasp of fuzziness and prefix_length in Elasticsearch 5+ fuzzy queries, let's break down max_expansions using your exact house example to make it crystal clear.
max_expansions Actually Does At its core, max_expansions limits the number of candidate terms Elasticsearch generates and checks when running a fuzzy query. When you set a fuzziness value, ES calculates all possible terms that are within the allowed edit distance from your input value. But if that list gets too long, it can slow down your query or return low-relevance results. max_expansions caps that list to a manageable number—only the top N most relevant candidates (ranked by how often they appear in your index) get used for the actual search.
house Query Example Let’s fill out your partial query to make it concrete:
GET my-index/my-type/_search { "query": { "fuzzy": { "my-field": { "value": "house", "fuzziness": 1, "prefix_length": 0, "max_expansions": 5 } } } }
Here’s what happens step-by-step:
- With
fuzziness=1, ES generates all terms that are 1 edit away fromhouse—think:- Replace one character:
mouse,horse,hause,bouse,housk - Delete one character:
ouse - Add one character:
ahouse,hhouse
That’s already 8+ possible candidates.
- Replace one character:
- But since
max_expansions=5, ES doesn’t check all of them. It picks the top 5 candidates that are most common in your index (e.g., ifhorseandmouseappear way more often thanhousk, those will make the cut) and only runs the query against those terms.
fuzziness These two parameters work hand-in-hand to balance relevance and performance:
fuzzinesssets the quality bar: it defines how "far" a term can be from your input (edit distance) to be considered a candidate.max_expansionssets the quantity limit: it prevents ES from wasting resources on dozens (or hundreds) of low-relevance candidates that meet thefuzzinesscriteria but aren’t useful for your search.
For example, if you set fuzziness=2 for house, ES could generate hundreds of candidates. But with max_expansions=10, you’re telling ES: "Only check the 10 most relevant terms that are 2 edits away—don’t bother with the rest."
- If you want fast, precise results: Use a small
max_expansions(5-10) to focus on the most likely matches. - If you need to cast a wider net (and don’t mind slower queries): Use a larger value (like 50, which is the default in ES5+) or omit it entirely to let ES use all eligible candidates.
- Pair with
prefix_length: If you setprefix_length=2forhouse, ES will only generate candidates starting withho(the first 2 characters stay fixed) and then applymax_expansionsto that smaller pool—this makes your query even faster and more targeted.
内容的提问来源于stack exchange,提问作者javaTry

