Elasticsearch from 0 to sky: Full-Text Queries

match_phrase query

This query is used to search for text when you want to find terms that are close to each other.

{
"query": {
"match_phrase": {
"author": "ivan frias"
}
}
}
{ "author":"hello Ivan Frias" }  (1)

{ "author":"Ivan frias" } (2)

{ "author":"hello ivan" } (3)
{
"query": {
"match_phrase": {
"author": {
"query":"ivan frias",
"slop": 1
}
}
}
}
{ "author":"Ivan frias" }              (1)

{ "author":"ivan hello Frias" } (2)

Searching in multiple fields

multi_match allows searching in multiple fields by matching against different fields.

{  
"query": {
"multi_match": {
"query": "ivan frias",
"fields": [
"author",
"body",
"tittle"
],
"type": "best_fields"
}
}
}
{  
"query": {
"multi_match": {
"query": "ivan frias",
"fields": [
"author^2",
"body",
"tittle"
],
"type": "best_fields"
}
}
}

Combining searches

We can use multi_match to search in multiple fields, this will often give a large number of results, we can use match_phrase combined by type.

{  
"query": {
"multi_match": {
"query": "ivan frias",
"fields": [
"author^2",
"body",
"tittle"
],
"type": "phrase"
}
}
}

Mispelt Werds

Sometimes writing errors are possible, this can be solved with Fuzziness, it has some disadvantages such as a high CPU load and poor precision.

{  
"query": {
"match": {
"author": {
"query": "ivam",
"fuzziness": 1
}
}
}
}

Data Engineer Elastic Stack