Elasticsearch full-text queries explained, for humans 🤓
Tags: todayilearned, elasticsearch, notes, workjournal
Published: 2019-01-26 01:24:21

---
title: Elasticsearch full-text queries explained, for humans 🤓
published: true
description: Notes on getting re-acquianted with elasticsearch
cover_image: https://media.giphy.com/media/7srpeY4TZMrO8/giphy.gif
tags: #todayilearned, #elasticsearch, #notes, #workjournal
canonical_url: https://ajsharp.com/elasticsearch-full-text-queries-explained-for-humans
---

* [ElasticSearch Full-text query Docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html)

ES has different query types. A quick summary:
* `match` - standard full text query
* `match_phrase` - phrase matching. like when you put a term in quotes on google.
* `match_phrase_prefix` - poor man’s autocomplete.
* `multi_match` - Multi-field `match`.
* `common` (not covered here) - Takes stopwords (e.g. “the”) into account.
* `query_string` (not covered here) - Expert mode. Can use AND|OR|NOT and multi-field search in a query string.
* `simple_query_string` (not covered here) - Simpler “more robust” for exposing to end-users. (how is simple more robust? seems counter-intuitive. no one knows.)

## `match` - standard full text query
* [Docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html)

By default does near-exact word equality matching.

**When to use**: If you're doing full-text search on a large text field, such as the body of this post.

Good default for matching a word or phrase in a block of text, e.g. searching in a large text field, description field, etc, such as searching for a phrase in someone’s Twitter bio.

Not good for partial matches of a single word or phrase, such as a partial match of someone’s Twitter username (see fuzziness below).

**Fuzzy matching**
Allows [fuzzy matching](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness) (e.g. typo tolerance) by specifying a [levenstein edit distance](http://en.wikipedia.org/wiki/Levenshtein_distance), number of one character changes needed to make one string match another string. Basically, it’s the number of typos you can make and still match the string.

*Example*: Your target match data is a field with the value “ajsharp”. With the `fuzziness` param set to 1, you can type “jjsharp” and it will match “ajsharp”. But if you type “jssharp” it won’t match, unless you increase `fuzziness` to 2.

## `match_phrase` - for matching exact phrases
* [Docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html)

**When to use:** If you need to be specific about whole phrase searches, or want to enable this functionality for your users.

Let’s say we have two twitter profiles indexed. Document A has the phrase “co-founder” in the bio, and Document B just has the word “founder”. 

If you search “founder” using match_phrase, both documents will match.
If you search “co-founder” using match_phrase, only Document A will match. However, had you done this query using `match`, both Document A and Document B would match the query “co-founder”

## `match_phrase_prefix` - poor man’s autocomplete
* [Docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html)

**When to use**: Only you need to match a single field. For many use-cases `multi_match` feels *far* more useful.

From the ES docs:
> Consider the query string quick brown f. This query works by creating a phrase query out of quick and brown (i.e. the term quick must exist and must be followed by the term brown). Then it looks at the sorted term dictionary to find the first 50 terms that begin with f, and adds these terms to the phrase query.

* Can be enhanced/improved upon with [Completion Suggester](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html)

##  `multi_match` - multi-field match 🎉🎉🎉
Allows you to search for the same string in multiple fields.

**When to use**: In many cases this is probably what you need if you're doing anything auto-completey (note: there are probably many uses cases this is great for but my primary one when I wrote this post was autocomplete, so 🤷🏻‍♂️).

Multi match queries can have a type:
* `best_fields`. Default. Match any field, but uses the _score from the best field. [Docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-best-fields)
* `most_fields` - Matches any field and combines the score
* `cross_fields` - Treats fields with same analyzer as if one big field.
* `phrase` - Uses `match_phrase`
* 🎉🎉🎉 `phrase_prefix` - Uses `match_phrase_prefix` on each field and combines the _score. Allows multi-field autocomplete 💥💥💥

**Field Boosting**
Fields can be boosted by using the `^` followed by number. Assume twitter profiles with screen_name and name fields. If you do a query like this, the screen_name will be three times as important as the name field in the ranking:
```javascript
{
  "query": {
    "multi_match" : {
      "query" : “ajsharp”,
      "fields" : [ “screen_name^3", “name” ] 
    }
  }
}
```

****

☝️ _**Be sure to check out [Sharesecret](https://www.sharesecret.co), which makes it easy to securely share sensitive data.**_