Course contents
Query DSL · 11 / 28

Full-text queries

Understand analysis, word requirements, phrases and multi-field search.

8 sectionsElasticsearch 9.4.3Not read yet

What you will learn

  • match and required words
  • Order and proximity
  • Multiple fields

Understand analysis, word requirements, phrases and multi-field search.

course-products examples use the teaching mapping and product from chapter 5, “Mapping and field types”. Exercise walkthroughs below use the trainer’s separate public dataset.

Chapter map
  • match and required words
  • Order and proximity
  • Multiple fields

Understand analysis, word requirements, phrases and multi-field search.

The course targets Elasticsearch 9.4.3. Exercise examples use the public products dataset; other requests illustrate separate teaching indices and may require setup or privileges unavailable in the trainer. Responses are shortened fragments; scores may vary. Check deployment and license requirements for advanced features.

01 / 08

match and required words

match analyzes input and builds term clauses. operator=or generally permits individual words; and requires all. minimum_should_match provides intermediate strictness. Absolute AND can lose useful products for long queries, so use quality evidence. Inputs reduced to no tokens by stop filters need a deliberate zero_terms_query policy.

Check your understanding
02 / 08

Order and proximity

match_phrase uses positions. slop permits positional displacement and is not simply a count of extra words in every case: transposition has a cost too. A phrase query does not compare the entire original string because analysis has already happened. Whole-name equality needs a separate keyword representation when required.

Check your understanding
03 / 08

Multiple fields

multi_match best_fields emphasizes the best field, most_fields combines evidence from representations, and cross_fields distributes terms across compatible fields. combined_fields treats compatible text fields as a combined representation, subject to analyzer constraints. Name and description do not always belong together: their roles and lengths can require explicit boosts.

The name is boosted while description matches remain eligible.

POST /course-products/_search
{
  "query": {
    "multi_match": {
      "query": "wireless headphones",
      "fields": [
        "name^3",
        "description"
      ],
      "type": "best_fields"
    }
  }
}

How to read this result

The name is boosted while description matches remain eligible.

Check your understanding
04 / 08

Catalog reasoning example

Product A has name="wireless headphones". Product B has name="headphones" and description="wireless charging case". Multi-field search may retrieve both with different evidence. Requiring all words in one field differs from allowing them anywhere across fields. State the requirement before choosing multi_match or combined_fields. A product with words split across fields is an essential counterexample.

Check your understanding
05 / 08

Search analyzed text

match is the starting point for searching a text field. The input TRAVEL is analyzed and can match Travel in name. The result is based on analyzed terms, not a case-sensitive comparison of the original JSON string. Score reflects the match in the current corpus, not a boolean confidence percentage.

For multiple input words the default operator is or: one analyzed word may be enough. That behavior is useful for broad recall but must not be confused with matching all words or a phrase. The following sections make those requirements explicit.

Try a different scenario

POST /products/_search
{
  "query": {
    "match": {
      "name": "TRAVEL"
    }
  },
  "size": 2,
  "_source": [
    "name",
    "price"
  ]
}
Elasticsearch response
Shortened response · JSON
{
  "hits": {
    "total": {
      "value": 60,
      "relation": "eq"
    },
    "hits": [
      {
        "_id": "3",
        "_score": 1.6332386,
        "_source": {
          "name": "Sony Travel Speaker 3",
          "price": 100
        }
      },
      {
        "_id": "13",
        "_score": 1.6332386,
        "_source": {
          "name": "Apple Travel Speaker 13",
          "price": 100
        }
      }
    ]
  }
}

How to read this result

The returned names contain the analyzed travel term despite the uppercase input. Compare this with a case-sensitive term query on a keyword field.

Apply this in exercise 11
06 / 08

Match a phrase

match_phrase considers token positions as well as terms. With the default slop of zero, the analyzed words must be adjacent and in the same order. The example searches for gaming keyboard; a title with keyboard before gaming is a different phrase even if it has both words.

Phrase matching still analyzes text. It does not require the entire field to equal the phrase: a longer title may contain the phrase as part of its token sequence. Exact full-field equality belongs to an appropriate keyword field, not to match_phrase.

Words versus phrase
  • gaming + keyboard
    • Gaming Keyboard: phrase + AND
    • Keyboard for Gaming: AND only

The same pair of tokens can satisfy an AND search but fail an exact phrase.

Try a different scenario

POST /products/_search
{
  "query": {
    "match_phrase": {
      "name": "gaming keyboard"
    }
  },
  "size": 2,
  "_source": [
    "name",
    "price"
  ]
}
Elasticsearch response
Shortened response · JSON
{
  "hits": {
    "total": {
      "value": 30,
      "relation": "eq"
    },
    "hits": [
      {
        "_id": "4",
        "_score": 4.6608744,
        "_source": {
          "name": "Logitech Gaming Keyboard 4",
          "price": 199
        }
      },
      {
        "_id": "14",
        "_score": 4.6608744,
        "_source": {
          "name": "Samsung Gaming Keyboard 14",
          "price": 199
        }
      }
    ]
  }
}

How to read this result

The phrase appears in the same order in matching titles. Other words before or after it do not prevent a match.

Apply this in exercise 12
07 / 08

Search multiple fields

multi_match applies a text query to explicitly listed fields. Searching name and description can retrieve a product whose useful wording appears only in the description. The default best_fields strategy derives relevance from the best matching field rather than simply adding one point for each matching field.

Field names belong in a fields array. Values remain case-analyzed according to each field mapping. This trainer requires an explicit allowed list, so do not use a wildcard field selector. The returned source fields are independent from the fields searched.

Try a different scenario

POST /products/_search
{
  "query": {
    "multi_match": {
      "query": "gaming",
      "fields": [
        "name",
        "description"
      ]
    }
  },
  "size": 2,
  "_source": [
    "name",
    "description"
  ]
}
Elasticsearch response
Shortened response · JSON
{
  "hits": {
    "total": {
      "value": 30,
      "relation": "eq"
    },
    "hits": [
      {
        "_id": "4",
        "_score": 2.3304372,
        "_source": {
          "name": "Logitech Gaming Keyboard 4",
          "description": "Reliable everyday accessory"
        }
      },
      {
        "_id": "14",
        "_score": 2.3304372,
        "_source": {
          "name": "Samsung Gaming Keyboard 14",
          "description": "Travel companion with long battery life"
        }
      }
    ]
  }
}

How to read this result

Inspect both returned text fields to find the source of a match. A missing word in name alone does not prove that the hit is incorrect.

Apply this in exercise 13
08 / 08

Require all words

A match object can contain query and operator. Setting operator to and requires every analyzed term in that one field. In the example both travel and speaker must occur in name. The words need not be adjacent, and this is not the same as requiring the string to equal the entire product name.

Do not confuse this term-level AND with bool.must across different query clauses. Both enforce conjunction, but at different levels. When a task specifies all words in one field, a match with operator and communicates that intent directly.

Try a different scenario

POST /products/_search
{
  "query": {
    "match": {
      "name": {
        "query": "travel speaker",
        "operator": "and"
      }
    }
  },
  "size": 2,
  "_source": [
    "name",
    "price"
  ]
}
Elasticsearch response
Shortened response · JSON
{
  "hits": {
    "total": {
      "value": 30,
      "relation": "eq"
    },
    "hits": [
      {
        "_id": "3",
        "_score": 3.9636757,
        "_source": {
          "name": "Sony Travel Speaker 3",
          "price": 100
        }
      },
      {
        "_id": "13",
        "_score": 3.9636757,
        "_source": {
          "name": "Apple Travel Speaker 13",
          "price": 100
        }
      }
    ]
  }
}

How to read this result

A name containing only travel or only speaker is excluded. Compare the total with the broader default OR version to understand the effect.

Apply this in exercise 14

Check your understanding

Answer before expanding each explanation.

1. What does match analyze?

Input text with the field or specified search analyzer.

2. Is AND always better?

No, it can sharply reduce recall on long queries.

3. Does a phrase equal the whole string?

No, it matches a sequence of analyzed tokens.

4. When is most_fields useful?

When multiple representations should contribute evidence.

5. Can arbitrary fields be combined?

No, consider types, analyzers and each field’s role.

Key takeaways

  • Input text with the field or specified search analyzer.
  • No, it can sharply reduce recall on long queries.
  • No, it matches a sequence of analyzed tokens.
  • When multiple representations should contribute evidence.
  • No, consider types, analyzers and each field’s role.

Official reference

Ready to put this into practice?

Confirm reading to save your place. Reading and exercise progress are tracked separately in this browser using your guest cookie.