短语与邻近搜索
了解如何使用 `match_phrase` 和 `slop` 参数搜索精确短语以及一定邻近范围内的词语。
短语与邻近搜索 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Beyond Single Words
Welcome! So far, you've learned to search for individual words. But what if you need to find exact phrases like "quick brown fox" or words that appear close together?
This lesson introduces phrase and proximity searches, powerful techniques to make your searches more precise and relevant.
Finding Exact Phrases
Sometimes, the order of words matters. For example, "New York" is different from "York New". To find an exact sequence of words, we use a phrase query.
In Elasticsearch, the match_phrase query is perfect for this. It looks for all terms in your query string, in the exact order, and next to each other.
match_phrase Example
Let's see match_phrase in action. We'll simulate indexing a few documents and then search for the exact phrase "quick brown fox".
public class Main {
public static void main(String[] args) {
System.out.println("Simulating search for exact phrase 'quick brown fox'");
System.out.println("on documents:");
System.out.println("- 'The quick brown fox jumps.'");
System.out.println("- 'A fox brown quick jumps.'");
System.out.println("- 'Quick brown fox is fast.'");
System.out.println("
--- Elasticsearch Query (simulated) ---");
System.out.println("{");
System.out.println(" \"query\": {");
System.out.println(" \"match_phrase\": {");
System.out.println(" \"text_field\": \"quick brown fox\"");
System.out.println(" }");
System.out.println(" }");
System.out.println("}");
System.out.println("
--- Search Results (simulated) ---");
System.out.println("Document: 'The quick brown fox jumps.' (MATCH!)");
System.out.println("Document: 'Quick brown fox is fast.' (MATCH!)");
System.out.println("No match for 'A fox brown quick jumps.'");
}
}Phrase Matching Logic
The match_phrase query requires two conditions:
- All terms present: Every word in your phrase ("quick", "brown", "fox") must exist in the document.
- Exact order: The words must appear in the same sequence.
- Contiguous: By default, the words must be right next to each other.
If any of these conditions aren't met, the document won't be considered a match.
Proximity Search with slop
What if you want to find words that are *close* to each other, but not necessarily an exact, contiguous phrase? This is where proximity search comes in.
Elasticsearch introduces the slop parameter for match_phrase queries. It allows for a certain number of "slops" or "gaps" between the words in your phrase.
Understanding slop
The slop parameter defines the maximum number of positions tokens can be "moved" to match the phrase.
slop: 0(default) means words must be contiguous.slop: 1allows one word to be skipped or reordered slightly.- Higher
slopvalues allow more flexibility in word order and distance.
Think of it as how many "moves" it takes to transform the document's words into your target phrase.
slop=1 Demonstration
Let's modify our previous search. We'll look for "quick fox" but allow for one word in between using "slop": 1.
public class Main {
public static void main(String[] args) {
System.out.println("Simulating search for 'quick fox' with slop: 1");
System.out.println("on documents:");
System.out.println("- 'The quick brown fox jumps.'");
System.out.println("- 'A quick sly fox is here.'");
System.out.println("- 'The quick fox is fast.'");
System.out.println("
--- Elasticsearch Query (simulated) ---");
System.out.println("{");
System.out.println(" \"query\": {");
System.out.println(" \"match_phrase\": {");
System.out.println(" \"text_field\": {");
System.out.println(" \"query\": \"quick fox\",");
System.out.println(" \"slop\": 1");
System.out.println(" }");
System.out.println(" }");
System.out.println(" }");
System.out.println("}");
System.out.println("
--- Search Results (simulated) ---");
System.out.println("Document: 'The quick brown fox jumps.' (MATCH!)");
System.out.println("Document: 'A quick sly fox is here.' (MATCH!)");
System.out.println("Document: 'The quick fox is fast.' (MATCH!)");
}
}slop and Word Order
slop can also help match phrases where words are slightly reordered. For example, to match "brown quick" when searching for "quick brown" with slop: 1.
It measures the minimum number of moves to get the document's tokens into the query's token order. Each move counts as 1 slop unit.
public class Main {
public static void main(String[] args) {
System.out.println("Simulating search for 'quick brown' with slop: 1");
System.out.println("on document:");
System.out.println("- 'The brown quick fox.'"); // 'brown' and 'quick' are swapped
System.out.println("
--- Elasticsearch Query (simulated) ---");
System.out.println("{");
System.out.println(" \"query\": {");
System.out.println(" \"match_phrase\": {");
System.out.println(" \"text_field\": {");
System.out.println(" \"query\": \"quick brown\",");
System.out.println(" \"slop\": 1");
System.out.println(" }");
System.out.println(" }");
System.out.println(" }");
System.out.println("}");
System.out.println("
--- Search Results (simulated) ---");
System.out.println("Document: 'The brown quick fox.' (MATCH!)");
System.out.println("Explanation: To change 'brown quick' to 'quick brown', one move is needed (slop 1).");
}
}match_phrase vs. match
It's important to differentiate match_phrase from the basic match query you've seen before.
matchquery: Finds documents containing *any* of the words, regardless of order or proximity. It's good for general relevancy.match_phrasequery: Requires *all* words in the exact order and, by default, contiguous. Withslop, it allows for controlled proximity. It's for high precision.
Choose match_phrase when the order and closeness of words are critical to your search.
Proximity Check
You want to find documents where "apple" and "pie" appear, with at most one word in between them, in that specific order.
Recap: Precise Searches
Great job! You've learned how to enhance your search precision:
match_phrase: For finding exact sequences of words.slopparameter: To control the allowed distance and reordering between words in a phrase.
These techniques are crucial for building highly accurate and user-friendly search experiences. Keep practicing to master them!
常见问题解答
「短语与邻近搜索」课时是免费的吗?
是的 — 「短语与邻近搜索」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
「短语与邻近搜索」这节课中我会学到什么?
了解如何使用 `match_phrase` 和 `slop` 参数搜索精确短语以及一定邻近范围内的词语。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elasticsearch & Full Text Search Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「短语与邻近搜索」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?
能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。