capabilities.work

Search and Fetch the Top Hit

A 3-step recipe with Elasticsearch. We can help you get access.

Recipe · 3 steps

Search and Fetch the Top Hit

Probe a query with a URI search, page it with a DSL search, then fetch the top hit in full.

  1. Run a lightweight URI search to confirm the query matches something before spending a structured query on it.
  2. Re-run the query through the query DSL with explicit size and from, giving the caller ranked, paged control the URI search cannot express.
  3. Fetch the complete document behind the top ranked hit, which returns the full source rather than the ranking-shaped search projection.
What you'll need to have ready
  • index: The index to search.
  • queryString: The user-supplied search terms.
  • searchField: The field the DSL match query targets (e.g. "title").
  • size: The number of hits to return per page.
  • from: The offset into the result set, for paging.
For your developer

Arazzo workflow elasticsearch:search-and-fetch, 3 operations: searchUri → searchDsl → getDocument. Specs on APIs.io.