0Pricing
Elasticsearch & Full Text Search Systems · Leçon

Recherches par expression et proximité

Découvrez comment rechercher des expressions exactes et des mots situés à une certaine proximité à l’aide des paramètres `match_phrase` et `slop`.

Recherches par expression et proximité est une leçon Elasticsearch & Full Text Search Systems gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Elasticsearch & Full Text Search Systems, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Elasticsearch & Full Text Search Systems comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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: 1 allows one word to be skipped or reordered slightly.
  • Higher slop values 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.

  • match query: Finds documents containing *any* of the words, regardless of order or proximity. It's good for general relevancy.
  • match_phrase query: Requires *all* words in the exact order and, by default, contiguous. With slop, 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.
  • slop parameter: 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!

Questions Fréquemment Posées

La leçon « Recherches par expression et proximité » est-elle gratuite ?

Oui — le texte complet de « Recherches par expression et proximité » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Elasticsearch & Full Text Search Systems, passe à CoddyKit PRO. Le cours Elasticsearch & Full Text Search Systems comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Recherches par expression et proximité » ?

Découvrez comment rechercher des expressions exactes et des mots situés à une certaine proximité à l’aide des paramètres `match_phrase` et `slop`. Tu pratiques Elasticsearch & Full Text Search Systems avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Elasticsearch & Full Text Search Systems ?

Aucune expérience préalable n'est requise. Elasticsearch & Full Text Search Systems sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.

Combien de temps prend la leçon « Recherches par expression et proximité » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Elasticsearch & Full Text Search Systems ?

Oui. Chaque leçon Elasticsearch & Full Text Search Systems inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Recherches par expression et proximité
  2. Requêtes approximatives et avec caractères génériques
  3. Mise en évidence des résultats de recherche
  4. Agrégations pour la recherche à facettes
← Retour à Elasticsearch & Full Text Search Systems